# Quaternion lookat algorithm

This topic is 4583 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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!

##### Share on other sites
Use the look-at parameters to compute a matrix and then convert that to a quaternion. That is the best option.

##### Share on other sites
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.

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 104
• 11
• ### Forum Statistics

• Total Topics
632977
• Total Posts
3009679
• ### Who's Online (See full list)

There are no registered users currently online

×