Rotation Matrix

Every object in a 3D game will have to be rotated at some point so that it faces in an appropriate direction. Most things face in the direction they are moving, but you can orient a 3D object in any direction you wish. Rotation is also a good way to draw attention to an object. For example, power-ups (ammo, extra lives, etc.) often rotate around the y axis so they stand out from the background scenery.

The simplest type of rotation matrix is a rotation about the x, y, or z axis, which you can create with the x_rotation, y_rotation, and z_rotation class methods in Matrix44 (see Figure 9-3).

y_rotation

Figure 9-3. Rotation matrices x rotation x rotation

z rotation

Figure 9-3. Rotation matrices

To predict which way a point will rotate, visualize yourself looking along the axis of rotation. Positive rotations go counterclockwise and negative rotations go clockwise. Let's experiment with this in the interactive interpreter. We are going to rotate a point at (0, 10, 0), -45 degrees around the z axis (see Figure 9-4).

>>> z_rotate = Matrix44.z_rotation(radians(-45))

This displays a z rotation matrix, and the result of using it to translate the point (0, 10, 0):

[ 0 0 1 0 ] [ 0 0 0 1 ] (7.0710678118654746, 7.0710678118654755, 0.0)

If the original point were the end of a watch hand at 12 o'clock, then the transformed point would be halfway between 1 and 2.

Figure 9-4. A rotation about the z axis

When working with 3D rotation, I find it helpful to visualize an axis for my head, where (0, 0, 0) is in my brain somewhere. The x axis points out of my right ear, the y axis points out the top of my head, and the z axis points out of my nose. If I were to rotate my head around the x axis, I would nod my head up and down. Rotating around the y axis would make me turn my head left or right. A rotation around the z axis would make me tilt my head quizzically from side to side. Alternatively, you can use your thumb and first two fingers to point along the positive direction of each axis and physically rotate your hand when you are thinking about rotations.

Was this article helpful?

0 -4

Post a comment