0
votes

I'm making a game in OpenGL, using freeglut.

I have a car, which I am able to move back and forward using keys and the camera follows it. Now, when I turn the car(glRotate in xz plane), I want the camera to change the Camera position(using gluLookAt) so it always points to the back of the car.

Any suggestions how do I do that?

1
The camera always points to the back of the car ... does the camera also need to follow the car? I.e. should the camera stay behind the car?LarsH
Yes, that's exactly what I want to do. I tried manipulating the values of direction vectors (sin/cos), but I don't know much about it so I am not sure. Let me know if you have any suggestions about it, and I can try figuring out again. Thanks!user2148703
Take a look at msdn.microsoft.com/en-us/library/…. While it is C#/XNA, it would give you idea of math behind this - after all, camera is all about math and transformation order.keltar

1 Answers

0
votes

For camera follow I use the object transform matrix

  1. get object transform matrix

    camera=object
    

    use glGetMatrix or whatever for that

  2. shift rotate the position so Z axis is directing where you want to look

    I use object aligned to forward on Z axis, but not all mesh models are like this so rotate by (+/-)90 deg around x,y or z to match this:

    • Z-axis is forward (or backward depends on your projection matrix and depth function)
    • X-axis is Right
    • Y-axis is Up

    with respect to your camera/screen coordinate system (projection matrix). Then translate to behind position

  3. apply POV rotation (optional)

    if you can slightly rotate camera view from forward direction (mouse look) then do it at this step

    camera*=rotation_POV
    
  4. convert matrix to camera

    camera matrix is usually inverse of the coordinate system matrix it represents so:

    camera=Inverse(camera)
    

For more info look here understanding transform matrices the OpenGL inverse matrix computation in C++ is included there.