Skip to content

Instantly share code, notes, and snippets.

@Happsson
Created September 1, 2014 21:45
Show Gist options
  • Save Happsson/f61719ac806c366315d7 to your computer and use it in GitHub Desktop.
Save Happsson/f61719ac806c366315d7 to your computer and use it in GitHub Desktop.
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, distance;
Array<TimeSlice> first;
Array<TimeSlice> second;
Vector2 calculated;
ShapeRenderer sr;
int touches = 0;
Vector2 vec1 = new Vector2(0,0);
Vector2 vec2 = new Vector2(0,0);
long startTime;
float[] firstFloatArrayX;
float[] firstFloatArrayY;
@Override
public void create () {
batch = new SpriteBatch();
font = new BitmapFont();
sr = new ShapeRenderer();
camera = new OrthographicCamera(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
xprev = Gdx.input.getAccelerometerX();
yprev = Gdx.input.getAccelerometerY();
accX1 = 0;
accY1 = 0;
accX2 = 0;
accY2 = 0;
calculated = new Vector2(0,0);
first = new Array<TimeSlice>();
second = new Array<TimeSlice>();
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++;
startTime = System.currentTimeMillis();
return false;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer,
int button) {
touches++;
if(touches == 4) 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();
if(touches == 0){
startTime = 0;
first = new Array<TimeSlice>();
second = new Array<TimeSlice>();
accX1 = 0;
accX2 = 0;
accY1 = 0;
accY2 = 0;
vec1.set(0,0);
vec2.set(0,0);
}
if(touches == 1){
first.add(new TimeSlice(new Vector2(x,y), System.currentTimeMillis() - startTime));
}
if(touches == 2){
sr.setProjectionMatrix(camera.combined);
printoutFirst();
sr.end();
}
if(touches == 3){
// second.add(new TimeSlice(new Vector2(x,y), System.currentTimeMillis()-startTime));
}if(touches > 3){
// calculate();
}
batch.begin();
font.drawMultiLine(batch,
"\n\nAccX1 = " + x +
"\nAccY1 = " + y +
"\nDistance = " + distance
, Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
batch.end();
sr.setProjectionMatrix(camera.combined);
sr.begin(ShapeType.Line);
sr.setColor(Color.GREEN);
sr.line(new Vector2(0,0), new Vector2(0,x*20));
sr.setColor(Color.RED);
sr.line(new Vector2(20,0), new Vector2(20, y *10));
sr.end();
}
private void printoutFirst() {
firstFloatArrayX = new float[(first.size * 2) + 2];
firstFloatArrayY = new float[(first.size * 2) + 2];
float yC1 = 0;
float yC2 = 0;
int j = 1;
int i = 0;
int c = 0;
distance = 0;
while(j < firstFloatArrayX.length-2){
float xC = first.get(c).getSlice() / 10f;
if(c>5){
yC1 = yC1 + ((
first.get(c).getDerivata().x +
first.get(c-1).getDerivata().x +
first.get(c-2).getDerivata().x) +
first.get(c-3).getDerivata().x +
first.get(c-4).getDerivata().x
/5);
yC2 = yC2 + ((
first.get(c).getDerivata().y +
first.get(c-1).getDerivata().y +
first.get(c-2).getDerivata().y) +
first.get(c-3).getDerivata().y +
first.get(c-4).getDerivata().y
/5);
}
firstFloatArrayX[i] = xC;
firstFloatArrayX[j] = yC1;
firstFloatArrayY[i] = xC;
firstFloatArrayY[j] = yC2;
distance = distance + (xC * yC1) + (xC * yC2);
j = j+2;
i = i+2;
c++;
}
firstFloatArrayX[firstFloatArrayX.length-2] = firstFloatArrayX[firstFloatArrayX.length-3];
firstFloatArrayX[firstFloatArrayX.length-1] = 0;
firstFloatArrayY[firstFloatArrayY.length-2] = firstFloatArrayY[firstFloatArrayY.length-3];
firstFloatArrayY[firstFloatArrayY.length-1] = 0;
sr.begin(ShapeType.Line);
sr.setColor(Color.BLUE);
sr.polygon(firstFloatArrayX);
sr.setColor(Color.OLIVE);
sr.polygon(firstFloatArrayY);
}
@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