Game Development Reference
In-Depth Information
/ /
Assume
t h e
m a t r i x
i s
s t o r e d
i n
t h e s e
v a r i a b l e s :
f l o a t
m11 , m12 , m13 ;
f l o a t
m21 , m22 , m23 ;
f l o a t
m31 , m32 , m33 ;
/ / We
w i l l
compute
t h e
E u l e r
a n g l e
v a l u e s
i n
r a d i a n s
/ /
and
s t o r e
them
h e r e :
f l o a t
h , p , b ;
/ /
E x t r a c t
p i t c h
from m32 ,
b e i n g
c a r e f u l
f o r
domain
e r r o r s
w i t h
/ /
a s i n ( ) .
We c o u l d
have
v a l u e s
s l i g h t l y
o u t
o f
r a n g e
due
t o
/ /
f l o a t i n g
p o i n t
a r i t h m e t i c .
f l o a t
s p
= m32 ;
i f
( s p < =
1.0 f )
{
p
=
1.570796 f ;
/ /
p i /2
}
e l s e
i f
( s p > =
1 . 0 f )
{
p
=
1.570796 f ;
/ /
p i /2
}
e l s e
{
p
=
a s i n ( s p ) ;
}
/ /
Check
f o r
t h e
Gimbal
l o c k
case ,
g i v i n g
a
s l i g h t
t o l e r a n c e
/ /
f o r
n u m e r i c a l
i m p r e c i s i o n
i f
( f a b s ( s p ) > 0 . 9 9 9 9 f )
{
/ / We a r e
l o o k i n g
s t r a i g h t
up
o r
down .
/ /
Slam
bank
t o
z e r o
and
j u s t
s e t
h e a d i n g
b
=
0 . 0 f ;
h
=
a t a n 2 ( m13 ,
m11 ) ;
}
e l s e
{
/ /
Compute
h e a d i n g
from m13 and m33
h
=
a t a n 2 ( m31 ,
m33 ) ;
/ /
Compute
bank
from m21 and m22
b
=
a t a n 2 ( m12 ,
m22 ) ;
}
Listing 8.4
Extracting Euler angles from an object-to-upright matrix
8.7.3 Converting a Quaternion to a Matrix
We have a few options for converting a quaternion to a rotation matrix. The
more common way is to expand the quaternion multiplication qvq
−1 . This
produces the correct matrix, but we are left with no real confidence why the
matrix is correct. (We are, however, left with some experience manipulating
quaternions; see Exercise 10.) We take a different option and stick purely
to the geometric interpretation of the components of the quaternion. Since
a quaternion is essentially an encoded version of an axis-angle rotation, we
attempt to construct the matrix from Section 5.1.3, which rotates about an
arbitrary axis:
2
4
3
5
n x 2 (1−cos θ) + cos θ
n x n y (1−cos θ) + n z sin θ
n x n z (1−cos θ)−n y sin θ
n y 2 (1−cos θ) + cos θ
n x n y (1−cos θ)−n z sin θ
n y n z (1−cos θ) + n x sin θ
.
n z 2 (1−cos θ) + cos θ
n x n z (1−cos θ) + n y sin θ
n y n z (1−cos θ)−n x sin θ
 
Search WWH ::




Custom Search