I'm using Opengl as the foundations for a small car simulation.
I've imported a model car and it drives around fine and the camera follows it. Unfortunately I can't seem to get the camera's position to move (or stay behind the car) when the car turns. It just rotates on the spot.
eg if the car turns left, the camera rotates to follow it but doesn't move to directly behind the car.
I've looked at these questions here and here but still can't seem to get it working.
This is how I calculate the position of the car (it just drives on the XZ plane for now):
velocity.x = sin(rotX_rad);
velocity.z = cos(rotX_rad);
globalPos.x + velocity.x*delta;
globalPos.z + velocity.z*delta;
glTranslatef(globalPos.x,globalPos.y,globalPos.z);
glRotatef(rotX, 0.0, 1.0, 0.0);
And this is currently how I have my lookAt function:
gluLookAt(camX+globalPos.x,
camY+globalPos.y,
camZ+globalPos.z,
lookX+globalPos.x+sin(rotX_rad),
lookY+globalPos.y+cos(rotX_rad),
lookZ+globalPos.z-cos(rotX_rad),
camNX, camNY, camNZ);
I would like the camera to be like this: (where Y is Z)
The way I see it, it should be:
camX + globalPos.x+ sin(rotX_rad)*(distFromCar),
camY,
camZ + globalPos.z- cos(rotX_rad)*(distFromCar),
but it behaves strangely....
What am I not doing?