0

First off apologies, I think i have had multiple entries with similar problems but have not been able to fix my issue as i dont believe i was able to explain my problem properly hopefully the crudely drawn image will help explain my problem. enter image description here

I have a square moving around my world on the x and y axis, the object goes off the screen and comes back on sometimes as it moves around. I want to use the gyroscope in the device to get values which will be used to change where the camera is looking by moving it in the x and y direction, to try and search for the object as it is moving around the world. This is the current code which seems to warp the shape of the square as i move the device.

class SquareRenderer implements GLSurfaceView.Renderer {

private int counter = 0;
private boolean mTranslucentBackground;
private Square mSquare;
private float mTransY;
private float mTransX;
private float mAngle;
private Context context;

private float xPoint = 0.0f;
private float yPoint = 0.0f;


public SquareRenderer(boolean useTranslucentBackground,Context context) {
    mTranslucentBackground = useTranslucentBackground;
    this.context=context;
    mSquare = new Square();
}

public void onDrawFrame(GL10 gl) {
    gl.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
    gl.glMatrixMode(GL11.GL_MODELVIEW);
    gl.glLoadIdentity();
    gl.glTranslatef(mTransX, mTransY, -10.0f);
    GLU.gluLookAt(gl, 0.0f, 0.0f, 0.0f,(float) MainActivity.azimuth , (float) MainActivity.roll, 0.0f, 0.0f, 1, 0.0f);

    mSquare.draw(gl);
    //GLU.gluLookAt(gl, 0.0f, 0.0f, 0.0f, (float) MainActivity.azimuth, (float) MainActivity.roll, 0.0f, 0.0f, 1, 0.0f);
    Random rnd = new Random();

    float maxx = 180.0f;
    float minx = -180.0f;
    float maxy = 90.0f;
    float miny = -90.0f;

    if( xPoint == 0.0f && yPoint == 0.0f){
        xPoint = rnd.nextFloat()*(maxx-minx)+minx;
        xPoint = round(xPoint,1);
        yPoint = rnd.nextFloat()*(maxy-miny)+miny;
        yPoint = round(yPoint,1);

        Log.d("XPOINT    YPOINT",xPoint +"      " +yPoint);
    }
    if(mTransX != xPoint && xPoint>mTransX) {
        mTransX += 0.1f;
        mTransX = round(mTransX,1);
    }else if(mTransX != xPoint && xPoint<mTransX){
        mTransX -= 0.1f;
        mTransX = round(mTransX,1);
    }else if(mTransX == xPoint){
        xPoint = rnd.nextFloat()*(maxx-minx)+minx;
        xPoint = round(xPoint,1);
    }
    if(mTransY != yPoint && yPoint >mTransY) {
        mTransY += 0.1f;
        mTransY = round(mTransY,1);
    }else if(mTransY != yPoint && yPoint < mTransY){
        mTransY -= 0.1f;
        mTransY = round(mTransY,1);
    }else if(mTransY == yPoint){
        yPoint = rnd.nextFloat()*(maxy-miny)+miny;
        yPoint = round(yPoint,1);
    }
}

public void onSurfaceChanged(GL10 gl, int width, int height) {
    gl.glViewport(0, 0, width, height);
    float ratio = (float) width / height;
    gl.glMatrixMode(GL11.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
}

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    gl.glDisable(GL11.GL_DITHER);
    gl.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_FASTEST);
    gl.glClearColor(0,0,0,0);
    gl.glEnable(GL11.GL_CULL_FACE);
    gl.glShadeModel(GL11.GL_SMOOTH);
    gl.glEnable(GL11.GL_DEPTH_TEST);
}

public static float round(float d, int decimalPlace) {
    BigDecimal bd = new BigDecimal(Float.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
    return bd.floatValue();
}

The azimuth and roll values are the ones obtained through the gyroscope. This is my first time with making an augmented reality-esque app and so any help will be appreciated.

user3074140
  • 733
  • 3
  • 13
  • 30

1 Answers1

0

Why are you still using the azimuth and roll as cartesian vector components? Those are polar coordinates and need to be transformed with trigonometry functions or used with rotate methods on the matrix.

Although this seems to be your main issue I might be wrong. You need a system on how to actually identify the problem. How about you first add some touch events which turn the camera on drag to see if you get the expected results. Those should simulate your azimuth and roll values.

As for integrating gyroscope with the camera there are multiple answers out there already. Even for Android such as this one

Community
  • 1
  • 1
Matic Oblak
  • 16,318
  • 3
  • 24
  • 43