这两天看MDL模型的显示代码,参考《3D数学基础:图形与游戏开发》中的公式,发现欧拉角转成四元数的公式和书中提到的公式不一致,狂晕!~~~
void AngleQuaternion(const vec3_t Angles, vec4_t Quaternion)
{ float Angle = Angles[0] * 0.5f; float Sin0 = (float)sin(Angle); float Cos0 = (float)cos(Angle); Angle = Angles[1] * 0.5f; float Sin1 = (float)sin(Angle); float Cos1 = (float)cos(Angle); Angle = Angles[2] * 0.5f; float Sin2 = (float)sin(Angle); float Cos2 = (float)cos(Angle); Quaternion[0] = Sin0 * Cos1 * Cos2 - Cos0 * Sin1 * Sin2; Quaternion[1] = Cos0 * Sin1 * Cos2 + Sin0 * Cos1 * Sin2; Quaternion[2] = Cos0 * Cos1 * Sin2 - Sin0 * Sin1 * Cos2; Quaternion[3] = Cos0 * Cos1 * Cos2 + Sin0 * Sin1 * Sin2; }
|cos(p/2) | |cos(b/2) | Euler to QuaternionConverting from Euler angles to a quaternion is slightly more tricky, as the order of operations must be correct. Since you can convert the Euler angles to three independent quaternions by setting the arbitrary axis to the coordinate axes, you can then multiply the three quaternions together to obtain the final quaternion. So if you have three Euler angles (a, b, c), then you can form three independent quaternions
And the final quaternion is obtained by Qx * Qy * Qz. |
|