Unity四元数旋转矩阵如何计算?

时间:2019-06-12 08:15:20

标签: unity3d quaternions

我不知道如何在Unity中根据四元数计算旋转矩阵。

通用旋转矩阵的计算如下:

http://www.utdallas.edu/~sxb027100/dock/quaternion.html

下面是wxmaxima的代码。

l: matrix([w, z, -y, x], [-z, w, x, y], [y, -x, w, z], [-x, -y, -z, w]);
r: matrix([w, z, -y, -x], [-z, w, x, -y], [y, -x, w, -z], [x, y, z, w]);
lr:  l . r;

着眼于通用旋转矩阵的z轴(0,0,1),它是以下公式。

2xz-2wy
2yz+2wx
w^2-x^2-y^2+z^2=1-2x^2-2y^2

在Unity中,是以下公式。

2xz+2wy
2yz-2wx
1-2x^2+2y^2

上述区别是什么?

下面是Unity中的参考代码。

public static Vector3 operator *(Quaternion rotation, Vector3 point)
{
  float num1 = rotation.x * 2f;
  float num2 = rotation.y * 2f;
  float num3 = rotation.z * 2f;
  float num4 = rotation.x * num1;
  float num5 = rotation.y * num2;
  float num6 = rotation.z * num3;
  float num7 = rotation.x * num2;
  float num8 = rotation.x * num3;
  float num9 = rotation.y * num3;
  float num10 = rotation.w * num1;
  float num11 = rotation.w * num2;
  float num12 = rotation.w * num3;
  Vector3 vector3;
  vector3.x = (float) ((1.0 - ((double) num5 + (double) num6)) * (double) point.x + ((double) num7 - (double) num12) * (double) point.y + ((double) num8 + (double) num11) * (double) point.z);
  vector3.y = (float) (((double) num7 + (double) num12) * (double) point.x + (1.0 - ((double) num4 + (double) num6)) * (double) point.y + ((double) num9 - (double) num10) * (double) point.z);
  vector3.z = (float) (((double) num8 - (double) num11) * (double) point.x + ((double) num9 + (double) num10) * (double) point.y + (1.0 - ((double) num4 + (double) num5)) * (double) point.z);
  return vector3;
}

https://github.com/jamesjlinden/unity-decompiled/blob/96fb16e2eb6fff1acf3d4e25fa713defb3d17999/UnityEngine/UnityEngine/Quaternion.cs

1 个答案:

答案 0 :(得分:0)

对直接(右侧)轴系统进行通用旋转,而单位为间接(左侧)轴系统。那应该解释-号以及您得到的区别。