Game Development Reference
In-Depth Information
So if an object should be rotated by q and then by p , the multiplication is pq . The
multiplication for two quaternions is performed using the Grassmann product :
Much like matrices, quaternions have an inverse. Luckily, to calculate the inverse
of a unit quaternion you simply negate the vector component. This negation of the
vector component is also known as the conjugate of the quaternion.
Because there is an inverse, there must be an identity quaternion. This is defined
as:
Finally, because the GPU still expects the world transform matrix to be in matrix
form, the quaternion will, at some point, need to be converted into a matrix. The
calculation of a matrix from a quaternion is rather complex, so it's not included in
this text. But rest assured that your 3D math library most likely has a function to
do this for you.
3D Game Object Representation
Once we are using quaternions, the translation, scale, and rotation can all be rep-
resented with data smaller than a single 4×4 matrix. This is what should be stored
for the world transform of a 3D game object. Then, when it's time to pass world
transform matrix information to the rendering code, a temporary matrix can be
constructed. This means that if code needs to update the position of the game ob-
ject, it only needs to change the position vector, and not an entire matrix (see List-
ing 4.3 ).
Listing 4.3 3D Game Object
Click here to view code image
class 3DGameObject
Quaternion rotation
Vector3 position
float scale
Search WWH ::




Custom Search