旋转的四元数数学?
我正在场景中使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该解决方案应该非常简单,并且不需要四元数。
从 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.绕任意轴旋转:给定以弧度表示的角度 r 和单位向量 u = ai + bj + ck 或 [a,b,c],定义:
并根据这些值构造旋转矩阵:
要找到需要执行的旋转,您可以计算当前向量和目标向量之间的叉积。 您将获得正交向量(这将是创建四元数的旋转向量),该向量的长度是您必须补偿的角度的正弦,以便起始向量和目标向量重叠。
Rotation around an arbitrary axis: Given angle r in radians and unit vector u = ai + bj + ck or [a,b,c], define:
and construct from these values the rotation matrix:
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.
四元数描述了绕轴的旋转。
将围绕轴
旋转一定量,具体取决于大小之间的平衡w
和向量的大小。例如,将其旋转为面向正 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)
。 四元数变为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 ofw
and the magnitude of the vector.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 bearctan(sqrt(x^2+y^2),z)
. The quaternion becomes