Quaternion - Rotate To

后端 未结 4 1955
醉话见心
醉话见心 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.
    

提交回复
热议问题