5

i'm making a car race for the first time using opengl,the first problem i face is how to make the camera follow the car with constant distance..here is the code for keyboard function.V is the velocity of the car.

void OnSpecial(int key, int x, int y) 
{
    float step = 5;

    switch(key) {

    case GLUT_KEY_LEFTa:
        carAngle = step;
        V.z = carAngle ;
        camera.Strafe(-step/2);
        break;

    case GLUT_KEY_RIGHT:
        carAngle = -step;
        V.z = carAngle ;
        camera.Strafe(step/2);
        break;

    case GLUT_KEY_UP:
        V.x += (-step);
        camera.Walk(step/2);

        break;
    case GLUT_KEY_DOWN:
        if(V.x<0)
        {
            V.x += step;
            camera.Walk(-step/2);
        }
        break;
    }
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
memo
  • 59
  • 1
  • 1
  • 2
  • 1
    Can you provide more details: how the car position is defined, what camera behavior do you want? Generally, you need to take the car's position and probably "look" direction, then calculate camera position from it. – n0rd May 30 '11 at 12:42
  • 1
    The rendering code would be more helpful to offer a meaningful answer. Also, could you describe what is currently happening? Is the camera fixed while the car speeds away? Or have you not gotten that far? – Kian May 30 '11 at 13:37
  • my code consists of display function which contain transformations function ,update function and a render scene function which draws m2d car and track..i used the code down after and before render it didn't work also .... about the car position i have a vector in which i can change in onSpecialkey function.with my code the car accelerate so at first the car is slower than the camera and then the camera become slower and the car vanishes.i need the camera to be in constant distance with the car. Any suggestions ? – memo Jun 01 '11 at 15:13

2 Answers2

5

Something like that maybe ?

vec3 cameraPosition = carPosition + vec3(20*cos(carAngle), 10,20*sin(carAngle));
vec3 cameraTarget = carPosition;
vec3 cameraUp = vec3(0,1,0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity()
gluLookAt(cameraPosition, cameraTarget, cameraUp);
glTranslate(carPosition);
drawCar();

It you're not using the old and deprecated openGL API (glBegin & stuff) you'll have to do something like

mat4 ViewMatrix = LookAt(cameraPosition, cameraTarget, cameraUp); // adapt depending on what math library you use
Calvin1602
  • 9,413
  • 2
  • 44
  • 55
0

The answer to that is simple. You have player controlled object (car) so you have its position and orientation via ModelViewMatrix in world space (usually pointed to the center of 3D model) To transform it to the correct follow ModelViewMatrix you must:

  1. obtain or construct car ModelMatrix as double M[16]

  2. translate/rotate it to the new position (inside cockpit or behind car)

    so the Z axis is pointing the way you want to see. Its usual to have the follow distance as a function of speed

  3. Invert M so M=Inverse(M)

  4. use M as ModelViewMatrix

  5. render

so in an nutshell:

ModelViewMatrix = rendered_object_matrix * Inverse(following_object_matrix * local_view_offset)

for additional stuff you need for this look at my answer here:

Spektre
  • 49,595
  • 11
  • 110
  • 380