问题描述
我试图了解四元数旋转是如何工作的,我找到了这个迷你教程 http://www.julapy.com/blog/2008/12/22/quaternion-rotation/ 但他做了一些我无法锻炼的假设,比如我该怎么做计算出围绕每个轴的旋转矢量,只需围绕轴旋转矢量."他如何计算 angleDegreesX、angleDegreesY 和 angleDegreesZ?
I'm trying to understand how the quaternion rotations work, I found this mini tutorial http://www.julapy.com/blog/2008/12/22/quaternion-rotation/ but He makes some assumptions that I can't workout, like how can I do "work out the rotation vectors around each axis, simply by rotating the vector around an axis." and how does he calculate angleDegreesX, angleDegreesY and angleDegreesZ?
谁能提供一个可行的例子或解释?
Can some one provide a working example or explanation?
推荐答案
最简短的总结是四元数只是旋转矩阵的简写.一个 4x4 矩阵需要 16 个单独的值,而一个四元数可以表示 4 中完全相同的旋转.
The shortest possible summary is that a quaternion is just shorthand for a rotation matrix. Whereas a 4x4 matrix requires 16 individual values, a quaternion can represent the exact same rotation in 4.
对于有数学倾向的人,我完全知道上面的内容过于简单化了.
为了提供更多细节,让我们参考维基百科文章:
To provide a little more detail, let's refer to the Wikipedia article:
单位四元数提供了一个方便的表示的数学符号对象的方向和旋转在三个维度.与欧拉相比角度他们更容易组成和避免云台锁问题.与旋转矩阵相比,它们是在数值上更稳定,可能是更高效
Unit quaternions provide a convenient mathematical notation for representing orientations and rotations of objects in three dimensions. Compared to Euler angles they are simpler to compose and avoid the problem of gimbal lock. Compared to rotation matrices they are more numerically stable and may be more efficient
从开头的段落中不清楚的是,四元数不仅方便,而且是独一无二的.如果您有一个对象的特定方向,在任意数量的轴上扭曲,则存在一个表示该方向的唯一四元数.
What isn't clear from that opening paragraph is that a quaternion is not only convenient, it's unique. If you have a particular orientation of an object, twisting on any number of axes, there exists a single unique quaternion that represents that orientation.
再次,对于数学倾向,我上面的唯一性评论假设右手旋转.有一个等价的左手四元数围绕相反的轴向相反的方向旋转.
为了简单的解释,这是有区别的,没有区别的.
如果您想创建一个简单的四元数来表示绕轴旋转,下面是一系列简短的步骤,可以帮助您完成:
If you'd like to make a simple quaternion that represents rotation about an axis, here's a short series of steps that will get you there:
- 选择你的旋转轴
v = {x, y, z}
.出于礼貌,请选择一个单位向量:如果它的长度不是 1,则将所有分量除以 v 的长度. - 选择一个您想绕该轴转动的旋转角度,并将其命名为
theta
. - 可以使用下面的示例代码计算等效单位四元数:
- Pick your axis of rotation
v = {x, y, z}
. Just for politeness, please pick a unit vector: if it's not already of length 1, divide all the components by the length of v. - Pick an angle of rotation that you'd like to turn about this axis and call that
theta
. - The equivalent unit quaternion can be computed using the sample code below:
四元数构造:
q = { cos(theta/2.0), // This is the angle component
sin(theta/2.0) * x, // Remember, angle is in radians, not degrees!
sin(theta/2.0) * y, // These capture the axis of rotation
sin(theta/2.0) * z};
请注意这些除以 2:确保在轮换中没有混淆.对于正常的旋转矩阵,向右旋转 90 度与向左旋转 270 度相同.相当于这两个旋转的四元数是不同的:不能将一个与另一个混淆.
Note those divisions by two: those ensure that there's no confusion in the rotation. With a normal rotation matrix, rotating to the right 90 degrees is the same as rotating to the left by 270. The quaternions that are equivalent to those two rotations are distinct: you can't confuse one with the other.
回答评论中的问题:
让我们通过设置以下参照系来简化问题:
Let's simplify the problem by setting the following frame of reference:
- 选择屏幕中心作为原点(我们将围绕它旋转).
- X 轴指向右侧
- Y 轴指向上方(屏幕顶部)
- Z 轴指向您脸部的屏幕外(形成一个不错的右手坐标系).
因此,如果我们有一个示例对象(例如箭头),它从指向右侧(正 x 轴)开始.如果我们将鼠标从 x 轴向上移动,鼠标将为我们提供正 x 和正 y.因此,完成一系列步骤:
So, if we have an example object (say an arrow) that starts by pointing to the right (positive x axis). If we move the mouse up from the x axis, the mouse will provide us with a positive x and positive y. So, working through the series of steps:
double theta = Math.atan2(y, x);
// Remember, Z axis = {0, 0, 1};
// pseudo code for the quaternion:
q = { cos(theta/2.0), // This is the angle component
sin(theta/2.0) * 0, // As you can see, the zero components are ignored
sin(theta/2.0) * 0, // Left them in for clarity.
sin(theta/2.0) * 1.0};
这篇关于这个四元数旋转代码是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!