matrix4 -> Euler Angles
matrix4 -> Euler Angles
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.
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.
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;
}
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;