Animation and Games Development 242-515, Semester 1, 2014-2015 7. Basic Maths • Objective o to give some background on the course 242-515 AGD: 7. Basic Maths 1 Overview 1. 2. 3. 4. 5. 6. 7. Vectors Matricies The Dot Product The Cross Product Transformations as Matricies Multiple Coordinate Spaces Models in a Scene Graph 242-515 AGD: 7. Basic Maths 2 1. Vectors • A vector is a magnitude (size) and a direction. o drawn as an arrow 242-515 AGD: 7. Basic Maths 3 • Two vectors V and W are added by placing the beginning of W at the end of V • Subtraction reverses the second vector W V V+W W V–W V –W V 242-515 AGD: 7. Basic Maths 4 Triangle Rule for Subtraction • Place c and d tail-to-tail. • d – c is the vector from the head of c to the head of d 242-515 AGD: 7. Basic Maths 5 6 7 • Vectors are added and subtracted component-wise: V W V1 W1 , V2 W2 , , Vn Wn V W V1 W1 ,V2 W2 , ,Vn Wn 242-515 AGD: 7. Basic Maths 8 Vector Magnitude • The magnitude of a vector is a scalar (a number): 242-515 AGD: 7. Basic Maths 9 10 Normalization • A normalized vector always has unit length. • To normalize a non-zero vector, divide by its magnitude. 242-515 AGD: 7. Basic Maths 11 Normalize [12, -5]: 242-515 AGD: 7. Basic Maths 12 Normalization as a Diagram unit circle 242-515 AGD: 7. Basic Maths 13 2. Matrices • A matrix is an array of numbers arranged in rows and columns o A matrix with n rows and m columns is an n m matrix o M is a 2 3 matrix o If n = m, the matrix is a square matrix 1 2 3 M 4 5 6 242-515 AGD: 7. Basic Maths 14 • The transpose of a matrix M is denoted MT and has its rows and columns exchanged: 1 2 3 M 4 5 6 242-515 AGD: 7. Basic Maths 1 4 T M 2 5 3 6 15 • An n-dimensional vector V can be thought of as an n 1 column matrix: V1 V2 V V1 , V2 , , Vn Vn • Or a 1 n row matrix: V T V1 V2 242-515 AGD: 7. Basic Maths Vn 16 Matrix Multiplication • The entries of the product of two matricies A and B are calculated by: m AB ij Aik Bkj k 1 • The number of columns in A must equal the number of rows in B. • If A is a n m matrix, and B is an m p matrix, then AB is an n p matrix. 242-515 AGD: 7. Basic Maths 17 • Example: 2 3 2 1 8 13 M 1 1 4 5 6 6 M 11 2 2 3 4 M 12 2 1 3 5 13 8 M 21 1 2 1 4 6 M 22 11 1 5 242-515 AGD: 7. Basic Maths 6 18 • In three dimensions, the product of a matrix and a column vector looks like: M 11 M 21 M 31 M 12 M 22 M 32 242-515 AGD: 7. Basic Maths M 13 Vx M 11Vx M 12Vy M 13Vz M 23 Vy M 21Vx M 22Vy M 23Vz M 33 Vz M 31Vx M 32V y M 33Vz 19 Identity Matrix In For any n n matrix M, the product with the identity matrix is M itself o I nM = M o MIn = M Invertible • An n n matrix M is invertible if there exists another matrix G such that 0 1 0 0 0 1 MG GM I n 0 0 1 • The inverse of M is denoted M-1 242-515 AGD: 7. Basic Maths 21 Determinant • The determinant of a square matrix M is denoted det M or |M| • A matrix is invertible if its determinant is not zero. • For a 2 2 matrix, a b a b det ad bc c d c d 242-515 AGD: 7. Basic Maths 22 • The determinant of a 3 3 matrix is m11 m12 m13 m21 m22 m23 m11 m31 m32 m33 m22 m23 m32 m33 m12 m21 m23 m31 m33 m13 m21 m22 m31 m32 m11 m22 m33 m23 m32 m12 m21m33 m23m31 m13 m21m32 m22 m31 242-515 AGD: 7. Basic Maths 23 Inverse • Explicit formulas exist for matrix inverses o These are good for small matrices, but other methods are generally used for larger matrices o In computer graphics, we usually deal with 2 2, 3 3, and a special form of 4 4 matrices 242-515 AGD: 7. Basic Maths 24 3. The Dot Product • The dot product is a product between two vectors that produces a magnitude (a number). • The dot product between two n-dimensional vectors V and W is given by n V W VW i i i 1 • In three dimensions, V W VxWx VyWy VzWz 242-515 AGD: 7. Basic Maths 25 Dot product a· b is the magnitude of the projection of the b vector onto the a vector. 242-515 AGD: 7. Basic Maths 26 Sign of the Dot Product 27 • The dot product equation: V W V W cos a oa is the angle between the two vectors o That's why the dot product is 0 between perpendicular vectors o If V and W are unit vectors, the dot product is 1 for parallel vectors pointing in the same direction, -1 for opposite 242-515 AGD: 7. Basic Maths 28 Dot Product for Finding Angles • The dot product can be used to find the angle between two vertors a and b. • First normalize a and b. ^ • The angle between them is cos-1 â . b 242-515 AGD: 7. Basic Maths 29 A Line as a Vector • A line in 3D space can be represented by R(t) = S + t V o S is a point on the line, and V is the direction along which the line runs. R is a point on the line, t is a magnitude. V t R S 242-515 AGD: 7. Basic Maths 30 Distance to a Line • Distance d from a point P to the line R(t)= S + t V P P S d S P S V V V 242-515 AGD: 7. Basic Maths 31 • Use Pythagorean theorem: P S V d P S V 2 2 2 • Taking square root, d P S 2 P S V 2 V2 • If V is unit length, then V 2 = 1 242-515 AGD: 7. Basic Maths 32 4. The Cross Product • The cross product between two vectors a and b produces a vector (a x b) which is perpendicular to both. 242-515 AGD: 7. Basic Maths 33 • The cross product between V and W is V W VyWz VzWy , VzWx VxWz , VxWy VyWx • One way of remembering this equation is as the determinant: ˆi ˆj kˆ V W Vx Vy Vz Wx Wy Wz 242-515 AGD: 7. Basic Maths 34 • It can also be expressed as a matrix-vector product 0 V W Vz Vy Vz 0 Vx Vy Wx Vx Wy 0 Wz • The perpendicularity property means that: V W V 0 242-515 AGD: 7. Basic Maths V W W 0 35 • The cross product equation is: V W V W sin a • This is equal to the area of the parallelogram formed by V and W: V ||V|| sin a a 242-515 AGD: 7. Basic Maths W 36 • Here's why: 242-515 AGD: 7. Basic Maths 37 b a 38 What about the axb Direction? • Does the vector a x b point up or down from the plane of a and b? • Place the tail of b at the head of a. • Using your right hand, curl your fingers in the direction of the vectors. • Your thumb points in the direction of a x b 242-515 AGD: 7. Basic Maths 39 • Reversing the order of the vectors negates the cross product: W V V W o the cross product is anti-commutative • If a x b = 0, then a is parallel to b. 242-515 AGD: 7. Basic Maths 40 5. Transformations as Matricies • A complex transformation can be built by combining simple transformations, of the types: o translations o scaling o rotations • Transformations are combined by multiplying their matrices together. 242-515 AGD: 7. Basic Maths 41 2D Translation y y (x+dx, y+dy) (x, y) dy (0,0) x dx x 42 2D Scaling • Resizes an object in each dimension about origin s x x 0 x 0 0 s y y 0 y y y (x, y) x 242-515 AGD: 7. Basic Maths (sxx, syy) x 43 2D Rotation • Rotate counter-clockwise about the origin by angle x cos sin x 0 y sin cos y 0 y y (x, y) x 242-515 AGD: 7. Basic Maths (x', y') x 44 Homogenous Transformation Matricies • An 4x4 homogenous matrix can store 3D transformations. • This makes matrix inversion and combinations of transformations simpler. • It also makes it possible to define perspective transformations. 242-515 AGD: 7. Basic Maths 45 3D Translation Translation by (Δx, Δy, Δz): x 1 0 0 0 1 0 y T x, y, z 0 0 1 z 0 0 0 1 T1( x, y, z)T( x, y, z) 3D Scaling Scaling about the origin sx 0 0 0 s 0 y Ssx , sy , sz 0 0 sz 0 0 0 0 0 0 1 1 1 1 S (sx , sy , sz ) S , , sx sy sz 1 242-515 AGD: 7. Basic Maths 47 3D Rotations • z-axis rotation M z -rotate cos sin 0 0 sin 0 0 cos 0 0 0 1 0 0 0 1 • Rotates about the z-axis through the angle 242-515 AGD: 7. Basic Maths 48 • Rotations about the x-, y- axes M x -rotate M y -rotate 242-515 AGD: 7. Basic Maths 1 0 0 0 0 cos sin 0 0 sin cos 0 0 0 0 1 0 sin 0 1 0 0 0 cos 0 0 0 1 cos 0 sin 0 49 Combining Transformations • The combined 4 4 homogenenous matrix is very common in computer graphics: R11 R21 M R31 0 R12 R13 R22 R23 R32 R33 0 0 Tx Ty Tz 1 o R is a 3 3 rotation matrix, and T is a translation vector 242-515 AGD: 7. Basic Maths 50 • The inverse of this 4 4 matrix is 'simple': M 1 R 1 0 242-515 AGD: 7. Basic Maths R111 1 R T R211 R311 1 0 R121 R131 1 R22 1 R23 R321 R331 0 0 R 1T x 1 R T y R 1T z 1 51 Transformations are not Commutative y y Translation followed by a rotation x x y Rotation followed by a translation (the meaning of the combined matrix) 242-515 AGD: 7. Basic Maths x 52 Concatenation of Transformations • How do we decompose a rotation about an arbitrary point into a sequence of basic transformations? y y x 242-515 AGD: 7. Basic Maths x 53 Rotating about an Arbitrary Point y translate to origin y (x0, y0) x x rotate y y (xo, yo) x 242-515 AGD: 7. Basic Maths translate back to (xo, yo) a TRT sequence x 54 • The orderings of the three operations is important o the rotation must be in between the two translations, or the end result will be different 242-515 AGD: 7. Basic Maths 55 T then R ≠ R then T 242-515 AGD: 7. Basic Maths 56 6. Multiple Coordinate Spaces • A point will be in a different location in different coordinate spaces: y v P (2,3) y u P (1,2) u x P in the xy coordinate space 242-515 AGD: 7. Basic Maths v x P in the uv coordinate space 57 • We use different coordinate spaces because some graphics operations are easier in one coordinate space instead of another. o e.g. rotating the hand of a robot model (see later) 242-515 AGD: 7. Basic Maths 58 Some Coordinate Spaces • World space o the global coordinate system o use it to keep track of every object in the game • Object space o o o o The origin is located at object’s origin The space's axes are aligned with the object's orientation The object's coordinates are defined using this space Called a local space in jME • Camera space o Camera’s “object” space 242-515 AGD: 7. Basic Maths 59 Robot in World Space 242-515 AGD: 7. Basic Maths 60 Robot’s Object Space 61 Object and World Space 62 Camera Space Object space for the viewer, represented by a camera. The camera space is used to project the 3D world onto the computer screen. 242-515 AGD: 7. Basic Maths 63 Where the camera is What the camera shows 64 Camera Space Terminology 242-515 AGD: 7. Basic Maths 65 Frustum Clipping Clipping: objects partially on screen Occlusion: objects hidden behind another object 7. Models in a Scene Graph • In a scene graph, a complex model is usually made up of simpler parts, connected together in a parent-child relationship. Scene Graph The 3D Model top half arm bottom half arm hand 242-515 AGD: 7. Basic Maths 67 Multiple Object Spaces • Each model part will have its own object space. • All these object spaces make the transformation of model parts (e.g. hand rotation) much easier. y object space x for top half arm y y x object space for bottom half arm x object space for hand 242-515 AGD: 7. Basic Maths 68 • Each object space can be thought of as a 'joint', allowing the model part in that space to be rotated. y x object space for top half arm y x y x 242-515 AGD: 7. Basic Maths object space = elbow joint for bottom half arm object space = hand joint for hand 69 Example: hand rotation • Rotate the hand's object space (the hand joint). y x = hand joint 242-515 AGD: 7. Basic Maths 70 • This rotation would be harder in the global space, since the rotation would need to be preceded by a translation of the wrist to the origin, rotation, then translation back o a TRT sequence 242-515 AGD: 7. Basic Maths 71 Child Rotation • Another advantage of the scene graph is that a rotation of an model part (e.g. the lower-half arm) affects it and all of its children (e.g. the hand). y = elbow joint x 242-515 AGD: 7. Basic Maths 72