3D Game Engine Design

Download Report

Transcript 3D Game Engine Design

3D Game Engine Design
Ch. 2.3.
3D MAP LAB
3D Game Engine Design
1
Quaternion

Quaternion Algebra

Def >


A Quaternion is defined by q = w + xi + yj + zk
where w, x, y, z are in R and
Def>

Let
then
3D Game Engine Design
2
Quaternion(cont’d)
q  q*  w2  x 2  y 2  z 2
3D Game Engine Design
3
Quaternion(cont’d)

Thm>
Let vˆ  xi  yj  zk then q  w  vˆ and let vˆ  ( x, y, z )
then quaternion multiplication can be defined using
vector dot product and cross product
q0  q1  ( w0  vˆ0 )(w1  vˆ1 )  ( w0 w1  vˆ0  vˆ1 )  w0 vˆ1  w1vˆ0  vˆ0  vˆ1
 ( w0 w1  x0 x1  y0 y1  z0 z1 )  ( w0 x1 , w0 y1 , w0 z1 )  ( w1 x0 , w1 y0 , w1 z0 )
 ( y0 z1  z0 y1 , z0 x1  x0 z1 , x0 y1  y0 x1 )
 ( w0 w1  x0 x1  y0 y1  z0 z1 )  ( w0 x1  w1 x0  y0 z1  z0 y1 )i
 ( w0 y1  w1 y0  z0 x1  x0 z1 ) j  ( w0 z1  w1 z0  x0 y1  y0 x1 )k

Thm>
A quaternion q may be also viewed as a 4D vector(
w, x, y, z).then the dot product of two quaternion is
q0  q1  w0 w1  x0 x1  y0 y1  z0 z1  W (q0  q1* )
3D Game Engine Design
4
Quaternion(cont’d)

Def>


Thm>


A unit quaternion is a quaternion q for which N (q)  1
The inverse of unit quaternion and the product of unit
quaternion
are themselves unit quaternion.
Unit quaternion can be represented by
q  w  xi  yj  zk , w2  x 2  y 2  z 2  1
q  w  1  w2 (
x
1 w
q  cos  uˆ sin  ,
2
i
y
1 w
2
j
z
1 w
where cos  w, sin   1  w2 , and uˆ 
3D Game Engine Design
2
k)
x
1 w
2
i
y
1 w
2
j
z
1 w
2
k
5
Quaternion(cont’d)
uˆ  u0i  u1 j  u2k and vector(u0 , u1 , u2 )

has length 1.
However observe that the quaternion product
uˆuˆ  1 similar
to unit-length complex numbers z  cos   i sin 
Euler’s identity for complex numbers generalizes to quaternion.
Euler' s Idnetity : ei  cos  i sin 
General form

: euˆ  cos  uˆ sin 
Thm>

The power of unit quaternion
ˆ sin  )t  cos(t )  u
ˆ sin(t )
qt  (cos  u

The logarithm of unit quaternion
loge (q)  loge (cos  uˆ sin  )  loge (euˆ )  uˆ
3D Game Engine Design
6
Quaternion(cont’d)


Caution : standard identities are not allowed because of
non-commutative of quaternion.
That is, e peq  e pq and log p  log q  log( p  q)
3D Game Engine Design
7
Quaternion(cont’d)

Relationship to quaternion to rotation

Thm>

Unit quaternion q  cos  uˆ sin  represents the rotation of
the 3D vector vˆ by an angle 2 about the 3D axis uˆ . And
rotated vector is represented by R(vˆ)  qvˆq* .
 Proof>
it is enough to show that
 R(v) is 3D vector
 R(v) is length preserving function.
 R(v) is linear transformation.
 R(v) doesn’t have reflection component.
 Rotation axis is really u. ( 1)
 Rotation angle 2
(2)
 We will prove (1),(2)
3D Game Engine Design
8
Quaternion(cont’d)

(1) R(uˆ )  quˆq *
 (cos  uˆ sin  )uˆ (cos  uˆ sin  )
 (uˆ cos  sin  )(cos  uˆ sin  )
 uˆ cos2   cos sin   cos sin   uˆ sin 2 
 uˆ (cos2   sin 2  )  uˆ

(2)
ˆ be a right  handed set of orthonorm al vectors.
let uˆ, vˆ and w
ˆ  vˆ  w
ˆ  0, and uˆ  vˆ  w
ˆ , vˆ  w
ˆ  uˆ, and w
ˆ  uˆ  vˆ
that is, uˆ  vˆ  uˆ  w
assum that the vector vˆ rotated by an angle  to qvˆq*
then cos()  vˆ  (qvˆq* )
u
(qvˆq* )
2
v
3D Game Engine Design

w
9
Quaternion(cont’d)
vˆ  (qvˆq* )  W (vˆ*qvˆq* )  W [vˆ(cos  uˆ sin  )vˆ(cos  uˆ sin  )]
 W [(vˆ cos  vˆuˆ sin  )(vˆ cos  vˆuˆ sin  )]
 W [vˆ 2 (cos ) 2  vˆ 2uˆ sin  cos  vˆuˆvˆ sin  cos  (vˆuˆ ) 2 (sin ) 2 ]
 W [(cos ) 2  (sin ) 2  (uˆ  vˆuˆvˆ) sin  cos ]
now vˆuˆ  vˆuˆ  vˆ  uˆ  vˆ  uˆ   wˆ and vˆuˆvˆ   wˆ vˆ  wˆ  vˆ  wˆ  vˆ  uˆ,
consequently,
cos()  W [(cos ) 2  (sin ) 2  (uˆ  vˆuˆvˆ) sin  cos )]
 W [(cos ) 2  (sin ) 2  uˆ (2 sin  cos )]
 W [(cos ) 2  (sin ) 2 ]
 cos(2 )
this com pletethe proof 2
3D Game Engine Design
10
Quaternion(cont’d)

Conversion between various rotation
representations
Angle-axis rotation
Quaternion rotation
3D Game Engine Design
Rotation matrix
11
Quaternion(cont’d)

Conversion between angle-axis and rotation
matrix

Thm> angle-axis to rotation matrix
Let  be an angle of rotation and uˆ  u0,u1,u2  be the unit lengthaxis of
rotation then corresponding rotation m atrix is
R  I  (sin ) S  (1  cos ) S 2 , where I is theidentity m atrix and
 0  u2
S   u2
0
 u1 u0
3D Game Engine Design
u1 
 u0 
0 
12
Quaternion(cont’d)

Thm> Rotation matrix to angle-axis
r00 r01 r02 

R   r10 r11 r12  then angle
 ? axis  ?
r20 r21 r22 
define trace( R) : r00  r11  r22 then cos  (trace( R)  1) / 2
 0  u2
R  RT  (2 sin  ) S , where S   u2
0
 u1 u0
u1 
 u0 
0 
=0 or 180(??)
3D Game Engine Design
13
Quaternion(cont’d)

Conversion between quaternion and angle-axis

Thm> angle-axis to quaternion
q  w  xi  yj  zk  cos( / 2)  sin( / 2)(u0i  u1 j  u2 k ), w2  x 2  y 2  z 2  1
represent rotation by  about the axis u  (u0 , u1 , u2 )
Given the angle and axis, the com ponentof the quaternionare
w  cos( / 2), x  u0 sin( / 2), y  u1 sin( / 2), and z  u2 sin( / 2).

Thm> quaternion to angle-axis
q  w  xi  yj  zk  cos( / 2)  sin( / 2)(u0i  u1 j  u2 k ), w2  x 2  y 2  z 2  1
  2 cos1 ( w) and u  ( x, y, z ) / 1  w2
3D Game Engine Design
14
Quaternion(cont’d)

Conversion between quaternion and rotation
matrix

Thm> quaternion to rotation matrix

q  w  xi  yj  zk
then Rotation m atrix  ??
1  2 y 2  2 z 2

R   2 xy  2 wz
 2 xz  2 wy

2 xy  2 wz
1 2x2  2z 2
2 yz  2 wx
2 xz  2 wy 

2 yz  2 wx 
1 2x2  2 y 2 

Thm> rotation matrix to quaternion
 r00 r01 r02 
 then q  w  xi  yj  zk ???
R
r
r
r
10
11
12



r20 r21 r22 

define trace( R) : r00  r11  r22 then cos  (trace( R)  1) / 2
then w 
3D Game Engine Design
trace( R)  1 / 2
15
x  (r12  r21 ) /(4 w), y  (r20  r02 ) /(4 w), z  ( r01  r10 ) /(4 w
)