matrix4 -> Euler Angles

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Boogle
Posts: 162
Joined: Fri Nov 21, 2003 3:16 pm
Location: Toronto, Canada

matrix4 -> Euler Angles

Post by Boogle »

Does anyone have working Irrlicht code to convert a rotational matrix to Euler angles (the standard Irrlicht representation of angles)? I've seen various threads about this before with suggested code, but no one has ever come back and said that the code worked 100% for them.
Guest

Post by Guest »

You may have seen this function the last time I posted it, but if not here it is. This is what I currently use. It extracts the rotation information from a matrix4 and gives you the Euler Angles. It works for me 100%. I use it in my space sim to help calculate/update rotation from a tranlsated and transformed origin (using transform matrices). I havent gotten any feedback from anybody else trying it. Niko was gonna look at it though.

Most of this code I found on http://skal.planet-d.net/demo/matrixfaq.htm#Q2 then adapted it to Irrlicht.

Code: Select all

vector3df getRotationDegrees(matrix4 matrix) 
{ 
   matrix4 mat=matrix; 
   vector3df rotation; 
   f32 D,C,Y,rotx,roty,X,Z; 

   Y = D = -asinf( mat(2,0)); 
    C     =  cosf( Y ); 
    Y    *= 180.000f/PI; 

    if ( fabs( Y ) > 0.0005f ) 
      { 
      rotx      =  mat(2,2) / C; 
     roty      = mat(2,1)  / C; 
      X  = atan2f( roty, rotx ) * 180.0000f/PI; 
      rotx      =  mat(0,0) / C; 
      roty      = mat(1,0) / C; 
      Z  = atan2f( roty, rotx ) * 180.0000f/PI; 
      } 
    else 
      { 
      X  = 0.00000f; 

      rotx      = -mat(1,1); 
      roty      = mat(1,0); 

      Z  = atan2f( roty, rotx ) * 180.0000f/PI; 
      } 

   if (fabs(X)>=360.00f)X=0.00f; 
   if (fabs(Y)>=360.00f)Y=0.00f; 
   if (fabs(Z)>=360.00f)Z=0.00f; 

   rotation=vector3df(X,Y,Z); 
   return rotation; 
} 
Chev
Posts: 37
Joined: Wed Nov 19, 2003 6:15 am

Post by Chev »

oops wasn't logged in, the last post was me. :oops:
Boogle
Posts: 162
Joined: Fri Nov 21, 2003 3:16 pm
Location: Toronto, Canada

Post by Boogle »

Cool Thanks :)
powerpop
Posts: 171
Joined: Thu Jan 08, 2004 1:39 am
Location: san francisco

Post by powerpop »

the above code does not seem to be right - it works okay for X and Z (if you negate them) - but Y seems to be off - i havent figured out how to fix it yet - anyone have a good idea on how to correct it?
powerpop
Posts: 171
Joined: Thu Jan 08, 2004 1:39 am
Location: san francisco

Post by powerpop »

using that same math reference above my conversion looks a little different - i am still not getting the correct rotations though - any ideas? - the rotation matrix comed from tokamak

Code: Select all

   neM3 mat = body->GetRotationM3(); 
   f32 D,C,Y,rotx,roty,X,Z; 

    if ( fabs( Y ) > 0.0005f ) 
      { 
      rotx      =  mat[2][2] / C; 
      roty      = mat[1][2]  / C; 
      X  = (float)atan2f( roty, rotx ) * 180.0000f/irr::core::PI; 
      rotx      =  mat[0][0] / C; 
      roty      = -mat[0][1] / C; 
      Z  = (float)atan2f( roty, rotx ) * 180.0000f/irr::core::PI; 
      } 
    else 
      { 
      X  = 0.0f; 

      rotx      = mat[1][2]; 
      roty      = mat[1][1]; 

	  Z  = (float)atan2f( roty, rotx ) * 180.0000f/irr::core::PI; 
      } 

    /* return only positive angles in [0,360] */
    if (X < 0) X += 360.0f;
    if (Y < 0) Y += 360.0f;
    if (Z < 0) Z += 360.0f;

	  
   tempVect.X = -1.0 * X;
   tempVect.Y = -1.0 * Y;
   tempVect.Z = Z;

Chev
Posts: 37
Joined: Wed Nov 19, 2003 6:15 am

Post by Chev »

I'm not familiar with tokamak's matrices. The code example that I posted was for Irrlicht matrix4 format.
powerpop
Posts: 171
Joined: Thu Jan 08, 2004 1:39 am
Location: san francisco

Post by powerpop »

i eventually figured this out - its part of the irrtokamak code i posted in the projects area
Post Reply