• Create Account

### #ActualEndurion

Posted 19 May 2012 - 10:55 PM

This routine served me well (the one time I used it), always be careful with these three angles, usually it ends up in tears and gimbal locks:

void ToEulerAngles( float& fAngleX, float& fAngleY, float& fAngleZ )
{
static float  fRadiansToDegree = 180.0f / 3.1415926f;
fAngleY	 = asinf( M[8] );		// Calculate Y-axis angle
float fCY   =  cosf( fAngleY );
if ( fabsf( fCY ) > 0.005f )
{
float ftrx	  =  M[10] / fCY;	// No, so get X-axis angle
float ftry	  = -M[9]  / fCY;
fAngleX  = atan2f( ftry, ftrx ) * fRadiansToDegree;
ftrx	  =  M[0] / fCY;			// Get Z-axis angle
ftry	  = -M[4] / fCY;
fAngleZ  = atan2f( ftry, ftrx ) * fRadiansToDegree;
}
else
{
// Gimbal lock?
fAngleX  = 0;					  // Set X-axis angle to zero
float ftrx	  =  M[5];		   // And calculate Z-axis angle
float ftry	  =  M[1];
fAngleZ  = atan2f( ftry, ftrx ) * fRadiansToDegree;
}
if ( fAngleX < 0 )
{
fAngleX += 360;
}
if ( fAngleY < 0 )
{
fAngleY += 360;
}
if ( fAngleZ < 0 )
{
fAngleZ += 360;
}
}


The routine is a member of a 4x4 matrix with these members:

    struct tStruct
{
float	    _11, _12, _13, _14;
float	    _21, _22, _23, _24;
float	    _31, _32, _33, _34;
float	    _41, _42, _43, _44;
};

union
{
tStruct   ms;
float	 m[4][4];
float	 M[16];
};


### #2Endurion

Posted 19 May 2012 - 10:54 PM

This routine served me well (the one time I used it), always be careful with these three angles, usually it ends up in tears and gimbal locks:

void ToEulerAngles( float& fAngleX, float& fAngleY, float& fAngleZ )
{
static float  fRadiansToDegree = 180.0f / 3.1415926f;
fAngleY	 = asinf( M[8] );		// Calculate Y-axis angle
float fCY   =  cosf( fAngleY );
if ( fabsf( fCY ) > 0.005f )
{
float ftrx	  =  M[10] / fCY;	// No, so get X-axis angle
float ftry	  = -M[9]  / fCY;
fAngleX  = atan2f( ftry, ftrx ) * fRadiansToDegree;
ftrx	  =  M[0] / fCY;			// Get Z-axis angle
ftry	  = -M[4] / fCY;
fAngleZ  = atan2f( ftry, ftrx ) * fRadiansToDegree;
}
else
{
// Gimbal lock?
fAngleX  = 0;					  // Set X-axis angle to zero
float ftrx	  =  M[5];		   // And calculate Z-axis angle
float ftry	  =  M[1];
fAngleZ  = atan2f( ftry, ftrx ) * fRadiansToDegree;
}
if ( fAngleX < 0 )
{
fAngleX += 360;
}
if ( fAngleY < 0 )
{
fAngleY += 360;
}
if ( fAngleZ < 0 )
{
fAngleZ += 360;
}
}


The routine is a member of a 4x4 matrix with these members:

    union
{
tStruct   ms;
float	 m[4][4];
float	 M[16];
};


### #1Endurion

Posted 19 May 2012 - 10:53 PM

This routine served me well (the one time I used it), always be careful with these three angles, usually it ends up in tears and gimbal locks:

void ToEulerAngles( float& fAngleX, float& fAngleY, float& fAngleZ )
{
static float  fRadiansToDegree = 180.0f / 3.1415926f;
fAngleY	 = asinf( M[8] );	    // Calculate Y-axis angle
float fCY   =  cosf( fAngleY );
if ( fabsf( fCY ) > 0.005f )
{
float ftrx	  =  M[10] / fCY;    // No, so get X-axis angle
float ftry	  = -M[9]  / fCY;
fAngleX  = atan2f( ftry, ftrx ) * fRadiansToDegree;
ftrx	  =  M[0] / fCY;		    // Get Z-axis angle
ftry	  = -M[4] / fCY;
fAngleZ  = atan2f( ftry, ftrx ) * fRadiansToDegree;
}
else
{
// Gimbal lock?
fAngleX  = 0;					  // Set X-axis angle to zero
float ftrx	  =  M[5];		   // And calculate Z-axis angle
float ftry	  =  M[1];
fAngleZ  = atan2f( ftry, ftrx ) * fRadiansToDegree;
}
if ( fAngleX < 0 )
{
fAngleX += 360;
}
if ( fAngleY < 0 )
{
fAngleY += 360;
}
if ( fAngleZ < 0 )
{
fAngleZ += 360;
}
}


PARTNERS