# Pitch Yaw and Roll

## Recommended Posts

I am having some problems implementing a camera with flight simulator like movement. Let me give you a little back story about how I arrived at this problem. I have been reading about matrix rotations recently and have been putting them to use in a basic camera class. I store the orientation of the camera in a 3x3 matrix that contains the up, right, and forward vectors of the camera, which are initially aligned with +Y, +X, and +Z respectively. The camera also has a vector called position which stores the position of the camera. Finally, there are three floats: rotx, roty, and rotz, which are Euler angles representing the camera's orientation. Through each iteration of the game loop, user input is read from the mouse. The mouse increases or decreases the rotx and roty angles. I use the following three rotation matrices in conjunction with the rotx and roty to produce new orientations for the up, right, and forward vectors:
Rotation about X axis
{ 1,           0,          0 }
{ 0,  cos(theta), sin(theta) }
{ 0, -sin(theta), cos(theta) }

{ cos(theta), 0, -sin(theta) }
{          0, 1,           0 }
{ sin(theta), 0,  cos(theta) }

{  cos(theta), sin(theta), 0 }
{ -sin(theta), cos(theta), 0 }
{           0,          0, 1 }

Using this approach I was able to create a basic first person camera. If you looked up and pressed the forward key, you would fly forward up above the scene. If you looked back down and pressed forward you could fly back down to the scene. That alone was very exciting for me. However, I decided to get adventurous and add rotation about the Z axis. Here is where everything started to go down hill. Rather than rotating around the camera's forward vector, the camera would rotate around the world's Z axis! This is not what you would expect when performing a roll inside of an airplane. Further more, after making a small rotation about the Z axis, I discovered that the other rotations remained about the global axis as well. This behavior did not make itself apparent with the more restricted movement of the basic first person camera. So, where do I go from here? I think I need to learn how to rotate about the local axis as opposed to the global axis. However, this is causing me some confusion because I thought that I was performing rotations based on the local axis of the camera. Since my camera is initially aligned perfectly with the world axis, perhaps it just appears that my rotations are occurring around the world axis. If any of you have had similar problems, I would really appreciate your pointers and suggestions. Here is the snippet of code where I'm creating the rotation matrix (might be useful to you):
orientation = Matrix3x3::getXRotation(xrot) * Matrix3x3::getYRotation(yrot) * Matrix3x3::getZRotation(zrot)


##### Share on other sites
You are probably getting gimbal lock. Here's a decent article about it:

http://www.3dlessons.com/tutorials/Eulars-Quats-Gimbal-Lock.-08768.html

##### Share on other sites
Thank you so much for the link, it eventually lead me to the solution I was looking for. I was not experiencing gimbal lock, just general noobness.

First off, I was not using incremental rotations. Here is some pseudo code that describes my first approach:

xrot += mouseyyrot += mousexif roll rightzrot += 0.001if roll leftzrot -= 0.001orientation = orientation * XRotationMatrix(xrot) * YRotationMatrix(yrot) * ZrotationMatrix(zrot)

My new approach is as follows:

xrot = mouseyyrot = mousexif roll rightzrot = 0.001if roll leftzrot = 0.001orientation = XRotationMatrix(xrot) * YRotationMatrix(yrot) * ZrotationMatrix(zrot) * orientation

Notice how I also had to adjust where I was multiplying the rotations against the current orientation. After making those changes, the behavior is exactly what I was looking for! Here is the post that helped give me the light bulb moment, just in case someone else finds it useful: http://forums.xna.com/forums/t/405.aspx

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628337
• Total Posts
2982164

• 9
• 24
• 9
• 9
• 13