Quaternion lookat algorithm

Started by
1 comment, last by someusername 18 years ago
Hi all, I'm trying to figure out Quaternions and so was trying to create a camera system that uses them. What I can't figure out is how do a look at type of algorithm. I understand how to do a look at using matrices, but i'm not really sure what to do with the quaternions. I've looked around a bit to try to find something that would help me, but either I couldn't find it, or maybe it just didn't make sense to me. If someone could kind of point me to somewhere that explains the concept or if someone could explain it to me themselves that would be great. Thanks!
Advertisement
Use the look-at parameters to compute a matrix and then convert that to a quaternion. That is the best option.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
It's better -indeed- to use a "look at" matrix and turn it into a quaternion, because it will assure that the model remains in a -as much as possible- upright position. With the direct quaternion approach, you have no control over the other two axes. However, you don't need a reference vector, like you do with "look at" matrices.
If it absolutely imperative that you only use a quaternion for this, you can do the following:
1) Let vDirFrom and vDirTo be the two *unit* vectors that correspond to the directions you wish to align. These must not be colinear.2) Calculate the vector, normal to both of them. "X" denotes the vector cross product operator. Normalize the result, because if the operands tend to align, it will not be a unit vector!vNormal = vDirFrom X vDirToNormalize(vNormal)3) Find the angle of vDirFrom and vDirTo on the plane they form. The "*" denotes the dot product. acos() is the inverse cosine function.float fAngle = acos( vDirFrom*vDirTo )4) Create a quaternion from scratch, that rotates around vNormal by angle fAngleQuaternion Qfloat fHalfAngle = 0.5f*fAngleQ.w = cos( fHalfAngle )Q.x = sin( fHalfAngle )*vNormal.xQ.y = sin( fHalfAngle )*vNormal.yQ.z = sin( fHalfAngle )*vNormal.z

Apply the rotation of Q and you're ready.

This topic is closed to new replies.

Advertisement