Reanimation
Reanimation

Reputation: 3336

Rotating Camera according to 3D Object's position

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)

enter image description here

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?

Upvotes: 2

Views: 1285

Answers (1)

redsoxfantom
redsoxfantom

Reputation: 956

Give this a shot, it makes sense in my head but it might not in reality :)

Ok, so we can represent your car's position and velocity as 2-element vectors (in the case of position it is a point, and in the case of velocity it is a true vector.)

Now that we have that, to calculate the position of the camera we can just take the negative of your velocity vector (this means just make all elements of the vector negative, yielding a vector of the same magnitude but exactly opposite direction) and add that vector to your car's position.

For example, say your car is at position (1,1) and your speed vector is (1,2). The negative speed vector would be (-1,-2) and the position of your camera would be (1,1) + (-1,-2) to be (0,-1). You will probably want to normalize the negative speed vector so that your camera stays a constant distance from the car, otherwise the faster you go the further the camera will get :)

Now that you have the camera's position, just call gluLookAt:

gluLookAt(camPos.x,camPos.y,camPos.z,carPos.x,carPos.y,carPos.z,0,1,0);

As you can see, we are passing in the camera's position (which we just calculated in the previous step) and telling the camera to look at the car. You may want to tweak some values (for example, have the camera be floating a few units off the ground, instead of directly behind the car, or maybe have the camera look at a point a few units above the car, etc.)

Let me know how this goes! Again, this is just something I thought might work for you, no guarantees that my math is right though :) But this should be faster and easier to understand than messing around with angles and such. One thing to keep in mind: this will not work if the car is completely stationary (no velocity vector)

Upvotes: 2

Related Questions