1 / 47

Game Mathematics

Game Mathematics. Essential Mathematics for Game Development. Matrices Vectors Fixed-point Real Numbers Triangle Mathematics Intersection Issues Euler Angles Angular Displacement Quaternion Differential Equation Basics. Matrices. a 11 .. a 1n . . . . a m1 .. a mn.

garran
Download Presentation

Game Mathematics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Game Mathematics

  2. 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 a11 .. a1n . . . . am1 .. amn • Matrix basics • Definition • Transpose • Addition 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

  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. 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 1 0 0 0 0 1 0 0 0 0 1 0 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 0 0 0 Sy 0 0 0 0 Sz 0 0 0 0 1 [x’ y’ z’ 1] = [x y z 1] = [x y z 1] S Here Sx, Sy and Sz are scaling factors.

  8. Rotation Transformations 1 0 0 0 0 cosq sinq 0 0 -sinq cosq 0 0 0 0 1 Rx = Ry = Rz = cosq 0 -sinq 0 0 1 0 0 sinq 0 cosq 0 0 0 0 1 cosq sinq 0 0 -sinq cosq 0 0 0 0 1 0 0 0 0 1

  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 and [x” y” z” 1] = [x y z 1] M3 M1 M2 = M2 M1

  10. 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. V = (x2-x1, y2-y1, z2-z1) (x1,y1,z1) (x2,y2,z2)

  11. X = V + W • = (x1, y1, z1) • = (v1 + w1, v2 + w2, v3 + w3) • Addition of vectors • Length of vectors W V + W W V + W V V |V| = (v12 + v22 + v32)1/2 U = V / |V|

  12. X = VXW • = (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) • Cross product of vectors • Definition • Application • A normal vector to a polygon is calculated from 3 (non-collinear) vertices of the polygon. Np polygon defined by 4 points V2 • Np = V1XV2 V1

  13. N(X) = detJJ-1TN(x) where X = F(x) Jthe Jacobian matrix,Ji(x) = • Normal vector transformation dF(x) dxi "Global and Local Deformations of Solid Primitives" Alan H. Barr Computer Graphics Volume 18, Number 3 July 1984 • (take scaling as example)

  14. Dot product of vectors • Definition • Application • |X| = V.W • = v1w1 + v2w2+v3w3 V q W V.W cosq = |V||W|

  15. 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 1 1 1 8 fractional bits ă = 315, a = 1.2305

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

  17. 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

  18. Triangular Coordinate System ha (xa,ya,za) Ac p Ab hb 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) than the point is outside the triangle “Triangular Coordinate System” A A A

  19. Triangle Area – 2D Area of a triangle in 2D xa ya A = ½ 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)

  20. 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; }

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

  22. Intersection • Ray Cast • Containment Test

  23. 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, t = 0, z = z0 + (z1 – z0) t { 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

  24. 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)

  25. 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”

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

  27. 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 • 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) = c2c3 c2s3 -s2 0 s1s2c3-c1s3 s1s2s3+c1c3 s1c2 0 c1s2c3+s1s3 c1s2s3-s1c3 c1c2 0 0 0 0 1 where si = sinqi and ci = cosqi

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

  29. 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 V rv q r r Rr rh n n V rv q Rrv

  30. 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

  31. 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

  32. 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 + 2sin2qn(n.r) + 2cosqsinqnxr) = (0, cos2qr + (1 - cos2q)n(n.r) + sin2qnxr) 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)

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

  34. 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 0 0 0 1

  35. Quaternion Interpolation • Spherical linear interpolation, slerp A P B t f sin((1 - t)f) sin(tf) slerp(q1, q2, t) = q1 + q2 sinf sinf

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

  37. Initial Value Problems • An ODE • Vector field • Solutions • Symbolic solution • Numerical solution . x = f (x, t) where fis 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 . . Follow the vectors … Start here

  38. 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

  39. 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)) 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

  40. The Runge-Kutta Method • Midpoint = Runge-Kutta method of order 2 • Runge-Kutta method of order 4 • O(h5) 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

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

  42. Game Geometry

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

  44. 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, …)

  45. Topology Data • Lines • Line segments • Polyline • Open / closed • Indexed triangles • Triangle Strips / Fans • Surfaces • Non-uniform Rational BSpline (NURBS) • Subdivision

  46. 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) • Topology • Face v0 v3 v6 v7 • Edge table polygon normal vertex normal v0 v7 v3 v6 Right-hand rule for indexing

  47. Triangle Strips v0 v6 v2 v4 T0 T4 T2 T5 T3 T1 v5 v1 v7 v3 v0 , v1 ,v2 ,v3 ,v4 ,v5 ,v6 ,v7

More Related