Game Mathematics.

Slides:



Advertisements
Similar presentations
Today Composing transformations 3D Transformations
Advertisements

Vectors, Points, Lines and Planes Jim Van Verth Lars M. Bishop
Vector Calculus Mengxia Zhu Fall Objective Review vector arithmetic Distinguish points and vectors Relate geometric concepts to their algebraic.
John C. Hart CS 318 Interactive Computer Graphics
Informationsteknologi Wednesday, November 7, 2007Computer Graphics - Class 51 Today’s class Geometric objects and transformations.
Chapter 4.1 Mathematical Concepts
Computer Graphics Recitation 2. 2 The plan today Learn about rotations in 2D and 3D. Representing rotations by quaternions.
Chapter 4.1 Mathematical Concepts. 2 Applied Trigonometry Trigonometric functions Defined using right triangle  x y h.
3D Coordinate Systems and Transformations Revision 1
CSCE 590E Spring 2007 Basic Math By Jijun Tang. Applied Trigonometry Trigonometric functions  Defined using right triangle  x y h.
3D orientation.
Computer Graphics CSC 630 Lecture 2- Linear Algebra.
CSCE 689: Computer Animation Rotation Representation and Interpolation
CSCE 441: Computer Graphics Rotation Representation and Interpolation
CSCE 641: Computer Graphics Rotation Representation and Interpolation Jinxiang Chai.
Vectors.
Linear Algebra Review CSE169: Computer Animation Instructor: Steve Rotenberg UCSD, Winter 2005.
CS 445 / 645 Introduction to Computer Graphics Lecture 21 Representing Rotations Lecture 21 Representing Rotations.
One Mark Questions. Choose the Correct Answer 1.If p is true and q is false then which of the following is not true? (a) ~ p is true (b) p  q is true.
2IV60 Computer Graphics Basic Math for CG
Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.
Mathematical Fundamentals
Computer Graphics: Programming, Problem Solving, and Visual Communication Steve Cunningham California State University Stanislaus and Grinnell College.
UNIVERSITI MALAYSIA PERLIS
Rotations and Translations
COS 397 Computer Graphics Svetla Boytcheva AUBG, Spring 2013.
CS 450: Computer Graphics REVIEW: OVERVIEW OF POLYGONS
Game Physics – Part IV Moving to 3D
Graphics CSE 581 – Interactive Computer Graphics Mathematics for Computer Graphics CSE 581 – Roger Crawfis (slides developed from Korea University slides)
1 February 24 Matrices 3.2 Matrices; Row reduction Standard form of a set of linear equations: Chapter 3 Linear Algebra Matrix of coefficients: Augmented.
Mathematics for Computer Graphics (Appendix A) Won-Ki Jeong.
Mathematical Topics Review of some concepts:  trigonometry  aliasing  coordinate systems  homogeneous coordinates  matrices, quaternions.
Chemistry 330 The Mathematics Behind Quantum Mechanics.
CS 450: COMPUTER GRAPHICS QUATERNIONS SPRING 2015 DR. MICHAEL J. REALE.
Computer Graphics Bing-Yu Chen National Taiwan University.
Mathematical Foundations Sections A-1 to A-5 Some of the material in these slides may have been adapted from university of Virginia, MIT and Åbo Akademi.
Rotations and Translations
CSE 681 Review: Transformations. CSE 681 Transformations Modeling transformations build complex models by positioning (transforming) simple components.
Mathematics for Graphics. 1 Objectives Introduce the elements of geometry  Scalars  Vectors  Points Develop mathematical operations among them in a.
CS 376 Introduction to Computer Graphics 02 / 16 / 2007 Instructor: Michael Eckmann.
1 Game Mathematics. 2 Matrices Matrices Vectors Vectors Fixed-point Real Numbers Fixed-point Real Numbers Triangle Mathematics Triangle Mathematics Intersection.
Character Motion 王銓彰 ■ We use Cartesian coordinate system ( 笛卡兒座標系 ) for 3D. ■ We use right-hand rule for axis orientation. ■ x.
University of British Columbia CPSC 414 Computer Graphics © Tamara Munzner 1 Rotations and Quaternions Week 9, Wed 29 Oct 2003.
Fundamentals of Computer Graphics Sai-Keung Wong National Chiao Tung University Department of Computer Science Date: 5 March References: wikipedia,
Lecture Fall 2001 Physically Based Animation Ordinary Differential Equations Particle Dynamics Rigid-Body Dynamics Collisions.
Geometric Objects and Transformation
Animating Rotations and Using Quaternions. What We’ll Talk About Animating Translation Animating 2D Rotation Euler Angle representation 3D Angle problems.
16/5/ :47 UML Computer Graphics Conceptual Model Application Model Application Program Graphics System Output Devices Input Devices API Function.
Matthew Christian. About Me Introduction to Linear Algebra Vectors Matrices Quaternions Links.
Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.
CS 551/645 Fall 2000 Parameterized Rotations, Curves, and Surfaces.
Animating with Quaternions
Euler Angles This means, that we can represent an orientation with 3 numbers Assuming we limit ourselves to 3 rotations without successive rotations about.
Week 5 - Monday.  What did we talk about last time?  Lines and planes  Trigonometry  Transforms  Affine transforms  Rotation  Scaling  Shearing.
Computer Graphics Mathematical Fundamentals Lecture 10 Taqdees A. Siddiqi
Computer Graphics Lecture 11 2D Transformations I Taqdees A. Siddiqi
Chapter 4.1 Mathematical Concepts. 2 Applied Trigonometry "Old Henry And His Old Aunt" Defined using right triangle  x y h.
Fundamentals of Computer Animation Orientation and Rotation.
Graphics Graphics Korea University kucg.korea.ac.kr Mathematics for Computer Graphics 고려대학교 컴퓨터 그래픽스 연구실.
REFERENCE: QUATERNIONS/ Ogre Transformations.
Math Fundamentals Maths revisit.
CPSC 641: Computer Graphics Rotation Representation and Interpolation
CS 445 / 645 Introduction to Computer Graphics
Review: Transformations
Review: Transformations
Rotations and Quaternions Week 9, Wed 29 Oct 2003
UMBC Graphics for Games
A definition we will encounter: Groups
Math review - scalars, vectors, and matrices
Presentation transcript:

Game Mathematics

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

Matrices Matrix basics Definition Transpose Addition a11 .. a1n . . . . am1 .. amn A = (aij) = C = A T cij = aji C = A + B cij = aij + bij

Scalar-matrix multiplication Matrix-matrix multiplication C = aA cij = aaij r C = A B cij = Saikbkj k = 1 cij = row x column

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

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

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)

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|

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

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

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

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

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 = 1.2305

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

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

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)

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!

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

Intersection Ray Cast Containment Test

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

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”

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

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) = 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

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

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 + 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)

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 t B 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 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 …

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

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 Dynamics Particle system Game FX System http://upload.wikimedia.org/wikipedia/en/4/44/Strand_Emitter.jpg