title: Rotating Camera according to 3D Object's position
body: <p>I'm using Opengl as the foundations for a small car simulation.</p>
  
  <p>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.</p>
  
  <p>eg if the car turns left, the camera rotates to follow it but doesn't move to directly behind the car.</p>
  
  <p>I've looked at these questions <a href="https://stackoverflow.com/questions/6171092/how-to-make-camera-follow-a-3d-object-in-opengl?rq=1">here</a> and <a href="https://stackoverflow.com/questions/11026298/make-an-object-follow-the-camera-in-opengl">here</a> but still can't seem to get it working.</p>
  
  <p>This is how I calculate the position of the car (it just drives on the XZ plane for now):</p>
  
  <pre><code>    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);
  </code></pre>
  
  <p>And this is currently how I have my lookAt function:</p>
  
  <pre><code>    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);
  </code></pre>
  
  <p>I would like the camera to be like this: (where Y is Z)</p>
  
  <p><img src="https://i.stack.imgur.com/dMdcf.png" alt="enter image description here"></p>
  
  <p>The way I see it, it should be:</p>
  
  <pre><code>    camX + globalPos.x+ sin(rotX_rad)*(distFromCar),
      camY,
      camZ + globalPos.z- cos(rotX_rad)*(distFromCar),
  </code></pre>
  
  <p>but it behaves strangely....</p>
  
  <p>What am I not doing?</p>