Game Development Reference
In-Depth Information
think you might be hazy on the whole rotation thing, play with a Direct3D sample
for a while, and you ' ll get it.
Quaternion Mathematics
Orientation can be expressed as three angles: yaw, pitch, and roll. In our teapot
example, yaw would be around the Y-axis, pitch would be around the Z-axis, and
roll would be around the X-axis. By the way, this happens to be called the Euler
representation, or Euler angles (you pronounce Euler like
). This method has
a critical weakness. Imagine that you want to interpolate smoothly between two
orientations. This would make sense if you had an object like an automated cannon
that slowly tracked moving objects. It would know its current orientation and the
target orientation, but getting from one to the other might be problematic with Euler
angles.
There is a special mathematical construct known as a quaternion, and almost every
3D engine supports its use. A quaternion is a fourth-dimensional vector, and it can
be visualized as a rotation about an arbitrary axis. Let
oiler
'
s look at an example:
D3DXQUATERNION q;
D3DXQuaternionIdentity(&q);
D3DXVECTOR3 axis(0,1,0);
float angle = -D3DX_PI / 4.0;
D3DXQuaternionRotationAxis(&q, &axis, angle);
D3DXMATRIX result;
D3DXMatrixRotationQuaternion(&result, &q);
This code has exactly the same effect on our teapot as the first rotation example. The
teapot rotates around the Y-axis
/4 degrees. Notice that I
'
m not setting the values
−π
of the quaternion directly, I
m using a DirectX API. I do this because the actual
values of the quaternion are not intuitive at all. Take a look at the resulting values
from our simple twist around the Y-axis:
'
q
{...}
D3DXQUATERNION
x
0.00000000
float
y
−0.38268343
float
z
0.00000000
float
w
0.92387950
float
Not exactly the easiest thing to read, is it?
The quaternion is sent into another DirectX function to create a transformation
matrix. This is done because vectors can
'
t be transformed directly with quaternions
you still have to use a transform matrix.
 
 
Search WWH ::




Custom Search