Quaternion To Euler

Started by
-1 comments, last by Alundra 9 years, 8 months ago

Hi all,

recently I have added a quaternion to euler to have a visual value of the quaternion but of course the value is not used in game.

Here the code I use :


void CQuaternion::ToEulerAngles( float* X, float* Y, float* Z ) const
{
  const float SingularityTest = x*y + z*w;
  if( SingularityTest > 0.4999995f )
  {
    *Y = 2.0f * CMath::ATan2( x, w );
    *Z = CMath::HALF_PI;
    *X = 0.0f;
  }
  else if( SingularityTest < -0.4999995f )
  {
    *Y = -2.0f * CMath::ATan2( x, w );
    *Z = -CMath::HALF_PI;
    *X = 0.0f;
  }
  else
  {
    const float zz = z * z;
    *Y = CMath::ATan2( 2.0f * (y*w - x*z), 1.0f - 2.0f * (y*y + zz) );
    *Z = CMath::ASin( 2.0f * SingularityTest );
    *X = CMath::ATan2( 2.0f * (x*w - y*z), 1.0f - 2.0f * (x*x + zz) );
  }
}

The code is based on this article :

http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/

Is it a correct way to do or a better way exists ?

Thanks for the help

This topic is closed to new replies.

Advertisement