4

我在世界空间中有一些对象,假设在 (0,0,0) 并希望将其旋转到面对 (10,10,10)。

我如何使用四元数来做到这一点?

4

4 回答 4

8

This question doesn't quite make sense. You said that you want an object to "face" a specific point, but that doesn't give enough information.

First, what does it mean to face that direction? In OpenGL, it means that the -z axis in the local reference frame is aligned with the specified direction in some external reference frame. In order to make this alignment happen, we need to know what direction the relevant axis of the object is currently "facing".

However, that still doesn't define a unique transformation. Even if you know what direction to make the -z axis point, the object is still free to spin around that axis. This is why the function gluLookAt() requires that you provide an 'at' direction and an 'up' direction.

The next thing that we need to know is what format does the end-result need to be in? The orientation of an object is often stored in quaternion format. However, if you want to graphically rotate the object, then you might need a rotation matrix.

So let's make a few assumptions. I'll assume that your object is centered at the world's point c and has the default alignment. I.e., the object's x, y, and z axes are aligned with the world's x, y, and z axes. This means that the orientation of the object, relative to the world, can be represented as the identity matrix, or the identity quaternion: [1 0 0 0] (using the quaternion convention where w comes first).

If you want the shortest rotation that will align the object's -z axis with point p:=[p.x p.y p.z], then you will rotate by φ around axis a. Now we'll find those values. First we find axis a by normalizing the vector p-c and then taking the cross-product with the unit-length -z vector and then normalizing again:

a = normalize( crossProduct(-z, normalize(p-c) ) );

The shortest angle between those two unit vectors found by taking the inverse cosine of their dot-product:

φ = acos( dotProduct(-z, normalize(p-c) ));

Unfortunately, this is a measure of the absolute value of the angle formed by the two vectors. We need to figure out if it's positive or negative when rotating around a. There must be a more elegant way, but the first way that comes to mind is to find a third axis, perpendicular to both a and -z and then take the sign from its dot-product with our target axis. Vis:

b = crossProduct(a, -z );

if ( dotProduct(b, normalize(p-c) )<0 ) φ = -φ;

Once we have our axis and angle, turning it into a quaternion is easy:

q = [cos(φ/2) sin(φ/2)a];

This new quaternion represents the new orientation of the object. It can be converted into a matrix for rendering purposes, or you can use it to directly rotate the object's vertices, if desired, using the rules of quaternion multiplication.

于 2012-10-22T17:56:38.653 回答
1

在Ogre::Vector3 类的 OGRE 源代码中可以找到计算表示两个向量之间旋转的四元数的示例。

于 2012-10-22T15:58:15.457 回答
1

为了回应您的澄清并回答这个问题,我无耻地复制了一个非常有趣且简洁的算法来查找两个向量之间的 quat,这看起来就像我以前从未见过的一样。从数学上讲,这似乎是有效的,而且由于您的问题是关于其背后的数学,我相信您将能够将此伪代码转换为 C++。

quaternion q;
vector3 c = cross(v1,v2);
q.v = c;
if ( vectors are known to be unit length ) {
    q.w = 1 + dot(v1,v2);
} else {
    q.w = sqrt(v1.length_squared() * v2.length_squared()) + dot(v1,v2);
}
q.normalize();
return q;

如果您需要帮助澄清该伪代码的任何部分,请告诉我。不过应该直截了当。

dot(a,b) = a1*b1 + a2*b2 + ... + an*bn

cross(a,b) = well, the cross product. it's annoying to type out and
can be found anywhere.
于 2012-10-22T16:40:18.953 回答
0

您可能想要使用 SLERP(球面线性插值)。请参阅本文以获取有关如何在 C++ 中执行此操作的参考

于 2012-10-22T15:52:51.423 回答