Sunday, January 08, 2006

Quaternions and Rotations (Part 2)

Rotations have a number of different representations, three of which are particularly useful in games, namely, Euler angles, 3x3 matrices and quaternions. Euler angles are the most compact having only 3 elements (the minimum number possible given that there are 3 degrees of freedom for a general rotation), compared to 9 for matrices and 4 for quaternions.

Euler angles are most useful when the rotation naturally decomposes into a rotation about the vertical axis and an elevation angle. Thus they tend to be used for gun emplacements, players, camera systems and so forth. However, it is often necessary to convert Euler angles to a matrix before performing some common operations, and thus they tend to use more CPU time than the other representations.

Matrices are probably the simplest representation of rotations, and by extension from 3x3 to 4x4 they can represent a combined translation and rotation. Since they can efficiently rotate vectors through matrix multiplication, they are used by 3d graphics APIs (e.g. Direct3D and OpenGL) for viewing transformations etc, that is, placing the polygonal models and camera in the 3d world space. For operations that involve rotating and translating large numbers of position vectors then matrices are the most efficient representation. However they do have some disadvantages: they are less compact than quaternions, also multiplying matrices is typically slower than multiplying quaternions, and finally when repeatedly multiplying matrices together they eventually become non-orthogonal, due to numerical errors, and must be re-orthogonalised (a fairly expensive operation). Quaternions by comparison can be re-normalised efficiently.

Human characters are usually represented as a skeleton of (20 or more) bones, which have vertices attached to them. These vertices define the polygons of the "skin", and for smooth skinned models some vertices are attached to more than one bone using weightings. For each frame of an animation, the lengths of the bones and the angles between the bones (stored as quaternions) define the posture of the skeleton. The memory required to store animations can be significant. In order to save memory only certain "keyframes" of the animation are stored and in-between frames are calculated by interpolation. This requires interpolation of the bone rotations, and quaternions are particularly well suited for this as there exists a slerp (sinusoidal linear interpolation) formula that can be used. Essentially one can interpolate between 2 quaternions by travelling along the great circle that passes through the corresponding 2 points on the 3-sphere. As quaternions are compact and easy to compress further, and because of the existence of this slerp formula, they are ideal for use in animation systems.

Labels:

0 Comments:

Post a Comment

<< Home