Making the camera follow the object

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:

glPushMatrix();
state->setCameraLook();
glTranslated(car->getPositionX(), car->getPositionY(), car->getPositionZ());
car->draw();
glPopMatrix();

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…

Any ideas?

Best regards,
Paulo Matos

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