‘ NOTE ^ Euler angles are probably the most familiar, and in most cases, the most practical way to represent the orientation of an object. A set of three Euler angles specifies the rotation around each axis, X, Y, and Z. For example, you might use Euler angles to represent the orientation of an airplane in your flight simulator. Rotations around each axis are called roll, pitch, and yaw; the axis each is I associated with varies de — I pending on the coordinate к system and the applications. a |
Quaternions comprise a set of four numbers that are used to represent rotations. Consider three-dimensional rotation, rotation in threedimensional space, discussed in Chapter 1, add one more dimension, and you end up with a fourdimensional rotation. There are two components in each quaternion, a vector component which consists of x, y, and z values, and a scalar value which is usually denoted as w. The scalar value represents the angle of rotation in the form cos(angle / 2).
Quaternions can also be written as a scalar value such as n followed by a bold letter representing a vector.
For example, =q = [nv].
I can already see that you are thinking, “why the heck would I need something like that?” To understand why you might need
quaternions, you first must learn a bit about another important part of 3D graphics, Euler angles, which are discussed in more detail later in this chapter.
There are several reasons you should use quaternions to rotate your objects and characters in your games. The first, and most important, advantage is the fact that quaternions are not susceptible to gimbal lock, a phenomenon in which an object loses the ability to rotate on one of its three axes. Gimbal lock shows its face when two axes point in the same direction. This causes all sorts of problems. See Figure 2.1 for a picture of gimbal lock.
Another advantage to quaternions is the ability to have smooth, interpolated rotations. It is much easier to interpolate rotations using quaternions rather than matrices, resulting in smoother animations. The last main advantage of using quaternions is they take less room than a rotation matrix (four elements versus nine), and some operations are cheaper to perform on quaternions in terms of processor cycles.
Even though quaternions have many advantages over matrices, keep in mind that most graphics APIs will not accept quaternions directly.
They must first be converted into matrices, a process that takes a fair amount of CPU cycles. Keep this in mind when deciding whether you are going to use quaternions in your game engine.
Figure 2.1 When two axes point in the same direction, you get gimbal lock. When you use quaternions for rotations, you avoid any possibility of gimbal lock occurring.