Transcript Document
Game Mathematics
1
Essential Mathematics for Game Development
Matrices
Vectors
Fixed-point Real Numbers
Triangle Mathematics
Intersection Issues
Euler Angles
Angular Displacement
Quaternion
Differential Equation Basics
2
Matrices
Matrix basics
– Definition
A
=
(aij) =
C
=
A
C
=
A + B
a11 .. a1n
.
.
.
.
am1 .. amn
– Transpose
T
cij = aji
– Addition
cij = aij + bij
3
– Scalar-matrix multiplication
C
=
aA
cij = aaij
– Matrix-matrix multiplication
C
=
A B
cij
r
= Saikbkj
k = 1
4
Transformations in Matrix form
– A point or a vector is a row matrix (de facto convention)
V = [x y z]
– Using matrix notation, a point V is transformed under
translation, scaling and rotation as :
V’ = V + D
V’ = VS
V’ = VR
where D is a translation vector and
S and R are scaling and rotation matrices
5
– To make translation be a linear transformation, we
introduce the homogeneous coordinate system
V (x, y, z, w)
where w is always 1
– Translation Transformation
x’ = x + Tx
y’ = y + Ty
z’ = z + Tz
V’ = VT
[x’ y’ z’ 1] = [x y z 1]
= [x y z 1] T
1
0
0
Tx
0
0
1
0
0
1
Ty Tz
0
0
0
1
6
– Scaling Transformation
x’ = xSx
y’ = ySy
z’ = zSz
V’ = VS
Sx
[x’ y’ z’ 1] = [x y z 1] 0
0
0
0
Sy
0
0
0
0
Sz
0
0
0
0
1
= [x y z 1] S
Here Sx, Sy and Sz are scaling factors.
7
– Rotation Transformations
Rx
Ry
Rz
=
1
0
0
0 cosq sinq
0 -sinq cosq
0
0
0
0
0
0
1
=
cosq
0
sinq
0
0 -sinq
1
0
0 cosq
0
0
0
0
0
1
=
cosq sinq 0
-sinq cosq 0
0
0
1
0
0
0
0
0
0
1
8
– Net Transformation matrix
[x’ y’ z’ 1] = [x y z 1] M1
and
[x” y” z” 1] = [x’ y’ z’ 1] M2
then the transformation matrices can be concatenated
M3 = M1 M2
and
[x” y” z” 1] = [x y z 1] M3
– Matrix multiplication are not commutative
M1 M2 = M2 M1
9
Vectors
A vector is an entity that possesses magnitude
and direction.
A 3D vector is a triple :
– V = (v1, v2, v3), where each component vi is a scalar.
A ray (directed line segment), that possesses
position, magnitude and direction.
(x1,y1,z1)
V = (x2-x1, y2-y1, z2-z1)
(x2,y2,z2)
10
Addition of vectors
X = V + W
= (x1, y1, z1)
= (v1 + w1, v2 + w2, v3 + w3)
W
V + W
W
V
V + W
V
Length of vectors
|V| = (v12 + v22 + v32)1/2
U
= V / |V|
11
Cross product of vectors
– Definition
X = V X W
= (v2w3-v3w2)i + (v3w1-v1w3)j + (v1w2-v2w1)k
where i, j and k are standard unit vectors :
i = (1, 0, 0), j = (0, 1, 0), k = (0, 0, 1)
– Application
» A normal vector to a polygon is calculated from 3 (non-collinear)
vertices of the polygon.
Np
V2
polygon defined by 4 points
Np = V1 X V2
V1
12
» Normal vector transformation
N(X) = detJ J-1T N(x)
where X = F(x)
dF(x)
J the Jacobian matrix, Ji(x) =
dxi
"Global and Local Deformations of Solid Primitives"
Alan H. Barr
Computer Graphics Volume 18, Number 3 July 1984
(take scaling as example)
13
Dot product of vectors
– Definition
|X| = V . W
= v1w1 + v2w2 + v3w3
– Application
V
q
cosq =
W
V
.
W
|V||W|
14
Fixed Point Arithmetics (1/2)
Fixed Point Arithmetics : N bits (signed) Integer
– Example : N = 16 gives range –32768 ă 32767
– We can use fixed scale to get the decimals
a = ă / 28
8 integer bits
11
1
8 fractional bits
ă = 315, a = 1.2305
15
Fixed Point Arithmetics (2/2)
Multiplication then Requires Rescaling
e = a.c = ă / 28 . ĉ / 28
ĕ = (ă . ĉ) / 28
Addition just Like Normal
e = a+c = ă / 28 + ĉ / 28
ĕ=ă+ĉ
16
Fixed Point Arithmetics - Application
Compression for Floating-point Real Numbers
– 4 bits reduced to 2 bits
– Lost some accuracy but affordable
– Network data transfer
Software 3D Rendering
17
Triangular Coordinate System
ha
(xa,ya,za)
Ac p
hb
h
(xb,yb,zb)
Ab
Aa
hc (x ,y ,z )
c
h=
Aa
A
ha +
Ab
A
hb +
c
Ac
A
c
hc
where A = Aa + Ab + Ac
If (Aa < 0 || Ab < 0 || Ac < 0) than
the point is outside the triangle
“Triangular Coordinate System”
18
Triangle Area – 2D
Area of a triangle in 2D
A=½
xa ya
xb yb
xc yc
xa ya
= ½ (xa*yb + xb*yc + xc*ya – xb*ya – xc*yb – xa*yc)
(xa,ya,za)
(xb,yb,zb)
(xc,yc,zc)
19
Triangle Area – 3D
Area of a triangle in 3D
A = ½ (N. Sum(Pi1 cross Pi2))
where (i1, i2) = (a,b), (b,c), (c,a)
float GmArea3(float *x0, float *x1, float *x2, float *n)
{
float area, len, sum1, sum2, sum0;
len = (float) sqrt(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]) * 2.0f;
/* find sum of cross products */
sum0 = x1[1] * (-x0[2] + x2[2]) + x2[1] * (-x1[2] + x0[2]) +
x0[1] * (-x2[2] + x1[2]);
sum1 = x1[2] * (-x0[0] + x2[0]) + x2[2] * (-x1[0] + x0[0]) +
x0[2] * (-x2[0] + x1[0]);
sum2 = x1[0] * (-x0[1] + x2[1]) + x2[0] * (-x1[1] + x0[1]) +
x0[0] * (-x2[1] + x1[1]);
/* find the area */
return = (sum0 * n[0] + sum1 * n[1] + sum2 * n[2]) / len;
}
20
Triangular Coordinate System - Application
Terrain Following
Hit Test
Ray Cast
Collision Detection
21
Intersection
Ray Cast
Containment Test
22
Ray Cast – The Ray
Shot a Ray to Calculate the Intersection of the Ray
with Models
Use Parametric Equation for a Ray
{
x = x0 + (x1 – x0) t
y = y0 + (y1 – y0) t,
z = z0 + (z1 – z0) t
t = 0, 8
When t = 0, the Ray is on the Start Point (x0,y0,z0)
Only the t 0 is the Answer Candidate
The Smallest Positive t is the Answer
23
Ray Cast – The Plane
Each Triangle in the Models has its Plane Equation
Use ax + by + cz + d = 0 as the Plane Equation
(a, b, c) is the Plane Normal Vector
|d| is the Distance of the Plane to Origin
Substitute the Ray Equation into the Plane
Solve the t to Find the Intersect
Check the Intersect Point Within the Triangle or
not by Using “Triangle Area Test” (p. 154)
24
2D Containment Test
Intersection = 1, inside
Intersection = 2, outside
(x0, y0)
Intersection = 0, outside
Trick : Parametric equation for a ray which is parallel to the x-axis
x = x0 + t
{
y = y0
, t = 0, 8
“if the No. of intersection is odd, the point is inside,
otherwise, is outside”
25
3D Containment Test
Same as the 2D containment test
“if the No. of intersection is odd, the point is inside,
otherwise, is outside”
26
Euler Angles
A rotation is described as a sequence of rotations
about three mutually orthogonal coordinates axes
fixed in space
– X-roll, Y-roll, Z-roll
R(q1, q2, q3) represents an x-roll, followed by y-roll, followed by z-roll
R(q1, q2, q3) =
c 2c 3
c 2s 3
s1s2c3-c1s3 s1s2s3+c1c3
c1s2c3+s1s3 c1s2s3-s1c3
0
0
where si = sinqi and ci = cosqi
-s2
s 1c 2
c 1c 2
0
0
0
0
1
There are 6 possible ways to define a rotation
– 3!
27
Euler Angles & Interpolation
Interpolation happening on each angle
Multiple routes for interpolation
More keys for constrains
R
z
z
y
x
y
x
R
28
Angular Displacement
R(q, n), n is the rotation axis
rv
q
r
r Rr
rh
V = nxrv = nxr
n
n
rv
V
rh = (n.r)n
rv = r - (n.r)n , rotate into position Rrv
q
V
Rrv
Rrv = (cosq)rv + (sinq)V
->
Rr = Rrh + Rrv
= rh + (cosq)rv + (sinq)V
= (n.r)n + (cosq) (r - (n.r)n) + (sinq) nxr
= (cosq)r + (1-cosq) n (n.r) + (sinq) nxr
29
Quaternion
Sir William Hamilton (1843)
From Complex numbers (a + ib), i 2 = -1
16,October, 1843, Broome Bridge in Dublin
1 real + 3 imaginary = 1 quaternion
q = a + bi + cj + dk
i2 = j2 = k2 = -1
ij = k & ji = -k, cyclic permutation i-j-k-i
q = (s, v), where (s, v) = s + vxi + vyj + vzk
30
Quaternion Algebra
q1 = (s1, v1) and q2 = (s2, v2)
q3 = q1q2 = (s1s2 - v1.v2 , s1v2 + s2v1 + v1xv2)
Noncommutative
Conjugate of q = (s, v), q = (s, -v)
qq = s2 + |v|2 = |q|2
A unit quaternion q = (s, v), where qq = 1
A pure quaternion p = (0, v)
31
Quaternion VS Angular Displacement
Take a pure quaternion p = (0, r)
and a unit quaternion q = (s, v) where qq = 1
and define Rq(p) = qpq-1 where q-1 = q for a unit quaternion
Rq(p) = (0, (s2 - v.v)r + 2v(v.r) + 2svxr)
Let q = (cosq, sinq n),
|n| = 1
Rq(p) = (0, (cos2q - sin2q)r + 2sin2q n(n.r) + 2cosqsinq nxr)
= (0, cos2qr + (1 - cos2q)n(n.r) + sin2q nxr)
Conclusion :
The act of rotating a vector r by an angular displacement (q, n)
is the same as taking this displacement, ‘lifting’ it into
quaternion space, by using a unit quaternion (cos(q/2),
32
sin(q/2)n)
Quaternion VS Rotation Matrix
q = (w,x,y,z)
1-2y2-2z2 2xy-2wz 2xz+2wy
2xy+2wz 1-2x2-2z2 2yz-2wx
2xz-2wy 2yz+2wx 1-2x2-2y2
0
0
0
0
0
0
1
33
float tr, s;
tr = m[0] + m[4] + m[8];
if (tr > 0.0f) {
s = (float) sqrt(tr + 1.0f);
q->w = s/2.0f;
s = 0.5f/s;
q->x = (m[7] - m[5])*s;
q->y = (m[2] - m[6])*s;
q->z = (m[3] - m[1])*s;
M0 M1 M2 0
M3 M4 M5 0
M6 M7 M8 0
0 0 0 1
}
else {
float qq[4];
int i, j, k;
int nxt[3] = {1, 2, 0};
i = 0;
if (m[4] > m[0]) i = 1;
if (m[8] > m[i*3+i]) i = 2;
j = nxt[i]; k = nxt[j];
s = (float) sqrt((m[i*3+i] - (m[j*3+j] + m[k*3+k])) + 1.0f);
qq[i] = s*0.5f;
if (s != 0.0f) s = 0.5f/s;
qq[3] = (m[j+k*3] - m[k+j*3])*s;
qq[j] = (m[i+j*3] + m[j+i*3])*s;
qq[k] = (m[i+k*3] + m[k+i*3])*s;
q->w = qq[3];
q->x = qq[0];
q->y = qq[1];
q->z = qq[2];
}
34
Quaternion Interpolation
Spherical linear interpolation, slerp
A
P
B
t
f
slerp(q1, q2, t) = q1
sin((1 - t)f)
sinf
+ q2
sin(tf)
sinf
35
Differential Equation Basics
Initial value problems
ODE
– Ordinary differential equation
Numerical solutions
– Euler’s method
– The midpoint method
36
Initial Value Problems
An ODE
.
x = f (x, t)
where f is a known function
.
x is the state of the system, x is the x’s time derivative
.
x & x are vectors
x(t0) = x0, initial condition
Vector field
Solutions
– Symbolic solution
– Numerical solution
Start here
Follow the vectors …
37
Euler’s Method
x(t + Dt) = x(t) + Dt f(x, t)
A numerical solution
– A simplification from Tayler series
Discrete time steps starting with initial value
Simple but not accurate
– Bigger steps, bigger errors
– O(Dt2) errors
Can be unstable
Not even efficient
38
The Midpoint Method
Error term
.
..
2
Concept : x(t0 + h) = x(t0) + h x(t0) + h /2 x(t0) + O(h3)
Result : x(t0+h) = x(t0) + h(f(x0 + h/2 f(x0))
Method : a. Compute an Euler step
Dx = Dt f(x, t)
b. Evaluate f at the midpoint
fmid = f((x+Dx)/2, (t+Dt)/2)
c. Take a step using the midpoint
x(t+Dt) = x(t) + Dt fmid
c
b
a
39
The Runge-Kutta Method
Midpoint = Runge-Kutta method of order 2
Runge-Kutta method of order 4
– O(h5)
k1
k2
k3
k4
=
=
=
=
h
h
h
h
f(x0, t0)
f(x0 + k1/2, t0 + h/2)
f(x0 + k2/2, t0 + h/2)
f(x0 + k3, t0 + h)
x(t0+h) = x0 + 1/6 k1 + 1/3 k2 + 1/3 k3 + 1/6 k4
40
Initial Value Problems - Application
Dynamics
– Particle system
Game FX System
41
Game Geometry
42
Game Models
Geometry
– Position / vertex normals / texture coordinates
Topology
– Primitive
» Lines / triangles / surfaces / …
Property
– Materials
– Textures
Motion
Hierarchy
43
Geometry Data
Vertex position
– (x, y, z, w)
– In model space or screen spane
Vertex normal
– (nx, ny, nz)
Vertex color
– (r, g, b) or (diffuse, specular)
Texture coordinates on vertex
– (u1, v1), (u2, v2), …
Skin weights
– (bone1, w1, bone2, w2, …)
44
Topology Data
Lines
– Line segments
– Polyline
» Open / closed
Indexed triangles
Triangle Strips / Fans
Surfaces
– Non-uniform Rational B Spline (NURBS)
Subdivision
45
Indexed Triangles
Geometric data
Vertex data
v0, v1, v2, v3, …
(x, y, z, nx, ny, nz, tu, tv)
or (x, y, z, vr, vg, vb, tu, tv)
polygon normal
Topology
v0
Face v0 v3 v6 v7
Edge table
vertex normal
v7
v3
v6
Right-hand rule for indexing
46
Triangle Strips
v0
v2
v6
v4
T0
T4
T2
T1
T5
T3
v5
v1
v7
v3
v0 , v1 , v2 , v3 , v4 , v5 , v6 , v7
47