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

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