Quaternion - Rotate To

后端 未结 4 1893
醉话见心
醉话见心 2020-12-17 05:22

I have some object in world space, let\'s say at (0,0,0) and want to rotate it to face (10,10,10).

How do i do this using quaternions?

相关标签:
4条回答
  • 2020-12-17 05:31

    In response to your clarification and to just answer this, I've shamelessly copied a very interesting and neat algorithm for finding the quat between two vectors that looks like I have never seen before from here. Mathematically, it seems valid, and since your question is about the mathematics behind it, I'm sure you'll be able to convert this pseudocode into 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;
    

    Let me know if you need help clarifying any bits of that pseudocode. Should be straightforward though.

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

    and

    cross(a,b) = well, the cross product. it's annoying to type out and
    can be found anywhere.
    
    0 讨论(0)
  • 2020-12-17 05:36

    An example of calculating the Quaternion that represents the rotation between two vectors can be found in the OGRE source code for the Ogre::Vector3 class.

    0 讨论(0)
  • 2020-12-17 05:38

    You may want to use SLERP (Spherical Linear Interpolation). See this article for reference on how to do it in c++

    0 讨论(0)
  • 2020-12-17 05:46

    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.

    0 讨论(0)
提交回复
热议问题