Jump to content
  • Advertisement
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post

Link to post
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 vDirTo

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 fAngle
Quaternion Q
float fHalfAngle = 0.5f*fAngle
Q.w = cos( fHalfAngle )
Q.x = sin( fHalfAngle )*vNormal.x
Q.y = sin( fHalfAngle )*vNormal.y
Q.z = sin( fHalfAngle )*vNormal.z

Apply the rotation of Q and you're ready.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!