Sign in to follow this  
Aitorman

Help with lookAt camera

Recommended Posts

Hello there.
I'm trying to make a camera in OpenGL wich follows an object, and i have implemented it like this:
The camera stores a vector wich represent an "offset" from the object to the camera. For example, if that vector is (0,0,-5), the camera will be 5 units behind the object.
Then I assign the object's rot to the camera's rot (all rotations are in quaternions), then I rotate the offset vector with the object's rotation, and I add the rotated offset to the camera's pos. That works fine for the camera position relative to the object. For the rotation, I use a lookAt function that takes the origin as the camera's pos, and the inverse of the rotated offset vector as the target object. This works fine too, except for one little problem:
when the camera is almost exactly below (or above) the object, but a little to the left, it is like this:
[img]http://i.imgur.com/BCPs6.png[/img]

and when it's also almost below, but a bit to the right, I want it to behave like this:
[img]http://i.imgur.com/rnn19.png[/img]

But instead, i get this:
[img]http://i.imgur.com/ECiQP.png[/img]

And I can't solve this problem, I've thought of adding 180 degrees of rotation in the cameras look vector when it's needed (in the second case), but that generates a problem In other situtations, when the object is rotated in a way that requires the camera to behave in the third image's way. What solution do you think is better to solve this problem?

Share this post


Link to post
Share on other sites
Hi, this is mostly the problem with LookAt camera matrix, the roll angle is unpredictable along one axis, usualy vertically, depending on what you use as an up_vector.
If you want to have the camra to be fixed with regard to the object, use the object's rotation for the camera too.

Share this post


Link to post
Share on other sites
Yeah, I've tried that, but the object's rotation only works for the camera if the offset is for example (0,0,5), but if it is (0,5,5), it doesn't work because It has to point 45 degrees for on the x axis than the object. Anyways, I have changed to another camera system. When you specify the offset vector, you also have to specify an offset rotation quaternion, and the camera's rotation will be the object's rotation multiplied by the offset rotation. So, if you have this offset vector (0,5,5), the offset rotation will have to rotate the object 45 degrees along the x axis to have the camera pointing to the object always

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this