Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Game Mathematics. 2 Matrices Matrices Vectors Vectors Fixed-point Real Numbers Fixed-point Real Numbers Triangle Mathematics Triangle Mathematics Intersection.

Similar presentations


Presentation on theme: "1 Game Mathematics. 2 Matrices Matrices Vectors Vectors Fixed-point Real Numbers Fixed-point Real Numbers Triangle Mathematics Triangle Mathematics Intersection."— Presentation transcript:

1 1 Game Mathematics

2 2 Matrices Matrices Vectors Vectors Fixed-point Real Numbers Fixed-point Real Numbers Triangle Mathematics Triangle Mathematics Intersection Issues Intersection Issues Euler Angles Euler Angles Angular Displacement Angular Displacement Quaternion Quaternion Differential Equation Basics Differential Equation Basics Essential Mathematics for Game Development

3 3 Matrix basics Matrix basics –Definition –Transpose –Addition Matrices A = (a ij ) = a 11.. a 1n.. a m1.. a mn C = A T c ij = a ji C = A + B c ij = a ij + b ij

4 4 –Scalar-matrix multiplication –Matrix-matrix multiplication C =  A c ij =  a ij C = A B c ij =  a ik b kj k = 1 r

5 5 Transformations in Matrix form 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 6 –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 + T x y’ = y + T y z’ = z + T z V’ = VT [x ’ y ’ z ’ 1] = [x y z 1] = [x y z 1] T 1 0 0 0 0 1 0 0 0 0 1 0 T x T y T z 1

7 7 –Scaling Transformation x’ = xS x y’ = yS y z’ = zS z V’ = VS [x ’ y ’ z ’ 1] = [x y z 1] = [x y z 1] S S x 0 0 0 0 S y 0 0 0 0 S z 0 0 0 0 1 Here S x, S y and S z are scaling factors.

8 8 –Rotation Transformations 1 0 0 0 0 cos  sin  0 0 -sin  cos  0 0 0 0 1 R x = R y = R z = cos  0 -sin  0 0 1 0 0 sin  0 cos  0 0 0 0 1 cos  sin  0 0 -sin  cos  0 0 0 0 1 0 0 0 0 1

9 9 –Net Transformation matrix –Matrix multiplication are not commutative [x ’ y ’ z ’ 1] = [x y z 1] M 1 and [x” y” z” 1] = [x’ y’ z’ 1] M 2 then the transformation matrices can be concatenated M 3 = M 1 M 2 and [x” y” z” 1] = [x y z 1] M 3 M 1 M 2 = M 2 M 1

10 10 A vector is an entity that possesses magnitude and direction. A vector is an entity that possesses magnitude and direction. A 3D vector is a triple : A 3D vector is a triple : –V = (v 1, v 2, v 3 ), where each component v i is a scalar. A ray (directed line segment), that possesses position, magnitude and direction. A ray (directed line segment), that possesses position, magnitude and direction.Vectors (x 1,y 1,z 1 ) (x 2,y 2,z 2 ) V = (x 2 -x 1, y 2 -y 1, z 2 -z 1 )

11 11 Addition of vectors Addition of vectors Length of vectors Length of vectors X = V + W = (x 1, y 1, z 1 ) = (v 1 + w 1, v 2 + w 2, v 3 + w 3 ) V W V + W V W |V| = (v 1 2 + v 2 2 + v 3 2 ) 1/2 U = V / |V|

12 12 Cross product of vectors 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 = (v 2 w 3 -v 3 w 2 )i + (v 3 w 1 -v 1 w 3 )j + (v 1 w 2 -v 2 w 1 )k where i, j and k are standard unit vectors : i = (1, 0, 0), j = (0, 1, 0), k = (0, 0, 1) NpNp V2V2 V1V1 polygon defined by 4 points N p = V 1 X V 2

13 13 »Normal vector transformation N(X) = detJ J -1T N(x) where X = F(x) J the Jacobian matrix, J i (x) =  F(x) xixi (take scaling as example) "Global and Local Deformations of Solid Primitives" Alan H. Barr Computer Graphics Volume 18, Number 3 July 1984

14 14 Dot product of vectors Dot product of vectors –Definition –Application |X| = V. W = v 1 w 1 + v 2 w 2 + v 3 w 3  V W cos  = V. WV. W |V||W|

15 15 Fixed Point Arithmetics : N bits (signed) Integer Fixed Point Arithmetics : N bits (signed) Integer –Example : N = 16 gives range –32768  ă  32767 –We can use fixed scale to get the decimals Fixed Point Arithmetics (1/2) a = ă / 2 8 111 8 integer bits 8 fractional bits ă = 315, a = 1.2305

16 16 Multiplication then Requires Rescaling Multiplication then Requires Rescaling Addition just Like Normal Addition just Like Normal Fixed Point Arithmetics (2/2) e = a. c = ă / 2 8. ĉ / 2 8  ĕ = (ă. ĉ) / 2 8 e = a+c = ă / 2 8 + ĉ / 2 8  ĕ = ă + ĉ

17 17 Compression for Floating-point Real Numbers Compression for Floating-point Real Numbers –4 bits reduced to 2 bits –Lost some accuracy but affordable –Network data transfer Software 3D Rendering Software 3D Rendering Fixed Point Arithmetics - Application

18 18 h haha hbhb hchc AaAa AcAc AbAb h = h a + h b + h c where A = A a + A b + A c If (A a < 0 || A b < 0 || A c < 0) than the point is outside the triangle “Triangular Coordinate System” AaAa AbAb AcAc A A A p (x a,y a,z a ) (x b,y b,z b ) (x c,y c,z c ) Triangular Coordinate System

19 19 Area of a triangle in 2D x a y a A = ½ x b y b x c y c x a y a = ½ (x a *y b + x b *y c + x c *y a – x b *y a – x c *y b – x a *y c ) Triangle Area – 2D (x a,y a,z a ) (x b,y b,z b ) (x c,y c,z c )

20 20 Area of a triangle in 3D A = ½ (N. Sum(P i1 cross P i2 )) where (i1, i2) = (a,b), (b,c), (c,a) Triangle Area – 3D 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; }

21 21 Terrain Following Terrain Following Hit Test Hit Test Ray Cast Ray Cast Collision Detection Collision Detection Triangular Coordinate System - Application

22 22 Ray Cast Ray Cast Containment Test Containment TestIntersection

23 23 Ray Cast – The Ray x = x 0 + (x 1 – x 0 ) t y = y 0 + (y 1 – y 0 ) t, t = 0, z = z 0 + (z 1 – z 0 ) t { Shot a Ray to Calculate the Intersection of the Ray with Models Shot a Ray to Calculate the Intersection of the Ray with Models Use Parametric Equation for a Ray Use Parametric Equation for a Ray 8 When t = 0, the Ray is on the Start Point (x 0,y 0,z 0 ) When t = 0, the Ray is on the Start Point (x 0,y 0,z 0 ) Only the t  0 is the Answer Candidate Only the t  0 is the Answer Candidate The Smallest Positive t is the Answer The Smallest Positive t is the Answer

24 24 Ray Cast – The Plane Each Triangle in the Models has its Plane Equation Each Triangle in the Models has its Plane Equation Use ax + by + cz + d = 0 as the Plane Equation Use ax + by + cz + d = 0 as the Plane Equation (a, b, c) is the Plane Normal Vector (a, b, c) is the Plane Normal Vector |d| is the Distance of the Plane to Origin |d| is the Distance of the Plane to Origin Substitute the Ray Equation into the Plane Substitute the Ray Equation into the Plane Solve the t to Find the Intersect Solve the t to Find the Intersect Check the Intersect Point Within the Triangle or not by Using “Triangle Area Test” (p. 154) Check the Intersect Point Within the Triangle or not by Using “Triangle Area Test” (p. 154)

25 25 Intersection = 1, inside Intersection = 2, outside Intersection = 0, outside Trick : Parametric equation for a ray which is parallel to the x-axis x = x 0 + t y = y 0, t = 0, { 8 (x 0, y 0 ) 2D Containment Test “if the No. of intersection is odd, the point is inside, otherwise, is outside”

26 26 3D Containment Test “if the No. of intersection is odd, the point is inside, otherwise, is outside” Same as the 2D containment test Same as the 2D containment test

27 27 A rotation is described as a sequence of rotations about three mutually orthogonal coordinates axes fixed in space A rotation is described as a sequence of rotations about three mutually orthogonal coordinates axes fixed in space –X-roll, Y-roll, Z-roll There are 6 possible ways to define a rotation There are 6 possible ways to define a rotation –3! R(  1,  2,   ) represents an x-roll, followed by y-roll, followed by z-roll R(  1,  2,    c 2 c 3 c 2 s 3 -s 2 0 s 1 s 2 c 3 -c 1 s 3 s 1 s 2 s 3 +c 1 c 3 s 1 c 2 0 c 1 s 2 c 3 +s 1 s 3 c 1 s 2 s 3 -s 1 c 3 c 1 c 2 0 0 0 0 1 where s i = sin  i and c i = cos  i Euler Angles

28 28 Interpolation happening on each angle Interpolation happening on each angle Multiple routes for interpolation Multiple routes for interpolation More keys for constrains More keys for constrains z x y R z x y R Euler Angles & Interpolation

29 29 R( , n), n is the rotation axis R( , n), n is the rotation axis n rRr  n r rvrv rhrh V  rvrv V Rr v r h = (n. r)n r v = r - (n. r)n, rotate into position Rr v V = nxr v = nxr Rr v = (cos  )r v + (sin  )V -> Rr = Rr h + Rr v = r h + (cos  )r v + (sin  )V = (n. r)n + (cos  )  r - (n. r)n) + (sin  ) nxr = (cos  )r + (1-cos  ) n (n. r) + (sin  ) nxr Angular Displacement

30 30 Sir William Hamilton (1843) Sir William Hamilton (1843) From Complex numbers (a + ib), i 2 = -1 From Complex numbers (a + ib), i 2 = -1 16,October, 1843, Broome Bridge in Dublin 16,October, 1843, Broome Bridge in Dublin 1 real + 3 imaginary = 1 quaternion 1 real + 3 imaginary = 1 quaternion q = a + bi + cj + dk q = a + bi + cj + dk i 2 = j 2 = k 2 = -1 i 2 = j 2 = k 2 = -1 ij = k & ji = -k, cyclic permutation i-j-k-i ij = k & ji = -k, cyclic permutation i-j-k-i q = (s, v), where (s, v) = s + v x i + v y j + v z k q = (s, v), where (s, v) = s + v x i + v y j + v z kQuaternion

31 31 q 1 = (s 1, v 1 ) and q 2 = (s 2, v 2 ) q 3 = q 1 q 2 = (s 1 s 2 - v 1. v 2, s 1 v 2 + s 2 v 1 + v 1 xv 2 ) Conjugate of q = (s, v), q = (s, -v) qq = s 2 + |v| 2 = |q| 2 A unit quaternion q = (s, v), where qq = 1 A pure quaternion p = (0, v) Noncommutative Quaternion Algebra

32 32 Take a pure quaternion p = (0, r) and a unit quaternion q = (s, v) where qq = 1 and define R q (p) = qpq -1 where q -1 = q for a unit quaternion R q (p) = (0, (s 2 - v. v)r + 2v(v. r) + 2svxr) Let q = (cos , sin  n), |n| = 1 R q (p) = (0, (cos 2  - sin 2  )r + 2sin 2  n(n. r) + 2cos  sin  nxr) = (0, cos2  r + (1 - cos2  )n(n. r) + sin2  nxr) Conclusion : The act of rotating a vector r by an angular displacement ( , n) is the same as taking this displacement, ‘lifting’ it into quaternion space, by using a unit quaternion (cos(  /2), sin(  /2)n) Quaternion VS Angular Displacement

33 33 1-2y 2 -2z 2 2xy-2wz 2xz+2wy 0 2xy+2wz 1-2x 2 -2z 2 2yz-2wx 0 2xz-2wy 2yz+2wx 1-2x 2 -2y 2 0 0 0 0 1 q = (w,x,y,z) Quaternion VS Rotation Matrix

34 34 M 0 M 1 M 2 0 M 3 M 4 M 5 0 M 6 M 7 M 8 0 0 0 0 1 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]; }

35 35 Spherical linear interpolation, slerp Spherical linear interpolation, slerp A B P  t slerp(q 1, q 2, t) = q 1 + q 2 sin((1 - t)  ) sin  sin(t  ) Quaternion Interpolation

36 36 Initial value problems Initial value problems ODE ODE –Ordinary differential equation Numerical solutions Numerical solutions –Euler’s method –The midpoint method Differential Equation Basics

37 37 An ODE An ODE Vector field Vector field Solutions Solutions –Symbolic solution –Numerical solution x = f (x, t) wheref is a known function x is the state of the system, x is the x’s time derivative x & x are vectors x(t 0 ) = x 0, initial condition. Start here Follow the vectors … Initial Value Problems..

38 38 A numerical solution A numerical solution –A simplification from Tayler series Discrete time steps starting with initial value Discrete time steps starting with initial value Simple but not accurate Simple but not accurate –Bigger steps, bigger errors –O(  t 2 ) errors Can be unstable Can be unstable Not even efficient Not even efficient x(t +  t) = x(t) +  t f(x, t) Euler’s Method

39 39 Concept :x(t 0 + h) = x(t 0 ) + h x(t 0 ) + h 2 /2 x(t 0 ) + O(h 3 ) Result : x(t 0 +h) = x(t 0 ) + h(f(x 0 + h/2 f(x 0 )) Method :a. Compute an Euler step  x =  t f(x, t) b. Evaluate f at the midpoint f mid = f((x+  x)/2, (t+  t)/2) c. Take a step using the midpoint x(t+  t) = x(t) +  t f mid... a b c Error term The Midpoint Method

40 40 Midpoint = Runge-Kutta method of order 2 Midpoint = Runge-Kutta method of order 2 Runge-Kutta method of order 4 Runge-Kutta method of order 4 –O(h 5 ) k 1 = h f(x 0, t 0 ) k 2 = h f(x 0 + k 1 /2, t 0 + h/2) k 3 = h f(x 0 + k 2 /2, t 0 + h/2) k 4 = h f(x 0 + k 3, t 0 + h) x(t 0 +h) = x 0 + 1/6 k 1 + 1/3 k 2 + 1/3 k 3 + 1/6 k 4 The Runge-Kutta Method

41 41 Dynamics Dynamics –Particle system Game FX System Game FX System Initial Value Problems - Application

42 42 Game Geometry

43 43 Geometry Geometry –Position / vertex normals / texture coordinates Topology Topology –Primitive »Lines / triangles / surfaces / … Property Property –Materials –Textures Motion Motion Hierarchy Hierarchy Game Models

44 44 Vertex position Vertex position –(x, y, z, w) –In model space or screen spane Vertex normal Vertex normal –(n x, n y, n z ) Vertex color Vertex color –(r, g, b) or (diffuse, specular) Texture coordinates on vertex Texture coordinates on vertex –(u 1, v 1 ), (u 2, v 2 ), … Skin weights Skin weights –(bone 1, w 1, bone 2, w 2, …) Geometry Data

45 45 Lines Lines –Line segments –Polyline »Open / closed Indexed triangles Indexed triangles Triangle Strips / Fans Triangle Strips / Fans Surfaces Surfaces –Non-uniform Rational B Spline (NURBS) Subdivision Subdivision Topology Data

46 46 Geometric data Vertex data v 0, v 1, v 2, v 3, … (x, y, z, n x, n y, n z, t u, t v ) or (x, y, z, v r, v g, v b, t u, t v ) Topology Face v 0 v 3 v 6 v 7 Edge table v0v0 v3v3 v6v6 v7v7 Right-hand rule for indexing polygon normal vertex normal Indexed Triangles

47 47 v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 T0T0 T1T1 T2T2 T3T3 T4T4 T5T5 v 0, v 1, v 2, v 3, v 4, v 5, v 6, v 7 Triangle Strips


Download ppt "1 Game Mathematics. 2 Matrices Matrices Vectors Vectors Fixed-point Real Numbers Fixed-point Real Numbers Triangle Mathematics Triangle Mathematics Intersection."

Similar presentations


Ads by Google