#### Archived

This topic is now archived and is closed to further replies.

# Obtaining angles of rotation from a rotation Matrix

This topic is 5914 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi guys, I have a 4x4 matrix used for rotations about the x,y,z, axis. I need to extract the angles applied to the 4x4 matrix, so that I can like display the values on the user''s display. If my question seems vague ... void getAngles(const Matrix44 rotMatrix44,double* rotX,double* rotY,double* Z); Heh, that would be my prototype, hense passing a rotation matrix and getting back the 3 angles of rotation. Thanks! Nelson

##### Share on other sites
I didn''t have time to really think this through and do out the math, but I might be able to get you started...

You know that any rotation matrix must keep one subspace invariant, which implies the existence of one eigenvector. So we can solve for this eigenvector by finding the roots of the charateristic polynomial (Let''s eliminate the 4th dimension which doesn''t really play a role until you do translations and perspective projections). Once you have an eigenvector, you know that the object has been rotated about this axis. By how much? Well, the trace of any transformation is constant regardless of how it is represented by a matrix. So, we''ll let the matrix
[[1 0 0]
[0 cos a -sin a]
[0 sin a cos a]] be the matrix that represents the rotation about the eigenvector of the original. We can tell immediately that the traces of the two matrices must be equal. So if A is your original matrix minus the 4th row and column, then the angle that the transformation rotated it about its eigenvector is equal to

angle = arccos{(1/2)[1 + tr(A)]}

Now that''s as far as I''ve thought. I''m not sure how exactly you would go about changing this to 3 angles, and I''m not even sure that I''m on the right track, but this would probably provide you with good information that''s easier to visualize anyways.

##### Share on other sites
Well I've never solved this problem, so I can't give you a definitive answer how to do it. But you should read this article on Euler angles and rotation matrices from mathworld. They give you the general formula for rotation matrices with respect to the 3 angles. From a glance, it looks like you could solve for the 3 angles by hand, and then just plug and chug in your function. (It may look like a lot of equation, but you actually only need to use 3 of the equations.)

Someone could correct me on this, but I think that they way you interpret rotX, rotY, and rotZ depend on the order that you rotate in.

Hope this helps.

Edit: Looking at that article again, the formula for the matrix A that you want is probably the one that's about halfway down the page, where they talk about pitch, roll, and yaw.

[edited by - Mr_Burns on May 5, 2002 4:18:10 AM]

##### Share on other sites
-Whoa,heh, I had to pull out my old mathbook to somewhat understand yellowjon,heh. I kinda do now, but where did you get the 3x3 matrix of
[[1 0 0 ]
[0 cosa -sina ]
[0 sina cosa ]]

-Mr Burns,
Yes, what I could do is solve for all the angle by using the expressions I derived by multiplying the X,Y,Z rotational matrices.
e.g. if the compound rotational Matrix C, whose
(1,1)=sinX*sinY*sinZ + cosX*cosZ
I have the value on the left side of the equation.
Since there are 9 other derived equations, and 6 unknowns (sinx,cosx,siny,cosy,sinz,cosz) I can prolly solve it by hand etc...

-Hmm, I thought this was done before, since it''s not in stone, I think I just might save the angles somewhere else. Actually what I am trying to solve overall is that my camera is rotated in x,y,z, but the camera''s matrix is rotated as -x,-y,-z (to obviously emulate drawing of the objects in the opposited rotated direction)

However if the camera wants to move forward by offV=(+1,+1,+1) in it''s
local coordinates, I have to rotate offV by the orientation of the camera ,or else it''ll move in world coordinates. But I cant use the matrix applied cuz its -x,-y,-z, not +x,+y,+z.
E.g if the camera is lookin at (0,-45d,0) then the matrix would be (0,45d,0). Now the camera wants to move (0,0,-1) (one step forward), If I use (0,45d,0) to rotate offV ,the new offV for the move would be (+.707,0,+.707) instead of (-.707,0,-.707).

So my solution was to extract the angles out , negate them and use these to rotate offV, But alas eh...

Thanks alot!
:D (reading up on eigen stuff,heh)

##### Share on other sites
Given an arbitrary rotation matrix, there are an infinite set of x,y,z Euler angles that will give you that matrix. You will not be able to find a uniqe set. There are internet sites that describe an algorithm that will give you one of these. Don''t expect the answer to be the values you have the matrix initially.

The first poster has a point about using eigenvectors. All rotation matrices in 3 space have one real eigenvalue and 2 complex conjugates. The eigenvector that is asociated with the real eigenvalue is the axis of rotation. From here, it is trivial to find the angle of rotation. Now, you are left with two equivalent rotations. A rotation of -alpha about the axis -eigenvector1 or a rotation of alpha about the axis eigenvector1. Any linear algebra textbook should give details on this method. Though, I''m sure that there are internet sites that give the bottom line for a solution. Look up "axis angle decomposing a rotation matrix"

Josh

##### Share on other sites
If you''re interested in finding the eigenvector and the angle of rotation about that axis, as MrAnderson suggested, about halfway down this article they give the general form of the rotation matrix about an arbitrary axis.

quote:

I think I just might save the angles somewhere else. Actually what I am trying to solve overall is that my camera is rotated in x,y,z, but the camera''s matrix is rotated as -x,-y,-z (to obviously emulate drawing of the objects in the opposited rotated direction)

If this means that you have the angles ahead of time, I see no reason why you shouldn''t just save them.

##### Share on other sites
Sorry, I guess I didn''t explain where that matrix came from. It represents a rotation around an axis where the first basis vector is kept invariant. Basically, my point was that if I chose the rotation to be represented by another matrix using the eigenvector (with eigenvalue 1) to be the first basis vector, I could solve for the trace. Since I know that the trace is invariant in all the matrices that represent such a transformation, I can just solve for the angle using the trace in the original matrix.

##### Share on other sites
Just curious. If you ran a vector through the rotation then would the axis of rotation be the cross product of the vector before and after rotation?

Yes.

##### Share on other sites
Looking at the original post he is talking about rotations about multiple axes and not a single axis. It seems like the eigen vector approach wouldn''t work then since there may be no real eigen values.

It seems like you could find the spherical coordinates of the transformed x-axis and use that to find two rotations that will put the transformed x-axis back to the original x-axis. After that rotation the y and z axes could be rotated to line them up with the original y and z axes. Assuming theta is the angle in the xy plane and phi is the angle with the z axis then theta would be just a rotation about the z axis. Once you did that then pi/2-phi would be a rotation about the y axis. Then the final rotation would be around the x-axis.

So if (x,y,z) is your transformed x-axis and you are moving back to a standard basis then r would be the magnitude of the transformed vector, theta would be atan2(x,y) and phi would be acos(z/r). theta is what got you there so -theta is what would get you back. pi/2-phi is what got you there so phi-pi/2 is what would get back. The easiest now would be to actually build that transform matrix and use it to find where the y or z axis now points which I think will make the last angle either acos(y/r) or acos(z/r) depending upon which you used.

Well, at least it seems plausible that it would work. If either of the first two extractions are wrong then your third one will be wrong as well so I would keep that in mind when debugging it. Also this is only going to tell you how to build the matrix from a specific concatenation of rotations. If you did a hundred incremental rotations there is no way to tell that any more than you can tell if you got 10 from 5+5 or 3+7.

This will only take you back to the standard basis. If you want to go to some other basis then you have to use dot products to find the angles. Things like z/r are z/r because the dot product of (x,y,z).(0,0,1) is z. If you scaled before rotation then that shouldn''t have any impact. Everything is ratios of lengths. If you scaled after with nonuniform scaling then your angles are going to be all wrong if you don''t unscale it first. As a general rule whatever order you want to build the matrix in you have to factor it in the reverse order.

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 38
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631708
• Total Posts
3001837
×