#### Archived

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

# Obtaining angles of rotation from a rotation Matrix

This topic is 5704 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.

##### Share on other sites
quote:
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.

Uhh, no. The eigenvector approach works perfectly. Absolutley *any* rotation matrix in 3-space has exactly one real eigenvalue and two complex ones, and the real one *always* is associated with the axis of rotation. That's right, *the one and only* axis of rotation. Any 3D rotation, no matter what Euler angles it is composed of is really just a rotation about one axis, x = (x1, x2, x3). This is not necesarily a coordinate system axis.

Recall that an eigenvector x, is the solution to:
A * x = alpha * x, A matrix, x vector, alpha scalar

You can easily see from this, that the eigenvector with real alpha (the eigenvalue) will be a vector that does not change direction when multiplied by a matrix. For rotation matrices, it is easy to see that the only vectors that do this are those along the axis.

[edited by - MisterAnderson42 on May 6, 2002 4:40:35 PM]

##### Share on other sites
Well, except for rotations of 180 degrees around an axis you seem to be right. I suppose those are technically reflections. So given an axis of rotation how would you find the rotation? Find the angle between the orthogonal component of a vector before and after rotation? Then if you want a rotation about the x, y and then z axis that angle would be your rotation about the x-axis wouldn''t it? Infinite number of ways to get from point a to b aside. Your other two angles would be the angles needed to bring the x-axis to the direction of the eigen vector. As far as selecting an eigen vector it seems like you could use the cross product of the before and after vectors you used for finding the rotation angle. It seems like that would solve the +/- angle problem. Reversing your axis of rotation would reverse the angle wouldn''t it?

##### Share on other sites
quote:
Well, except for rotations of 180 degrees around an axis you seem to be right. I suppose those are technically reflections.

A 180 degree rotation is not a reflection.

And what MisterAnderson said is correct, for any rotation matrix there is exactly one axis of rotation. There may be several rotation axes which would take point A to point B but a matrix acts on all points.

##### Share on other sites
I have been taking the pure mathematical approach. This algorithm goes as follows:

find the real eigenvalue of U, a 3x3 rotation (or upper 3x3 of a homogenous transformation)

solve U - transpose(U) = 2 * sin(alpha) * A
where A =
[0 -a3 a2]
[a3 0 -a1]
[-a2 a1 0]

and (a1, a2, a3) is the real eigenvector (axis of rotation).

quote:
As far as selecting an eigen vector it seems like you could use the cross product of the before and after vectors you used for finding the rotation angle

Yes, this would work in most cases. One must take care with this method not to choose a test vector too close to the axis, as this will produce inaccurate results. (of course, choosing the axis exactly will give back an axis of 0,0,0, but this can be checked for).

As far as factoring rotation matrices into XYZ Euler angles goes, there are many methods. I have offered an alternative solution because Euler angles are evil. Dave Eberly goes over this in 3d Game Engine Design, and I''m sure the method is available elswhere on the net. Consequently, Eberly also covers a method for extracting the axis-angle rotation from a matrix. He does not derive the result there, and I do not understand where it comes from.

##### Share on other sites
The logic for saying it is a reflection is that a rotation of 180 degrees around an axis is equivalent to a refection across two orthogonal planes whose intersection is the axis of rotation. Alternatively it is a reflection across a line. The point was that it was a special case where you get more than one real eigen value. I personally have no problem with saying that is not a rotation matrix. I also have no problem saying that rotation by 360 degrees around an axis is not a rotation since it is the identity matrix. If you want to say the identity matrix is a rotation matrix that is fine by me too.

##### Share on other sites
A rotation about 3 axes is equivalent to a rotation about 1 axis. When you compose several rotations, you end up with another rotation. This is why it is convenient and intuitive to look at one eigenvector.

##### Share on other sites
quote:
The logic for saying it is a reflection is that a rotation of 180 degrees around an axis is equivalent to a refection across two orthogonal planes whose intersection is the axis of rotation

OK. That''s 2 reflections.

quote:
The point was that it was a special case where you get more than one real eigen value.

Then the axis of rotation is the eigenvector associated with the eigenvalue 1. In your 180 degree rotations there are two eigenvalues which are -1 (the complex conjugate pair). What MisterAnderson was saying is still true, you discard this pair and the remaining real one corresponds to the rotation axis.