Yaw, pitch, roll camera

From Sidvind
Jump to: navigation, search

Yaw, pitch and roll is a way of describing the rotation of the camera in 3D. There is other ways like quaternions but this is the simplest. Yaw, pitch and roll is the name of how much we should rotate around each axis.

Yaw, pitch and roll[edit]

Fig 1.

Think about yourself as the camera right now. Look around a bit. Yaw is the angle when moving the head left ↔ right (rotation around Y-axis). Pitch is up and down (rotation around X-axis). Roll, which we usually don't experience is when you tilt your head (rotation around Z-axis).

This can be a bit confusing at start but you get used to it. Depending on the application we might omit roll. In a typical FPS we can, but a flight simulator would strange if the camera can't roll.

Also, yaw might sometimes be refered to as heading, but it's the same thing.

Basic camera (omitting roll)[edit]

Code: Camera.h

  1. class Camera{
  2.         public:
  3.                 void view();
  4.                 void motion(int x,int y);
  5.         protected:
  6.                 Vector3 pos;
  7.                 Vector3 target;
  8.                 GLfloat phi;
  9.                 GLfloat theta;
  10. };

Code: Camera.cpp

  1. void Camera::motion(int x,int y){
  2.         theta+= x / 200;        /* Adjust this to control the sensitivity */
  3.         phi+= y / 200;
  5.         target.x = cos(theta) * sin(phi);
  6.         target.y = cos(phi);
  7.         target.z = sin(theta) * sin(phi);
  8. }
  10. void Camera::view(){
  11.         gluLookAt (pos.x,pos.y,pos.z,
  12.                    pos.x + target.x,
  13.                    pos.y + target.y,
  14.                    pos.z + target.z,
  15.                    0,1,0
  16.                   );
  17. }

I won't explain the formula used to convert the angles to a vector, use it if you want. It works in this case. When you move the mouse call the motion method with dx and dy.