如何在不使用 transform.Rotate 的情况下在局部轴或世界轴上旋转带有第二四元数的四元数? [英] How do I rotate a Quaternion with 2nd Quaternion on its local or world axes without using transform.Rotate?

查看:15
本文介绍了如何在不使用 transform.Rotate 的情况下在局部轴或世界轴上旋转带有第二四元数的四元数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Transform.Rotate 有一个非常用于选择是否应相对于世界轴或局部轴进行旋转的有用界面.

Transform.Rotate has a very helpful interface for selecting whether or not a rotation should be made relative to world axes or the local axes.

在幕后,它正在对变换的rotation、一个Quaternion 和另一个Quaternion 进行一些数学运算.这个数学的确切性质取决于你选择当地国旗还是世界国旗.

Behind the scenes, it's doing some math to the transform's rotation, a Quaternion, with another Quaternion. And the exact nature of this math changes depending on if you choose the local or world flag.

如何在不将第一个 Quaternion 分配给 Transform 的 rotation(浪费内存和/或时间)的情况下进行这种数学运算,只是为了用它做一些数学运算?

How can I do this sort of math without assigning the first Quaternion to a Transform's rotation (wasting memory and/or time) just to do some math with it?

假设我的第一个 QuaternionQuaternion q1 = Quaternion.Euler(0f,0f,90f); 而第二个是 Quaternion q2 = Quaternion.Euler(90f,0f,0f).

Suppose my first Quaternion is Quaternion q1 = Quaternion.Euler(0f,0f,90f); and the second is Quaternion q2 = Quaternion.Euler(90f,0f,0f).

沿其局部轴将第二个应用于第一个将产生一个旋转,将其右侧旋转为面朝下,将其正面旋转为右面.

Applying the second to the first along its local axes would give a rotation that rotates its right side to face down and its front to face right.

沿原始轴将第二个应用于第一个将产生一个旋转,使其背面朝下旋转,顶部朝右旋转.

Applying the second to the first along the original axes would give a rotation that rotates its back to face down and its top to face right.

推荐答案

使用 <代码>四元数运算符 *.

在 Unity 中,当您将两个四元数相乘时,它会将第二个四元数(以全局轴表示)沿第一个 四元数 之后产生的局部轴应用于第一个四元数.

Use the Quaternion operator *.

In Unity, when you multiply two quaternions, it applies the second quaternion (expressed in global axes) to the first quaternion along the local axes produced after the first Quaternion).

所以,如果你想做 Rotate(q2, Space.Self) 的等价物,其中 q1 是变换的原始 rotation,而 q3 是变换的新 rotation,你需要 Quaternion q3 = q1 * q2.

So, if you want to do the equivalent of Rotate(q2, Space.Self), where q1 is the transform's original rotation, and q3 is the transform's new rotation, you want Quaternion q3 = q1 * q2.

如果您想在应用 q1 之前(即沿全局轴)沿轴将 q2 应用到 q1,您会怎么做?好吧,事实证明这等效于沿 q2 的局部轴应用 q1.

What do you do if you want to apply q2 to q1 along the axes before q1 applies (i.e., along the global axes)? Well, it turns out that is equivalent to applying q1 along the local axes of q2.

例如,沿 q1 的全局轴应用 q2 会使其背面朝下旋转,顶部朝右旋转.考虑到沿 p2 的局部轴应用 q1 也会使其背面朝下旋转,顶部朝右旋转.它们导致相同的方向!

As an example, applying q2 along the global axes of q1 rotates its back to face down and its top to face right. Consider that that applying q1 along the local axes of p2 also rotates its back to face down and its top to face right. They result in the same orientation!

那是什么意思?如果你想在 q1 应用之前沿轴应用 q2q1,然后做 Quaternion q3 = q2 * q1.

So what does that mean? If you want to apply q2 to q1 along the axes before q1 applies, then do Quaternion q3 = q2 * q1.

这相当于执行 Rotate(q2, Space.World) 其中 q1 是变换的原始 rotation,并且q3 是新的 rotation.

This is the equivalent of doing Rotate(q2, Space.World) where q1 is the transform's original rotation, and q3 is the new rotation.

Rotate 不同的是,您可以使用此操作符相对于其父轴旋转对象!如果您执行 transform.localRotation = q2 * transform.localRotation,您将相对于父对象的轴旋转对象.

Unlike Rotate, you can use this operator to rotate an object relative to its parent's axes! If you do transform.localRotation = q2 * transform.localRotation, you will be rotating the object relative to the axis of the parent.

这篇关于如何在不使用 transform.Rotate 的情况下在局部轴或世界轴上旋转带有第二四元数的四元数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆