Game Physics Cookbook
上QQ阅读APP看书,第一时间看更新

How rotations work

A three-dimensional rotation can be expressed as three individual rotations, one around the X Axis, one around the Y Axis, and one around the Z Axis. The smallest matrix we can use to store this type of rotation is a 3 X 3 matrix. When storing rotation in a larger 4 X 4 matrix, we store rotations in its upper 3 X 3 sub-matrix.

The 3 X 3 Rotation Matrix is composed of three vectors that represent each axis of the coordinate system of the matrix. These vectors are called the basis vectors. The basis vectors are stored row or column wise depending on the major of the matrix. We use a 3 X 3 matrix to store three-dimensional rotation data; it is not the only function of a 3 X 3 matrix. We will discuss different uses of 3 X 3 matrices later in the book:

How rotations work

The orientation of this 3 X 3 matrix can be expressed by some combination of yaw, pitch, and roll. Yaw represents rotation around the objects, local Perpendicular Axis, the Y-Axis. Pitch is the rotation around the object's local Lateral Axis, the X-Axis. Roll is the rotation around the object's local Longitudinal Axis, the Z-Axis:

How rotations work

To get a complete rotation, we combine yaw, pitch, and roll into one matrix using matrix multiplication. With this method each axis is rotated in succession. That means each rotation affects the axis of the previous rotations. Because of this it is possible for two or more axes axis to align, causing a loss in degree of rotational freedom. This is known as Gimbal Lock:

How rotations work

In the preceding figure, the regular rotation rotates the object 45 degrees on its X Axis; this only affects the object in terms of its Pitch. Next, the object is rotated 45 degrees on its Y Axis. This rotation affects the plane object in terms of both its Yaw and Pitch. Finally, the object is rotated 45 degrees on its Z Axis. This final rotation affects the object in terms of Yaw, Pitch, and Roll. Each rotation affects the previous rotation.

The same figure also demonstrated a Gimbal Lock. For the Gimbal Lock to happen, the object is rotated 90 degrees around its X Axis. This rotation only affects the object in terms of Pitch. Next, the object is rotated 90 degrees around its Y Axis. This affects the object in terms of Yaw and Pitch. This is where the Gimbal Lock happens. The change in Yaw aligned the objects Pitch and Roll to be on the same Axis! We can no longer change the Pitch or Roll of the object independently. At this point we have lost a degree of rotational freedom.

As long as we use Euler angles there no solution to Gimbal Lock. We can use an axis angle matrix representation, which does not rely on Euler angles, to avoid Gimbal Lock. Angle Axis matrices will be described later in this chapter.

Getting ready

We're going to implement a Rotation function that will take three Euler angles that represent rotation around each axis. The Rotation function will call three helper functions: XRotation, YRotation, and ZRotation. These helper functions will be implemented in the next section. Because a three-dimensional rotation can be represented in a 3 X 3 or a 4 X 4 matrix, we need to implement separate methods to generate each.

How to do it…

Follow these steps to create a rotation matrix using Euler angles on each axis:

  1. Add the rotation function declarations to matrices.h:
    mat4 Rotation(float pitch, float yaw, float roll);
    mat3 Rotation3x3(float pitch, float yaw, float roll);
  2. Implement the rotation functions in matrices.cpp:
    mat4 Rotation(float pitch, float yaw, float roll) {
       return  ZRotation(roll) * 
               XRotation(pitch) * 
               YRotation(yaw);
    }
    mat3 Rotation3x3(float pitch, float yaw, float roll) {
       return  ZRotation3x3(yaw) *
               XRotation3x3(pitch) * 
               YRotation3x3(yaw);
    }

How it works…

The preceding code creates a rotation matrix by combining rotations around the Z-Axis first, X-Axis second, and Y-Axis last. Because we are representing rotation using Euler angles here, Gimbal Lock is a possible problem. As mentioned earlier, the individual XRotation, YRotation, and ZRotation functions will be described in the next section.

Note

This rotation order mimics the D3DX YawPitchRoll function. We implemented the Rotation function for both 3 X 3 and 4 X 4 matrices because either matrix could represent a three-dimensional rotation.