PDA

View Full Version : Using gluLookAt() to orbit around a moving object



Jack2606
05-12-2011, 07:25 AM
Hi,

I've just started using gluLookAt() and seem to understand the basics of how it works.. However, i'm struggling to come up with a solution for the following problem using it..

Basically i have a moving sphere that i want the camera to be able to orbit around.. So I tried:

gluLookAt(0, 0, sphereZ + 4, sphereX, sphereY, sphereZ, 0.0, 1.0, 0.0);
glRotatef(angle,0,1,0);

It works perfectly before the sphere moves.. and it will follow the sphere if I do not rotate.. But if i rotate the view slightly it seems to get messed up.. and i'm not sure why.

Basically i want the camera to always be at an equal distance from the sphere in the X and Z (so it orbits around) and i need it to look at the centre of the sphere..

Its probably a very stupid question, but I really can't figure out how to get that to work..

Any help will be appreciated! And please just ask if you need clarification on anything.

Cheers!

Jack2606
05-13-2011, 02:18 PM
Bump!

Any ideas anyone? :(

Aleksandar
05-13-2011, 02:28 PM
Its probably a very stupid question, but I really can't figure out how to get that to work..
Maybe the answer is so trivial that nobody bothers to answer. :)

Well, to do what you want you don't need gluLookAt() function at all.
Just draw sphere at the center of the coordinate system and rotate around it!

You are probably wandering how your ball can move around the scene if it is stationary. :)
The answer is quite easy. You will move the rest of the scene in the opposite direction.

If you still want to use gluLookAt(), than you'll need a little trigonometry to calculate position of the viewer in the world space and set those coordinates as first three parameters of the function.