# Angle extraction from matrix

Hello,

I am trying to extract the angles of rotation about the x y z axes from the currnet matrix.

I use glGetDoublev to get the matrix and then the following equations

rotation y = -asin(matrix[8])
rotation x = atan2(matrix[9],matrix[10])
rotation z = atan2(matrix[4],matrix[0])

this works ±180 degrees for x and z rotation

but only ±90 degres for y rotation. I need ± 180 degrees for y rotation.

Hopefully someone has a simple fix.

Your problem is not well constrained as stated. If you allow 180 degrees around one axes, and then 180 degrees around another axis, then there are (at least) 2 solutions that will end up in the same point.

If you want an in-depth understanding of the math involved, I suggest googling for “polar matrix decomposition.”

If I am not mistaken this problem is connected to the everpresent Gimbal lock. You should
search the web on Gimbal lock, Quaternion. Modify your camera code, and then the values you obtain, along with your camera controls will work without problems.

Thanks for your suggestion. I am looking thru the links but so far they seem to reduce to the equations I already have.

The following restates the problem in case I have not explained myself.

Apart from special cases when the rotation about y is 90 degrees or -90 degrees the equations I have been using give 2 results; the angle and pi - angle. It is this ambiguity I would like to solve.

To demonstrate the problem render a cube using a matrix that has had 135 degree rotation applied to each axis. Decompose the matrix and you get x rotation of 135 deg
z rotation of 135 degrees and two possible results for x of 45 and 135 degrees.

Just FYI: The book “Graphics Gems II” has a chapter on decomposing transformations matrices into rotation/scale/translation matrices. Perhaps that would help.

I believe the source code from all the Gems books is freely available on the net.

-Brian

Thankyou,

The section in Graphic Gems II is ‘Decomposing a Matrix Into Simple Transformations’. I found the source code sample on http://www.acm.org/tog/GraphicsGems

I will try the example code and see if I can understand the math.