How can I get the camera to follow a moving object from behind in C++ and openGL [closed]
- by user1324894
I am trying to get the camera to follow an object that moves around my environment using the gluLookAt function. This is my code for the object moving in the direction that it faces:
Xtri += -Vtri*cos((90+heading)*(PI/180.0f));
Ztri += Vtri*sin((90+heading)*(PI/180.0f));
I then render the object:
glPushMatrix();
glTranslatef(Xtri,0,Ztri);
glRotatef(heading,0,1,0);
drawTriangle();
glPopMatrix();
All heading is is a spin variable so that if I press left or right it spins in that direction. When you press up on the arrows it moves forward and if you press down it moves backwards in the direction that it is facing.
To try and get it so the camera follows I am using the gluLookAt function like this:
gluLookAt(Xtri,0,(Ztri+20), Xtri,0,Ztri, 0,1,0);
So that it follows the car from a distance and should follow it around. However, the object doesn't even move at all now all it can do is rotate still but not move forwards or backwards and when it spins it doesn't follow the spin instead it just watches it turn still fixed to the same position. Where is it that I am going wrong?
UPDATE:
I have updated the gluLookAt function so now it is:
gluLookAt((Xtri+Vtri),0,((Ztri+20)), (Xtri+Vtri),0,(Ztri), 0,1,0);
This seems to move the object around. I have a stationary terrain so I can see that the object is now moving and in the direction that it is facing. However, I want the camera to follow the object when it spins as well so it is always viewing the object from behind.