Game Development Reference
In-Depth Information
be a bit more expensive to calculate, depending on the system. But regardless of
the interpolation type, the aforementioned objective arrow problem can easily be
solved with quaternions.
Quaternions also only require four floating point values to store their information,
which means memory is saved. So just as the position and uniform scale of an
object can be stored as a 3D vector and float, respectively, the orientation of the
object can be stored with a quaternion.
Forallintentsandpurposes,gamesuse unit quaternions ,which,likeunitvectors,
are quaternions with a magnitude of one. A quaternion has both a vector and a
scalar component and is often written as . The calculation of the vec-
tor and scalar components depends on the axis of rotation, â, and the angle of ro-
tation, θ :
It should be noted that the axis of rotation must be normalized. If it isn't, you
may notice objects starting to stretch in non-uniform ways. If you start using qua-
ternions, and objects are shearing in odd manners, it probably means somewhere a
quaternion was created without a normalized axis.
Most 3D game math libraries should have quaternion functionality built in. For
these libraries, a CreateFromAxisAngle or similar function will automatic-
ally construct the quaternion for you, given an axis and angle. Furthermore, some
math libraries may use x, y, z, and w-components for their quaternions. In this
case, the vector part of the quaternion is x, y, and z while the scalar part is w.
Now let's think back to the spaceship problem. You have the initial facing of the
shipdownthez-axis,andthenewfacingcanbecalculatedbyconstructingavector
from the ship to target position P . To determine the axis of rotation, you could take
the cross product between the initial facing and the new facing. Also, the angle of
rotation similarly can be determined by using the dot product. This leaves us with
both the arbitrary axis and the angle of rotation, and a quaternion can be construc-
ted from these.
It is also possible to perform one quaternion rotation followed by another. In order
to do so, the quaternions should be multiplied together, but in the reverse order.
Search WWH ::




Custom Search