Followers 0

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

## 1 post in this topic

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;
}[/CODE]

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 );
}[/CODE]

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

##### 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.
1

## Create an account

Register a new account

Followers 0

• 9
• 10
• 20
• 11
• 28