• Create Account

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

1 reply to this topic

### #1Josh Klint  Members

1439
Like
0Likes
Like

Posted 11 October 2012 - 12:54 AM

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:
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:
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, 11 October 2012 - 12:54 AM.

Build mobile games with native code

### #2Josh Klint  Members

1439
Like
1Likes
Like

Posted 11 October 2012 - 02:19 AM

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.

Build mobile games with native code

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.