# Thread: Using gluLookAt() to orbit around a moving object

1. ## Using gluLookAt() to orbit around a moving object

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!

2. ## Re: Using gluLookAt() to orbit around a moving object

Bump!

Any ideas anyone?

3. ## Re: Using gluLookAt() to orbit around a moving object

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

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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•