Search code examples
javaopengl3drotationjmonkeyengine

Obtaining the camera rotation in radians on the X, Y, and Z axis in OpenGL?


I'm trying to obtain the camera rotation on various axis in OpenGL (but using Java, LWJGL, and jME specifically). The camera object allows me to get the direction as a Vector3f, but this doesn't seem to work to get the componentised rotation; each axis appears tied to another axis. I found that toAngleAxis with the angle component with offset was a quick hack, but doesn't work properly in most situations. I'm not so good at maths unfortunately, otherwise I may have been able to work out this problem :) Again, I just need the X, Y and Z axes componentised and in radians, from 0 radians to 2 PI radians.

Can anyone help?

Cheers and thanks in advance, Chris


Solution

  • Obtaining the rotation angels requires just transforming the view vector given in cartesian coordinates into spherical coordinates. You can find the formulas in wikipedia.

    viewvector = <x, y, z>
    
    r = sqrt(x² + y² + z²)
    phi = arctan2(y, x)
    theta = arccos(z / r)
    

    Note that you can only obtain two rotation angels form the view vector. Obtaining the third rotation angle requires knowing the projection plane x or y axis.