Game Development Reference
In-Depth Information
m 11 +m 22 +m 33 +1
2
=⇒ x= m 23 −m 32
4w
y= m 31 −m 13
4w
z= m 12 −m 21
4w
w=
m 11 −m 22 −m 33 +1
2
=⇒ w= m 23 −m 32
4x
y= m 12 +m 21
4x
z= m 31 +m 13
4x
x=
−m 11 +m 22 −m 33 +1
2
=⇒ w= m 31 −m 13
4y
x= m 12 +m 21
4y
z= m 23 +m 32
4y
y=
−m 11 −m 22 +m 33 +1
2
=⇒ w= m 12 −m 21
4z
x= m 31 +m 13
4z
y= m 23 +m 32
4z
z=
Table 8.2. Extracting a quaternion from a rotation matrix
Listing 8.5 implements this strategy in a straightforward manner.
/ /
I n p u t
m a t r i x :
f l o a t
m11 , m12 , m13 ;
f l o a t
m21 , m22 , m23 ;
f l o a t
m31 , m32 , m33 ;
/ /
Output
q u a t e r n i o n
f l o a t
w, x , y , z ;
/ /
Determine
which
o f w,
x ,
y ,
o r
z
has
t h e
l a r g e s t
a b s o l u t e
v a l u e
f l o a t
fourWSquaredMinus1
=
m11
+
m22
+
m33 ;
f l o a t
f o u r X S q u a r e d M i n u s 1
=
m11 m22 m33 ;
f l o a t
f o u r Y S q u a r e d M i n u s 1
=
m22 m11 m33 ;
f l o a t
f o u r Z S q u a r e d M i n u s 1
=
m33 m11 m22 ;
i n t
b i g g e s t I n d e x
=
0 ;
f l o a t
f o u r B i g g e s t S q u a r e d M i n u s 1
=
fourWSquaredMinus1 ;
i f
( f o u r X S q u a r e d M i n u s 1 > f o u r B i g g e s t S q u a r e d M i n u s 1 )
{
f o u r B i g g e s t S q u a r e d M i n u s 1
=
f o u r X S q u a r e d M i n u s 1 ;
b i g g e s t I n d e x
=
1 ;
}
i f
( f o u r Y S q u a r e d M i n u s 1 > f o u r B i g g e s t S q u a r e d M i n u s 1 )
{
f o u r B i g g e s t S q u a r e d M i n u s 1
=
f o u r Y S q u a r e d M i n u s 1 ;
b i g g e s t I n d e x
=
2 ;
}
i f
( f o u r Z S q u a r e d M i n u s 1 > f o u r B i g g e s t S q u a r e d M i n u s 1 )
{
f o u r B i g g e s t S q u a r e d M i n u s 1
=
f o u r Z S q u a r e d M i n u s 1 ;
b i g g e s t I n d e x
=
3 ;
}
/ /
P e r f o r m
s q u a r e
r o o t
and
d i v i s i o n
f l o a t
b i g g e s t V a l
=
s q r t ( f o u r B i g g e s t S q u a r e d M i n u s 1
+
1 . 0 f )
0 . 5 f ;
f l o a t
m u l t
=
0 . 2 5 f
/
b i g g e s t V a l ;
/ /
Apply
t a b l e
t o
compute
q u a t e r n i o n
v a l u e s
s w i t c h
( b i g g e s t I n d e x )
{
c a s e
0 :
w =
b i g g e s t V a l ;
x
=
( m23 m32 )
m u l t ;
Search WWH ::




Custom Search