这个四元数旋转代码如何工作? [英] How does this Quaternion rotation code work?

查看:136
本文介绍了这个四元数旋转代码如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解四元数旋转是如何工作的,我找到了这个迷你教程 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:


单位四元数为代表$ b提供方便的
数学符号$ b b方向和旋转对象
三维。与Euler
角度相比,它们更容易构成,
避免了万向节锁定的问题。
与旋转矩阵相比,它们是
更稳定,可能更有效

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:


  1. 选择你的旋转轴 v = {x,y,z} 。只是为了礼貌,请选择一个单位矢量:如果它不是长度为1,则将所有分量除以v的长度。

  2. 选择你想要的旋转角度转过这个轴并调用 theta

  3. 可以使用下面的示例代码计算等效单位四元数:

  1. 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.
  2. Pick an angle of rotation that you'd like to turn about this axis and call that theta.
  3. 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};

请注意这两个除法:它们确保轮换中没有混淆。使用正常旋转矩阵,向右旋转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.

编辑:回复评论中的问题:

responding to the question in the comments:

让我们通过设置以下参考框架来简化问题:

Let's simplify the problem by setting the following frame of reference:


  1. 选择屏幕中心作为原点(我们将围绕它旋转)。

  2. X轴指向右侧

  3. Y轴向上(屏幕顶部)

  4. Z轴指向屏幕外的脸部(形成)一个很好的右手坐标系。)。

  1. Pick the center of the screen as the origin (we're going to rotate around that).
  2. X axis points to the right
  3. Y axis points up (top of the screen)
  4. Z axis points out of the screen at your face (forming a nice right handed coordinate system).

所以,如果我们有一个示例对象(比如箭头),首先指向右(正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};

这篇关于这个四元数旋转代码如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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