【问题标题】:How does quaternions rotation matrix of Unity calculate?Unity的四元数旋转矩阵是如何计算的?
【发布时间】:2019-06-13 16:31:27
【问题描述】:

我不知道如何在 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

【问题讨论】:

  • 只是一个猜测,但我想一般旋转是针对直接(右手)轴系统进行的,而统一是间接(左手)轴系统。这应该可以解释 - 符号。
  • 你是对的!!
  • 转置右手方向的旋转矩阵,我解决了问题。谢谢!!!!!!!!!!!!!!!!!!
  • 没问题:) -
  • (我会把这个作为答案发布)

标签: unity3d quaternions


【解决方案1】:

一般旋转适用于直接(右手)轴系统,而统一是间接(左手)轴系统。这应该可以解释 - 符号,以及你得到的区别。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-06
    • 2021-07-10
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多