Help with lookAt camera

Started by
1 comment, last by Aitorman 11 years, 7 months ago
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:
BCPs6.png

and when it's also almost below, but a bit to the right, I want it to behave like this:
rnn19.png

But instead, i get this:
ECiQP.png

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?
Advertisement
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.
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

This topic is closed to new replies.

Advertisement