Game Development Reference
In-Depth Information
It is not too di cult to understand why q t interpolates from identity to
q as t varies from 0 to 1. Notice that the log operation essentially converts
the quaternion to exponential map format (except for a factor of 2). Then,
when we perform the scalar multiplication by the exponent t, the effect is to
multiply the angle by t. Finally, the exp “undoes” what the log operation
did, recalculating the new w and v from the exponential vector. At least
this is how it works academically in an equation. Although Equation (8.8)
is the o cial mathematical definition and works elegantly in theory, direct
translation into code is more complicated than necessary. Listing 8.2 shows
how we could compute the value of q t in C. Essentially, instead of working
with a single exponential-map-like quantity as the formula tells us to, we
break out the axis and half-angle separately.
/ /
Q u a t e r n i o n
( i n p u t
and
o u t p u t )
f l o a t
w, x , y , z ;
/ /
I n p u t
exponent
f l o a t
e x p o n e n t ;
/ /
Check
f o r
t h e
c a s e
o f
an
i d e n t i t y
q u a t e r n i o n .
/ /
T h i s
w i l l
p r o t e c t
a g a i n s t
d i v i d e
by
z e r o
i f
( f a b s (w) < . 9 9 9 9 f )
{
/ /
E x t r a c t
t h e
h a l f
a n g l e
a l p h a
( a l p h a
=
t h e t a / 2 )
f l o a t
a l p h a
=
a c o s (w ) ;
/ /
Compute new
a l p h a
v a l u e
f l o a t
newAlpha
=
a l p h a
e x p o n e n t ;
/ /
Compute new w v a l u e
w =
c o s ( newAlpha ) ;
/ /
Compute new
xyz
v a l u e s
f l o a t
m u l t
=
s i n ( newAlpha )
/
s i n ( a l p h a ) ;
x
=
m u l t ;
y
=
m u l t ;
z
=
m u l t ;
}
Listing 8.2
Raising a quaternion to an exponent
There are a few points to notice about this code. First, the check for
the identity quaternion is necessary since a value of w = ±1 would cause
the computation of mult to divide by zero. Raising an identity quaternion
to any power results in the identity quaternion, so if we detect an identity
quaternion on input, we simply ignore the exponent and return the original
quaternion.
Second, when we compute alpha , we use the arccos function, which
always returns a positive angle. This does not create a loss of generality.
Any quaternion can be interpreted as having a positive angle of rotation,
since negative rotation about an axis is the same as positive rotation about
the axis pointing in the opposite direction.
Search WWH ::




Custom Search