Search code examples
c++openglmathrotationglulookat

openGL - Rotating above and below an object using gluLookAt


I have a cube in open GL. I have the cube rotating by multiplying by the rotation matrix. I have also got the camera rotating around the cube on the x axis using gluLookAt as you can see below.

    case 'j':
        eyeX = 10*cos(angle);
        eyeZ = 10*sin(angle);
        centerX = -cos(angle);
        centerZ = -sin(angle);
        angle -= 0.1;
        break;
    case'l':
        eyeX = 10*cos(angle);
        eyeZ = 10*sin(angle);
        centerX = -cos(angle);
        centerZ = -sin(angle);
        angle += 0.1;
        break;

where

gluLookAt( eyeX, eyeY, eyeZ, centerX, centerY, centerZ, 0.0, 1.0, 0.0);

What I am struggling with is getting the camera to rotate above and below the cube circling it on the y axis.


Solution

  • To make the cam orbiting an object

    • subtract the object's world pos. from the cam world pos.,

      cam_pos_os = cam_pos_ws - object_pos_ws

    • rotate the vector pointing to cam_pos_os like you do it in world space in your code

    • add the object's world pos. again to the cam pos. in object space,

      cam_pos_ws = cam_pos_os + object_pos_ws