旋转的四元数数学?

发布于 2024-07-17 12:19:29 字数 160 浏览 7 评论 0原文

我正在场景中使用 gluDisk() 绘制一个扁平磁盘。 gluDisk() 绘制面向正 Z 轴的磁盘,但我希望它面向我拥有的任意法线。
显然我需要使用 glRotate() 来使磁盘正确朝向,但旋转应该是多少? 我记得这可以使用四元数来计算,但我似乎不记得数学了。

I'm drawing a flat disk using gluDisk() in my scene. gluDisk() draws the disk facing the positive Z axis but I want it to be facing some arbitrary normal I have.
Clearly I need to use glRotate() to get the disk facing properly but what should be the rotation? I remember this can be calculated using Quaternions but I can't seem to remember the math.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷你仙 2024-07-24 12:19:29

该解决方案应该非常简单,并且不需要四元数。

从 Normal1 到 Normal2 的旋转轴必须与两者正交,因此只需取它们的向量叉积即可。

旋转量很容易从它们的点积中得出。 这个值是|A|.|B|.cos(theta),但是由于两个法向量需要归一化,所以会得到cos(theta),所以只需取反余弦即可得到旋转量。

生成的向量和角度是 glRotate() 所需的参数 - 无需自己计算实际的旋转矩阵。

ps 不要忘记 glRotate() 需要以度为单位的角度,但普通的 C 三角函数以弧度为单位。

The solution should be pretty straightforward, and shouldn't require quarternions.

The axis of rotation to get from Normal1 to Normal2 must be orthogonal to both, so just take their vector cross-product.

The amount of rotation is easily derived from their dot-product. This value is |A|.|B|.cos(theta), but as the two normal vectors should be normalised it will give cos(theta), so just take the inverse cosine to get the rotation amount.

The resulting vector and angle are the required parameters for glRotate() - there's no need to calculate the actual rotation matrix yourself.

p.s. don't forget that glRotate() needs the angle in degrees, but the normal C trig functions work in radians.

终弃我 2024-07-24 12:19:29

绕任意轴旋转:给定以弧度表示的角度 r 和单位向量 u = ai + bj + ck 或 [a,b,c],定义:

q0 = cos(r/2)  
q1 = sin(r/2) a   
q2 = sin(r/2) b  
q3 = sin(r/2) c  

并根据这些值构造旋转矩阵:

   ( q0^2+q1^2 - q2^2 - q3^2 | 2*(q1*q2 - q0*q3)           | 2*(q1*q3 + q0*q2)         )
Q =( 2*(q2*q1 + q0*q3)       | (q0^2 - q1^2 + q2^2 - q3^2) | 2*(q2*q3 - q0*q1)         )
   ( 2*(q3*q1 - q0*q2)       | 2*(q3*q2 + q0*q1)           | q0^2 - q1^2 - q2^2 + q3^2 )

要找到需要执行的旋转,您可以计算当前向量和目标向量之间的叉积。 您将获得正交向量(这将是创建四元数的旋转向量),该向量的长度是您必须补偿的角度的正弦,以便起始向量和目标向量重叠。

Rotation around an arbitrary axis: Given angle r in radians and unit vector u = ai + bj + ck or [a,b,c], define:

q0 = cos(r/2)  
q1 = sin(r/2) a   
q2 = sin(r/2) b  
q3 = sin(r/2) c  

and construct from these values the rotation matrix:

   ( q0^2+q1^2 - q2^2 - q3^2 | 2*(q1*q2 - q0*q3)           | 2*(q1*q3 + q0*q2)         )
Q =( 2*(q2*q1 + q0*q3)       | (q0^2 - q1^2 + q2^2 - q3^2) | 2*(q2*q3 - q0*q1)         )
   ( 2*(q3*q1 - q0*q2)       | 2*(q3*q2 + q0*q1)           | q0^2 - q1^2 - q2^2 + q3^2 )

To find the rotation you need to do, you can calculate the cross product between the current vector and the target vector. You will obtain the orthogonal vector (which will be your rotation vector to create the quaternion) and the length of this vector is the sin of the angle you have to compensate so that the start and target vector overlap.

幻想少年梦 2024-07-24 12:19:29

四元数描述了绕轴的旋转。 将围绕轴 旋转一定量,具体取决于 大小之间的平衡w 和向量的大小。

<cos θ/2, x*sin θ/2, y*sin θ/2, z*sin θ/2>, where |<x, y, z>| = 1

例如,将其旋转为面向正 Y 轴,则需要将其绕 X 轴旋转 90°。 向量将为 <0, 1, 0>,四元数将为 = <0,0,1,0>

要将图形从面向正 Z 轴旋转到面向向量 ,您需要找到旋转向量和旋转角度。 要找到旋转轴,您可以采用当前向量及其所需位置的叉积。

如果它面向正 Z 轴,则当前矢量将为 <0, 0, 1>。 如果您希望它面向 ,则旋转轴将为 <0, 0, 1> x<x,y,z> = <-y, x, 0>,角度将为 arctan(sqrt(x^2+y^2),z)。 四元数变为

<cos(θ/2), -y*sin(θ/2), x*sin(θ/2), 0>, where θ = arctan(sqrt(x^2+y^2), z)

Quaternions describe a rotation about an axis. <w,x,y,z> will rotate around the axis <x,y,z> some amount depending on the balance between the magnitude of w and the magnitude of the vector.

<cos θ/2, x*sin θ/2, y*sin θ/2, z*sin θ/2>, where |<x, y, z>| = 1

For example, rotating it to face the positive Y-axis instead, you need to rotate it 90° around the X-axis. The vector would be <0, 1, 0>, and the quaternion would be <cos 90°, 0, sin 90°, 0> = <0, 0, 1, 0>.

To rotate the figure from facing the positive Z-axis, to facing the vector <x,y,z> you need to find the rotation-vector and angle of rotation. To find the rotation axis, you can take the cross-product of a current vector, and where you want it to be.

If it is facing the positive Z-axis, the current vector would be <0, 0, 1>. If you want it to face <x,y,z>, the rotation-axis would be <0, 0, 1> x <x, y, z> = <-y, x, 0>, and the angle would be arctan(sqrt(x^2+y^2),z). The quaternion becomes

<cos(θ/2), -y*sin(θ/2), x*sin(θ/2), 0>, where θ = arctan(sqrt(x^2+y^2), z)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文