Search code examples
androidopengl-esrotationcubemotion

Android OpenGL Rotate cube in the direction of touch movement


Possible Duplicate:
Visually correct rotation with OpenGl and touch screen capabilities

I have a cube which I want to rotate in the same direction I am moving my finger while touching. I do know how to get current touch movement and use those values to rotate the cube, but that's not what I want.

This is what I am currently using:

    case MotionEvent.ACTION_DOWN:
        mx = me.getX();
        my = me.getY();
        break;
    case MotionEvent.ACTION_MOVE:
        x_diff = mx - me.getX();
        y_diff = my - me.getY();
        mx = me.getX();
        my = me.getY();
        this.rot_x -= y_diff;
        this.rot_y -= x_diff;
        break;

Rotation:

gl.glRotatef(rot_x, 1, 0, 0);
gl.glRotatef(rot_y, 0, 1, 0);
gl.glRotatef(rot_z, 0, 0, 1);

Solution

  • I found a solution. The correct way to do it was implementing an ArcBall rotation.

    NeHe OpenGL Tutorial: http://nehe.gamedev.net/tutorial/arcball_rotation/19003/
    Java port: http://www.java-tips.org/other-api-tips/jogl/arcball-rotation-nehe-tutorial-jogl-port.html