I am making a game in opengl and am trying to get a camera to follow an airplane. How can I get the camera to match the roll, pitch, and yaw of the plane? I am using glm as my math library. Thanks!


If you really need to match the camera with the plane, all you have to do is use same transformation data as you do for your plane, to generate a view matrix for your camera.

But here's a catch, though. Camera positioning works in reverse for rasterization, so you need to invert your plane position to use it as a basis for camera transformation matrix.

//I suggest using quaternion for rotations (glm::quat), but for now
//let's consider to use Euler angles, as they are stated in the question
glm::vec3 plane_rotation_euler;
glm::vec3 plane_position;
//Presumingly, this is how you do plane transformation
glm::mat4 plane_world_transform = glm::mat4(1.0f);
plane_world_transform =
    glm::translate(plane_world_transform, plane_position);
plane_world_transform =
    glm::rotate(plane_world_transform, plane_rotation_euler.x, glm::vec3(1, 0, 0));
plane_world_transform =
    glm::rotate(plane_world_transform, plane_rotation_euler.y, glm::vec3(0, 1, 0));
plane_world_transform =
    glm::rotate(plane_world_transform, plane_rotation_euler.z, glm::vec3(0, 0, 1));

//Then this is how you calculate camera view transformation matrix
glm::mat4 view_transform = glm::mat4(1.0f);
view_transform =
    glm::translate(view_transform , -plane_position);//Note the inversion
view_transform =
    glm::rotate(view_transform , plane_rotation_euler.x, glm::vec3(1, 0, 0));
view_transform  =
    glm::rotate(view_transform , plane_rotation_euler.y, glm::vec3(0, 1, 0));
view_transform =
    glm::rotate(view_transform , plane_rotation_euler.z, glm::vec3(0, 0, 1));
|improve this answer|||||

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.