Download presentation
Presentation is loading. Please wait.
1
Game Mathematics
2
Fixed-point Real Numbers Triangle Mathematics Intersection Issues
Essential Mathematics for Game Development Matrices Vectors Fixed-point Real Numbers Triangle Mathematics Intersection Issues Euler Angles Angular Displacement Quaternion Differential Equation Basics
3
Matrices Matrix basics Definition Transpose Addition a11 .. a1n . .
am1 .. amn A = (aij) = C = A T cij = aji C = A + B cij = aij + bij
4
Scalar-matrix multiplication
Matrix-matrix multiplication C = aA cij = aaij r C = A B cij = Saikbkj k = 1 cij = row x column
5
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
6
Translation Transformation
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 Tx Ty Tz 1 [x’ y’ z’ 1] = [x y z 1] = [x y z 1] T
7
Scaling Transformation
x’ = xSx y’ = ySy z’ = zSz V’ = VS Sx 0 Sy Sz 0 [x’ y’ z’ 1] = [x y z 1] = [x y z 1] S Here Sx, Sy and Sz are scaling factors.
8
Rotation Transformations
0 cosq sinq 0 0 -sinq cosq 0 Rx = Ry = Rz = cosq 0 -sinq 0 sinq 0 cosq 0 cosq sinq 0 0 -sinq cosq 0 0
9
Net Transformation matrix
Matrix multiplication are not commutative [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 [x” y” z” 1] = [x y z 1] M3 M1 M2 = M2 M1
10
A vector is an entity that possesses magnitude and direction.
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)
11
Addition of vectors Length of vectors X = V + W = (x1, y1, z1)
= (v1 + w1, v2 + w2, v3 + w3) W V + W W V + W V V |V| = (v12 + v22 + v32)1/2 U = V / |V|
12
Cross product of vectors
Definition Application A normal vector to a polygon is calculated from 3 (non-collinear) vertices of the polygon. 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) Np polygon defined by 4 points V2 Np = V1 X V2 V1
13
Normal vector transformation
N(X) = detJ J-1T N(x) where X = F(x) J the Jacobian matrix, Ji(x) = dF(x) dxi "Global and Local Deformations of Solid Primitives" Alan H. Barr Computer Graphics Volume 18, Number 3 July 1984
14
Normal vector transformation: Example
Given plan: S = S(x, 0, z), x in [0, 1], z in [0, 1] Transform the plane to the curved surface of a half cylinder: x’ = r – r cos (x/r) y’ = r sin (x /r) z’ = z where r is the radius of the cylinder and r = 1/p. 14
15
Dot product of vectors Definition Application |X| = V . W
= v1w1 + v2w2 + v3w3 V q W V . W cosq = |V||W|
16
Fixed Point Arithmetics (1/2)
Fixed Point Arithmetics : N bits (signed) Integer Example : N = 16 gives range –32768 ai 32767 We can use fixed scale to get the decimals a = ai / 28 8 integer bits 1 1 1 8 fractional bits ai = 315, a =
17
Fixed Point Arithmetics (2/2)
Multiplication then Requires Rescaling Addition just Like Normal e = a.c = ai / 28 . ci / 28 ei = (ai . ci) / 28 e = a+c = ai / 28 + ci / 28 ei = ai + ci
18
Fixed Point Arithmetics - Application
Compression for Floating-point Real Numbers 4 bytes reduced to 2 bytes Lost some accuracy but affordable Network data transfer Software 3D Rendering
19
Triangular Coordinates
ha (xa,ya,za) Ac p hb Ab h (xb,yb,zb) Aa hc (xc,yc,zc) Aa Ab Ac h = ha hb hc where A = Aa + Ab + Ac If (Aa < 0 || Ab < 0 || Ac < 0) then the point is outside the triangle “Triangular Coordinates” A A A
20
Triangle Area – 2D Area of a triangle in 2D xa ya A = ½ xb yb xc yc
= ½ (xa*yb + xb*yc + xc*ya – xb*ya – xc*yb – xa*yc) (xa,ya,za) (xb,yb,zb) (xc,yc,zc)
21
Triangle Area – 3D p0 u p1 v p2 u = p1 – p0 v = p2 – p0
Area of a triangle in 3D p0 (x0,y0,z0) u p1 (x1,y1,z1) v (x2,y2,z2) p2 u = p1 – p0 v = p2 – p0 area = ½ |u x v | How to use triangular coordinates to determine the location of a point? Area is always positive!
22
Triangular Coordinate System - Application
Terrain Following Hit Test Ray Cast Collision Detection
23
Intersection Ray Cast Containment Test
24
Ray Cast – The Ray x = x0 + (x1 – x0) t y = y0 + (y1 – y0) t, t = 0, z = z0 + (z1 – z0) t { Shoot a ray to calculate the intersection between the ray and models Use a parametric equation to represent a ray 8 The ray emits from (x0,y0,z0) Only the t 0 is the answer candidate The smallest positive t is the answer
25
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 equation Solve t for finding the intersection Check whether or not the intersect point insider the triangle
26
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 = y , t = 0, { 8 “if the No. of intersection is odd, the point is inside, otherwise, it is outside”
27
Same as the 2D containment test
“if the No. of intersection is odd, the point is inside, otherwise, is outside”
28
Rotation vs Orientation
Orientation: relative to a reference alignment Rotation: change object from one orientation to another Represent an orientation as a rotation from the reference alignment
29
There are 6 possible ways to define a rotation
Euler Angles A rotation is described as a sequence of rotations about three mutually orthogonal coordinates axes fixed in space (e.g. world coordinate system) X-roll, Y-roll, Z-roll There are 6 possible ways to define a rotation 3! R(q1, q2, q3) represents an x-roll, followed by y-roll, followed by z-roll R(q1, q2, q3) = c2c c2s s s1s2c3-c1s3 s1s2s3+c1c3 s1c2 0 c1s2c3+s1s3 c1s2s3-s1c3 c1c2 0 where si = sinqi and ci = cosqi Left hand system
30
Euler Angles & Interpolation
Interpolation happening on each angle Multiple routes for interpolation More keys for constraints Can lead to gimbal lock R z z y y x x R
31
R(q, n), n is the rotation axis
Angular Displacement R(q, n), n is the rotation axis rh = (n.r)n rv = r - (n.r)n , rotate into position Rrv V = nxrv = nxr 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 rv V q r r Rr rh n n V rv q Rrv
32
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
33
Quaternion Algebra q1 = (s1, v1) and q2 = (s2, v2)
q3 = q1q2 = (s1s2 - v1.v2 , s1v2 + s2v1 + v1xv2) 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) Noncommutative
34
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), sin(q/2)n)
35
Conversion: Quaternion to Matrix
1-2y2-2z2 2xy-2wz 2xz+2wy 0 2xy+2wz x2-2z2 2yz-2wx 0 2xz-2wy 2yz+2wx 1-2x2-2y2 0 q = (w,x,y,z)
36
Conversion: Matrix to Quaternion
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; } 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]; M0 M1 M2 0 M3 M4 M5 0 M6 M7 M8 0
37
Quaternion Interpolation
Spherical linear interpolation, slerp A P t B f sin((1 - t)f) sin(tf) slerp(q1, q2, t) = q q2 sinf sinf
38
Differential Equation Basics
Initial value problems ODE Ordinary differential equation Numerical solutions Euler’s method The midpoint method Kunge –Kutta methods
39
Initial Value Problems
An ODE Vector field Solutions Symbolic solution Numerical solution . 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 . . Start here Follow the vectors …
40
Discrete time steps starting with initial value
Euler’s Method x(t + h) = x(t) + h 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 Step error: O(h2) errors Total error: O(h) Can be unstable Not efficient
41
The Midpoint Method Error term . ..
Concept : x(t0 + h) = x(t0) + h x(t0) + h2/2 x(t0) + O(h3) Result : x(t0 + h) = x(t0) + h[ f (x0 + h/2 f(x0 , t0), t0 +h/2) ] Method : a. Compute an Euler step Dx = h f(x0 , t0) b. Evaluate f at the midpoint fmid = f ( x0+Dx/2, t0 +h/2 ) c. Take a step using the midpoint x( t+ h) = x(t) + h fmid c b a
42
The Runge-Kutta Method
Midpoint = Runge-Kutta method of order 2 Runge-Kutta method of order 4 Step error: O(h5) Total error: O(h4) k1 = h f(x0, t0) k2 = h f(x0 + k1/2, t0 + h/2) k3 = h f(x0 + k2/2, t0 + h/2) k4 = h f(x0 + k3, t0 + h) x(t0+h) = x0 + 1/6 k1 + 1/3 k2 + 1/3 k3 + 1/6 k4
43
Initial Value Problems - Application
Dynamics Particle system Game FX System
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.