View Full Version : Making the camera follow the object

01-15-2004, 10:27 AM
Hi all,

I have an object, in this case a car which moves around the world and I would like the camera do follow the car wherever it goes. I have a function that draws the car at the origin, and a function that draws the camera as if the car is at the origin. Then I in the display func:

glTranslated(car->getPositionX(), car->getPositionY(), car->getPositionZ());

This way the problem is that in the beginning everything is ok since the car is at the origin. But when I move the car, the car goes away and the camera seems to stay where it is, not moving. If I exchange the lines which set the camera position and the translate, it seems the camera moves itself towars the car and enters in it... http://www.opengl.org/discussion_boards/ubb/frown.gif

Any ideas?

Best regards,
Paulo Matos

01-16-2004, 11:30 AM
For applying your camera, just use gluLookAt and inside of it use the following parameters

gluLookAt(CamX, CamY, CamZ, carX, carY, carZ, 0, 1, 0);

Then for the camera position you could just use

CamX = CarX + cos(someangle)*(distance from car)

And the same would go for CamY, and CamZ