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