430 likes | 452 Views
Learn key mathematical concepts for game development, including matrices, vectors, fixed-point arithmetic, real numbers, intersection issues, Euler angles, quaternion, differential equations, and geometry. Explore matrix basics, transformations, matrix multiplication, vectors, and ray-casting techniques. Understand fixed-point arithmetic for efficient computation. Master triangular coordinates and collision detection methods for immersive game development.
E N D
Essential Mathematics for Game Development Matrices Vectors Fixed-point Real Numbers Triangle Mathematics Intersection Issues Euler Angles Angular Displacement Quaternion Differential Equation Basics
Matrices a11 .. a1n . . . . am1 .. amn A = (aij) = C = A T cij = aji C = A + B cij = aij + bij • Matrix basics • Definition • Transpose • Addition
C = aA cij = aaij r C = A B cij = Saikbkj k = 1 cij = row x column • Scalar-matrix multiplication • Matrix-matrix multiplication
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 • Transformations in Matrix form • A point or a vector is a row matrix (de facto convention)
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
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.
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
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
Vectors V = (x2-x1, y2-y1, z2-z1) (x1,y1,z1) (x2,y2,z2) • 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.
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| Addition of vectors Length of vectors
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) Np polygon defined by 4 points V2 • Np = V1XV2 V1 • Cross product of vectors • Definition • Application • A normal vector to a polygon is calculated from 3 (non-collinear) vertices of the polygon.
N(X) = detJJ-1TN(x) where X = F(x) Jthe 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 • Normal vector transformation
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
|X| = V.W • = v1w1 + v2w2+v3w3 V q W V.W cosq = |V||W| • Dot product of vectors • Definition • Application
Fixed Point Arithmetics (1/2) a = ai / 28 8 integer bits 1 1 1 8 fractional bits ai = 315, a = 1.2305 • Fixed Point Arithmetics : N bits (signed) Integer • Example : N = 16 gives range –32768 ai 32767 • We can use fixed scale to get the decimals
Fixed Point Arithmetics (2/2) e = a.c = ai / 28 . ci/ 28 ei = (ai. ci) / 28 e = a+c = ai / 28 +ci/ 28 ei = ai + ci Multiplication then Requires Rescaling Addition just Like Normal
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
Triangular Coordinates 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) then the point is outside the triangle “Triangular Coordinates” A A A
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)
Triangle Area – 3D 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 = ½ |uxv| How to use triangular coordinates to determine the location of a point? Area is always positive!
Triangular Coordinate System - Application Terrain Following Hit Test Ray Cast Collision Detection
Intersection Ray Cast Containment Test
Ray Cast – The Ray • Shoot a ray to calculate the intersection between the ray and models • Use a parametric equation to represent a ray x = x0 + (x1 – x0) t y = y0 + (y1 – y0) t, t = 0, z = z0 + (z1 – z0) t { 8 • The ray emits from (x0,y0,z0) • Only the t 0 is the answer candidate • The smallest positive t is the answer
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
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, it is outside”
3D Containment Test • Same as the 2D containment test “if the No. of intersection is odd, the point is inside, otherwise, is outside”
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
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) = 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 Left hand system
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
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
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
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
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)
Conversion: Quaternion to 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)
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 0 0 0 1 http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
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
Differential Equation Basics • Initial value problems • ODE • Ordinary differential equation • Numerical solutions • Euler’s method • The midpoint method • Kunge –Kutta methods
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
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
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
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
Initial Value Problems - Application http://upload.wikimedia.org/wikipedia/en/4/44/Strand_Emitter.jpg • Dynamics • Particle system • Game FX System