Sign in to follow this  

Euler to Quaternion and back, with rotation order yaw, pitch, roll

This topic is 1924 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I used the book "Game Math" as the basis of a lot of my code. Their euler to quaternion conversion code creates rotations in the order of pitch then yaw. (My camera isn't rolled, so I don't know what order that is in.)

I need euler rotations converted to quaternions and back, with the order of rotation yaw, pitch, then roll. I've got everything working except the gimbal lock check.
Euler to Quat:
[CODE]Quat::Quat(const Vec3& euler)
float x = Math::Mod(euler.x, 360.0);
float y = Math::Mod(euler.y, 360.0);
float z = Math::Mod(euler.z, 360.0);
float c1 = Math::Cos(-z/2.0);
float s1 = Math::Sin(-z/2.0);
float c2 = Math::Cos(-x/2.0);
float s2 = Math::Sin(-x/2.0);
float c3 = Math::Cos(-y/2.0);
float s3 = Math::Sin(-y/2.0);
float c1_c2 = c1 * c2;
float s1_s2 = s1 * s2;
this->x = c1*s2*c3 - s1*c2*s3;
this->y = c1_c2*s3 + s1_s2*c3;
this->z = s1*c2*c3 + c1*s2*s3;
this->w = c1_c2*c3 - s1_s2*s3;

Quat to Euler, without Gimbal Lock check:
[CODE]Vec3::Vec3(const Quat& q)
float x2 = q.x*q.x;
float y2 = q.y*q.y;
float z2 = q.z*q.z;
this->y = -Math::ATan2( 2.0*q.y*q.w-2.0*q.z*q.x,1.0-2.0*y2-2.0*x2 );
this->x = -Math::ASin( Math::Clamp(2.0*q.z*q.y+2*q.x*q.w,-1.0,1.0) );
this->z = -Math::ATan2( 2.0*q.z*q.w-2.0*q.y*q.x,1.0-2.0*z2-2.0*x2 );

Can you give me any tips? Thank you. Edited by Josh Klint

Share this post

Link to post
Share on other sites
Well, I ended up just converting the Quaternion to a Mat4, then getting the yaw, when the pitch is high or low. Then I reduced that code to only the lines I needed and put it all in the same function. Problem solved.

Share this post

Link to post
Share on other sites
Sign in to follow this