Created
August 31, 2014 16:00
-
-
Save Happsson/ff064f7b436a30f88d55 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package se.stalofilm.distanceangle; | |
import com.badlogic.gdx.ApplicationAdapter; | |
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.InputProcessor; | |
import com.badlogic.gdx.graphics.Color; | |
import com.badlogic.gdx.graphics.GL20; | |
import com.badlogic.gdx.graphics.OrthographicCamera; | |
import com.badlogic.gdx.graphics.g2d.BitmapFont; | |
import com.badlogic.gdx.graphics.g2d.SpriteBatch; | |
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; | |
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; | |
import com.badlogic.gdx.math.Vector2; | |
import com.badlogic.gdx.utils.Array; | |
public class Angle extends ApplicationAdapter { | |
SpriteBatch batch; | |
OrthographicCamera camera; | |
BitmapFont font; | |
float x,y, xprev,yprev, deltax, deltay, accX1, accY1, accX2, accY2; | |
float angle; | |
Array<Vector2> first; | |
Array<Vector2> second; | |
Vector2 calculated; | |
ShapeRenderer sr; | |
int touches = 0; | |
Vector2 vec1 = new Vector2(0,0); | |
Vector2 vec2 = new Vector2(0,0); | |
@Override | |
public void create () { | |
batch = new SpriteBatch(); | |
font = new BitmapFont(); | |
sr = new ShapeRenderer(); | |
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); | |
xprev = Gdx.input.getAccelerometerX(); | |
yprev = Gdx.input.getAccelerometerY(); | |
accX1 = 0; | |
accY1 = 0; | |
accX2 = 0; | |
accY2 = 0; | |
calculated = new Vector2(0,0); | |
first = new Array<Vector2>(); | |
second = new Array<Vector2>(); | |
Gdx.input.setInputProcessor(new InputProcessor() { | |
@Override | |
public boolean keyDown(int keycode) { | |
// TODO Auto-generated method stub | |
return false; | |
} | |
@Override | |
public boolean keyUp(int keycode) { | |
// TODO Auto-generated method stub | |
return false; | |
} | |
@Override | |
public boolean keyTyped(char character) { | |
// TODO Auto-generated method stub | |
return false; | |
} | |
@Override | |
public boolean touchDown(int screenX, int screenY, int pointer, | |
int button) { | |
touches++; | |
return false; | |
} | |
@Override | |
public boolean touchUp(int screenX, int screenY, int pointer, | |
int button) { | |
touches++; | |
if(touches == 6) touches = 0; | |
return false; | |
} | |
@Override | |
public boolean touchDragged(int screenX, int screenY, int pointer) { | |
// TODO Auto-generated method stub | |
return false; | |
} | |
@Override | |
public boolean mouseMoved(int screenX, int screenY) { | |
// TODO Auto-generated method stub | |
return false; | |
} | |
@Override | |
public boolean scrolled(int amount) { | |
// TODO Auto-generated method stub | |
return false; | |
} | |
}); | |
} | |
@Override | |
public void render () { | |
Gdx.gl.glClearColor(0, 0, 0, 1); | |
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); | |
x = Gdx.input.getAccelerometerX(); | |
y = Gdx.input.getAccelerometerY(); | |
deltax = x - xprev; | |
deltay = y - yprev; | |
if(touches == 0){ | |
first = new Array<Vector2>(); | |
second = new Array<Vector2>(); | |
accX1 = 0; | |
accX2 = 0; | |
accY1 = 0; | |
accY2 = 0; | |
vec1.set(0,0); | |
vec2.set(0,0); | |
} | |
if(touches == 1){ | |
if(x<0 && y<0) first.add(new Vector2(x,y)); | |
accX1 = x; | |
accY1 = y; | |
} | |
if(touches == 3){ | |
if(x<0 && y < 0) second.add(new Vector2(x,y)); | |
accX2 = x; | |
accY2 = y; | |
}if(touches > 3){ | |
calculate(); | |
} | |
batch.begin(); | |
font.drawMultiLine(batch, | |
"\n\nAccX1 = " + accX1 + | |
"\nAccY1 = " + accY1 + | |
"\nVec1: " + vec1.toString() + | |
"\nAccX2 = " + accX2 + | |
"\nAccY2 = " + accY2 + | |
"\nVec2: " + vec2.toString() + | |
"\nANGLE = " + angle | |
, Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2); | |
batch.end(); | |
sr.setProjectionMatrix(camera.combined); | |
sr.begin(ShapeType.Line); | |
sr.setColor(Color.GREEN); | |
if(!vec1.isZero() && !vec2.isZero()){ | |
sr.line(new Vector2(0,0), vec1); | |
sr.line(new Vector2(0,0), vec2); | |
} | |
sr.end(); | |
xprev = x; | |
yprev = y; | |
} | |
private void calculate() { | |
vec1 = new Vector2(0,0); | |
vec2 = new Vector2(0,0); | |
for(int i = 0; i < first.size; i++){ | |
vec1.x = vec1.x + first.get(i).x; | |
vec1.y = vec1.y + first.get(i).y; | |
} | |
vec1.x = vec1.x / first.size; | |
vec1.y = vec1.y / first.size; | |
for(int i = 0; i < second.size; i++){ | |
vec2.x = vec2.x + second.get(i).x; | |
vec2.y = vec2.y + second.get(i).y; | |
} | |
vec2.x = vec2.x / second.size; | |
vec2.y = vec2.y / second.size; | |
angle = vec1.angle(vec2); | |
vec1.scl(20f); | |
vec2.scl(20f); | |
} | |
@Override | |
public void dispose() { | |
Gdx.app.exit(); | |
batch.dispose(); | |
font.dispose(); | |
sr.dispose(); | |
super.dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment