# working with rotation matrices

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 03-24-2005
hannibar
This is very handy indeed, and I think I will implent my camera that way. But it still doesn't satisfie all my needs. Another problem I have is the following :

I use ODE for physics. Ode returns me a rotation matrix, and doesn't provide a function to get the seperate rotation matrices for x, y and z. So I wont be able to do something like your camera class for the objects that use ode.

Or do you thing I'm approaching the problem the wrong way too? What I do now is : Store a rotation matrix for each object. I can do a few simple operations on it, like setting the rotation to (angx, angy, angz). Or I can rotate the matrix by multiplying with another matrix. But for the operations I asked (like setting only the y-axis to a specific value), I'm still stuck.
Or am I doing it the wrong way from the beginning ?
• 03-24-2005
Sang-drax
Quote:

Originally Posted by Bubba
Well if you start with a rotation matrix with all the values in it, I'm almost positive that rotation can be inverted. Take the inverse of the matrices you want to 'remove' from the final matrix and multiply the final matrix by these matrices. This will get you back to the original matrices.

Just for fun, how would you approach the problem of separating one rotation matrix R into, Rx, Ry and Rz?
I don't think it's obvious. The method I posted doesn't work for large angles.
• 03-25-2005
hannibar
Quote:

Originally Posted by Sang-drax
Just for fun, how would you approach the problem of separating one rotation matrix R into, Rx, Ry and Rz?
I don't think it's obvious. The method I posted doesn't work for large angles.

Indeed. When y or z are between PI/2 and 3*PI/2, x becomes Pi to large. As a consequence (because you multiply the wrong matrix) also y an z will be wrong. I think the only way to correct this, is when you know the sign of every cos and sin. I've been searching for a method to find these, but I haven't found one yet.
• 03-25-2005
VirtualAce
From the book.

Code:

```void ComputeHeadingPitchBank(float *hdg, float *pitch, float *bank) {   float m11,m12,m13;   float m21,m22,m23;   float m31,m32,m33;   *pitch=0.0f;   *hdg=0.0f;   *bank=0.0f;     //Extract pitch from m23, adjust for domain errors   float sp=-m23;   if (sp<=-1.0f)   {     pitch=-1.570796f;  //-pi/2   }   else if (sp>=1.0f)  {     pitch=1.570796f;  //pi/2   }    else   {     pitch=asin(sp);   }   //Check for gimbal lock   if (sp>0.9999f)   {     //We are looking/moving straight up     //Bank to zero and just set heading     bank=0.0f;     hdg=atan2(-m31,m11);   }   else   {     hdg=atan2(m13,m33);       //Compute bank from m21 and m22     bank=atan2(m21,m22);   } }```
Is there a way in ODE to retrieve the x,y and z matrices prior to all of the calculations? If there is then you can get the original angles from these. The way I'm doing it here is very slow.
• 03-25-2005
hannibar
As far as I know, can't get the x,y and z matrices prior to all of the calculations out of ODE. So your code won't work.
• 03-25-2005
VirtualAce
You misunderstood me. If you CAN get the x,y,z matrices then you don't have to use the code I posted. If not, you must use the code I posted. That code should work.

Your matrix will look like this:

m11,m12,m13,0
m21,m22,m23,0
m31,m32,m33,0
0,0,0,1

Just zero out the translation portion since it doesn't matter in this calculation.
• 03-28-2005
hannibar
Quote:

Originally Posted by Bubba
You misunderstood me. If you CAN get the x,y,z matrices then you don't have to use the code I posted. If not, you must use the code I posted. That code should work.

Your matrix will look like this:

m11,m12,m13,0
m21,m22,m23,0
m31,m32,m33,0
0,0,0,1

Just zero out the translation portion since it doesn't matter in this calculation.

Your code does not work. Try using your code on a rotation matrix with :
x-rot : 40 degrees (any number)
y-rot : 240 degrees (any number between Pi/2 and 3Pi/2)
z-rot : doesn't matter.
It will give wrong results.
Basically your code has the same problem as the one sang-drax posted. It doesn't work for y between Pi/2 and 3Pi/2 (because the cos is negative then, and changes the genus of m11, m12, m23 and m33, and atan2 interprets this wrongly).
• 03-28-2005
VirtualAce
So test for this condition and change the sign of the result.
Also it's possible the reason this does not work is because you need to change all rotations to be in range -180 to +180 or you could be in gimbal lock in which case you cannot separate the axis of rotation from another axis.

Keep posting here my friend and we will solve it or find a workaround.
• 03-30-2005
hannibar
Quote:

Originally Posted by Bubba
So test for this condition and change the sign of the result.

I've been trying to do this for a while but I couldn't find a solution for it. I've shown the problem to a few other people who are (to say the least) good in math. They told me that it is impossible to find the correct angles if you use only m11, m12, m13, m23 and m33. This is because it are 5 equations with 6 variables (3 times cos and sin).

So the only way to solve this would be to gather some more information from m21, m22, m31 and m32. And this would make it way to complicated, slow, and difficult to code.

That's why I've decided to stop searching. I will also stop my current project, learn about quaternions, restructure everything, and recode my engine from scratch. Hopefully with some good thinking, I will be able to avoid the problems I am having now.

Anyway thanks a lot for all the help.
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12