I'm writing a program in C++ using OpenGL and GLM; I'm trying to get my camera to follow my car. So far it successfully follows it around the screen, but I'm having trouble adjusting the Camera's origin (position) to make it stay directly behind the car when it turns. (when the car turns, I want the camera to turn as well to stay inline with it).

After doing a little googling and S/Oing, I think the way forward is by using Geometric Slerp, who's equation is seen here.

It involves the following equation and relates to this diagram:

enter image description hereenter image description here

Below is what I'm trying to achieve; in a program, I have a camera following a car and when the car turns, the cameras position needs to update to stay behind it. Below are two doodles to help you understand my description above. The first image shows the car and camera; the second shows the details:

Car Turning, Camera FollowingDetails

I have tried writing a function for the equation as seen below:

vec2 slerp = (((sin((1-t)*Omega))/(sin(Omega)))*p0)+(((sin(t*Omega))/(sin(Omega)))*p1)

But I'm unsure how to calculate P1... I'm assuming P0 is the current position and Omega is the rotation of the car. (so by turning left, the camera rotates from P0 to P1.

This is how my car position is updated and how I've got my camera following my object:

camOrigin.x  = (0   +globalPos.x+velocity.x); //Cam Position
camOrigin.y  = (0   +globalPos.y+velocity.y); //Assuming I need to *slerp to X and Z??
camOrigin.z  = (100 +globalPos.z+velocity.z); //stays 100 behind car on Z plane
camLookingAt.x = (0 +globalPos.x+velocity.x); //Cam Looking at Object
camLookingAt.y = (0 +globalPos.y+velocity.y);
camLookingAt.z = (0 +globalPos.z+velocity.z);
camNormalXYZ.x = (0);                         //Cam Normal = Y
camNormalXYZ.y = (1);
camNormalXYZ.z = (0);

Or maybe I'm looking at this wrong and there's an easier way to do this?

  • \$\begingroup\$ Seems like you don't need to use slerp at all. The easiest way to achieve what you want is to construct camera origin vector from car's forward vector. Just inverse it, multiply by scalar (100 units) and add a vector pointing up (eventualy; for elevation). You can use it then to calculate actual camera's origin coordinates. Just add camera's origin vector to car's coordinates. \$\endgroup\$ – Helbreder Dec 9 '13 at 18:23
  • \$\begingroup\$ @Reanimation Do you just want to put the camera behind the car or to make the camera smoothly follow the car rotation after some time delay? The first option will make the camera move mechanically and will lessen the immersion. If you see videos of car games you will see that the camera has some delay and does not match the car rotation while in the curve. \$\endgroup\$ – dsilva.vinicius Dec 9 '13 at 18:50
  • \$\begingroup\$ @dsilva.vinicius, yes, I'm hoping it to maintain it's position behind the car. So as it goes around a corner, the camera makes the same rotation but maintains it's distance. \$\endgroup\$ – Reanimation Dec 9 '13 at 19:07

Based on your sketch, you can simply rotate P0 by Omega to become P1. The problem is finding the rotation axis which is trivial in your case: the up vector of your camera. Keep in mind that rotation happens around the origin(0,0,0) by default so might need to translate, rotate and translate back depending on the space you're working in.

So what you have to do is:

  1. Calculate P1 by rotating P0 around the camera up axis by Omega.
  2. Now you have P1 you can interpolate between the two values.

I also want to stress the fact that interpolation is usually used for animation. In other words generating intermediate values. So you first calculate start and end points then generate the intermediate values.

The second option, is to use vector math, which might be more generic and a little bit more more complex. It's might also be slower (performance wise) which is not worth the trouble.


you actually just want to rotate your p0 by rotation_speed*deltaTime

or in rotation notation:

P' = P0*Axis_angle(axis,rotation_speed*deltaTime);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.