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