//https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles public static FixVec3 ToEuler(FixQuaternion q) { FixVec3 result; Fix t0 = 2 * (q.w * q.z + q.x * q.y); Fix t1 = Fix.One - (2 * (q.y * q.y + q.z * q.z)); result._z = FixMath.Atan2(t0, t1); Fix t2 = 2 * (q.w * q.y - q.z * q.x); if (t2 >= Fix.One) { result._y = FixMath.PI / 2; } else if (t2 <= -Fix.One) { result._y = -(FixMath.PI / 2); } else { result._y = FixMath.Asin(t2); } Fix t3 = 2 * (q.w * q.x + q.y * q.z); Fix t4 = Fix.One - (2 * (q.x * q.x + q.y * q.y)); result._x = FixMath.Atan2(t3, t4); return(result); }
//https://math.stackexchange.com/questions/2975109/how-to-convert-euler-angles-to-quaternions-and-get-the-same-euler-angles-back-fr public static FixVec3 ToEuler(FixQuaternion q) { FixVec3 result; Fix t0 = 2 * (q.w * q.z + q.x * q.y); Fix t1 = 1 - 2 * (q.y * q.y + q.z * q.z); result.z = FixMath.Atan2(t0, t1); Fix t2 = 2 * (q.w * q.y - q.z * q.x); if (FixMath.Abs(t2) >= Fix.one) { result.y = FixMath.PI / 2; //t2 = 1; } else if (t2 <= -Fix.one) { result.y = -(FixMath.PI / 2); //t2 = -1; } else { result.y = FixMath.Asin(t2); } Fix t3 = 2 * (q.w * q.x + q.y * q.z); Fix t4 = 1 - 2 * (q.x * q.x + q.y * q.y); result.x = FixMath.Atan2(t3, t4); return(result); }
//https://gamedev.stackexchange.com/questions/50963/how-to-extract-euler-angles-from-transformation-matrix public FixVec3 EulerAngle() { FixVec3 ea = new FixVec3(); ea.x = FixMath.Atan2(-m[1, 2], m[2, 2]); Fix cosYangle = FixMath.Sqrt(FixMath.Pow(m[0, 0], 2) + FixMath.Pow(m[0, 1], 2)); ea.y = FixMath.Atan2(m[0, 2], cosYangle); Fix sinXangle = FixMath.Sin(ea.x); Fix cosXangle = FixMath.Cos(ea.x); ea.z = FixMath.Atan2((cosXangle * m[1, 0]) + (sinXangle * m[2, 0]), (cosXangle * m[1, 1]) + (sinXangle * m[2, 1])); return(ea); }