working with rotation matrices

This is a discussion on working with rotation matrices within the Game Programming forums, part of the General Programming Boards category; This is very handy indeed, and I think I will implent my camera that way. But it still doesn't satisfie ...

  1. #16
    Registered User
    Join Date
    Feb 2005
    Posts
    61
    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 ?

  2. #17
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    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.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #18
    Registered User
    Join Date
    Feb 2005
    Posts
    61
    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.

  4. #19
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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.

  5. #20
    Registered User
    Join Date
    Feb 2005
    Posts
    61
    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.

  6. #21
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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.
    Last edited by VirtualAce; 03-25-2005 at 04:07 PM.

  7. #22
    Registered User
    Join Date
    Feb 2005
    Posts
    61
    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).

  8. #23
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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.
    Last edited by VirtualAce; 03-28-2005 at 02:33 PM.

  9. #24
    Registered User
    Join Date
    Feb 2005
    Posts
    61
    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.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. camera rotation matrix
    By Vick jr in forum Game Programming
    Replies: 5
    Last Post: 05-26-2009, 08:16 AM
  2. Replies: 8
    Last Post: 05-07-2009, 11:31 AM
  3. one final problem with rotation
    By DavidP in forum Game Programming
    Replies: 3
    Last Post: 11-19-2003, 02:50 AM
  4. 90 Degree Rotation Blt
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 04-08-2002, 07:05 PM
  5. saving contents of a function
    By speve in forum C Programming
    Replies: 5
    Last Post: 01-05-2002, 07:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21