1 / 53

The Math Lecture

The Math Lecture. ( Part I - Matrices). Introduction. For 2D games, we use a lot of trigonometry For 3D games, we use a lot of linear algebra Most of the time, we don’t have to use calculus A matrix can: Translate (move) a vertex Rotate a vertex Scale a vertex

isaiahf
Download Presentation

The Math Lecture

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. The Math Lecture (Part I - Matrices)

  2. Introduction • For 2D games, we use a lot of trigonometry • For 3D games, we use a lot of linear algebra • Most of the time, we don’t have to use calculus • A matrix can: • Translate (move) a vertex • Rotate a vertex • Scale a vertex • Math libraries cover up these details • Learn it anyway!

  3. Introduction • For 2D games, we use a lot of trigonometry • For 3D games, we use a lot of linear algebra • Most of the time, we don’t have to use calculus • A matrix can: • Translate (move) a vertex • Rotate a vertex • Scale a vertex • Math libraries cover up these details • Learn it anyway!

  4. What is the matrix? • Control • Position, scaling, and rotationof all your 3D objects in OpenGL

  5. What is a matrix? • m x n array of scalars M =

  6. A Frame in Matrix Form • Where P0 is the origin of the frame M: v2 v1 P0 v3

  7. Matrix Operations • Scalar multiplication, addition, matrix-matrix multiplication… • In this class, we really only care about: • matrix-matrix multiplications with square (m x m) matrices • matrix-vector or matrix-point multiplication

  8. Matrix-Point (or vector) Multiplication In GLM you can use the overloaded operator * for this

  9. Matrix-Matrix Multiplication Does A*B = B*A? NO What does the identity do? ? ? In GLM you can use the overloaded operator * for this

  10. Examples ? ?

  11. Activity: 4x4 Matrix Multiplication • (5 min) In CG, we mostly use 4x4 matrices • Multiply the following: • Use GLM!

  12. Properties of Matrices • Associative • A(BC) = (AB)C • NOT commutative (usually) • AB = BA • However, • IA = AI = A In GLM the * operator adheres to these rules

  13. Matrix Algebra • Assume AB=C • Assume A and C are known. • B is unknown • What is B? • Lets do some algebra!! • A-1AB=A-1C • A-1A = Identity • Then B=A-1C

  14. MAtrices • The foundation of all geometric operations (translation, rotation, scaling, skewing…) • Have multiple rows and columns (usually 3x3 or 4x4) • Below is an identity matrix • We can multiply a matrix • with another matrix, and get a matrix • with a vector of “appropriate” dimension (later)

  15. Transposing a Matrix • Have a transpose (denoted ) where • In other words, make the rows columns! • glm::transpose Transpose

  16. Transposing a Matrix • Have a transpose (denoted ) where • In other words, make the rows columns! • Mirror image along the diagonal Transpose The Matrix has you…

  17. Matrices • There’s also an inverse matrix (denoted ) where • Not all matrices are invertible • Can check by getting the determinant of the matrix (looking for non-zero) • glm::inverse

  18. Matrices • There’s also an inverse matrix (denoted ) where • Not all matrices are invertible • Can check by getting the determinant of the matrix (looking for non-zero)

  19. Matrices • There’s also an inverse matrix (denoted ) where • Not all matrices are invertible • Can check by getting the determinant of the matrix (looking for non-zero)

  20. Matrices • There’s also an inverse matrix (denoted ) where • Not all matrices are invertible • Can check by getting the determinant of the matrix (looking for non-zero)

  21. Matrices • There’s also an inverse matrix (denoted ) where • Not all matrices are invertible • Can check by getting the determinant of the matrix (looking for non-zero) Follow the white rabbit…

  22. A few Special Matrices(Used for Rotation)

  23. Matrix Multiplication • Multiplying a point by a matrix gives us a new point! • Let’s say that we want to rotate the point around the z axis by 90 degrees • Points are always stored with a 1 for the 4th element = Oldpoint Newpoint

  24. Axis-Angle Rotations in GLM glm::rotate( angle, x, y, z); Where angle is the angle and (x,y,z) is a vector you want to rotate around (normalize it first!) Example: Rotate 30 deg around the vector (1,1,1) glm::vec3 v = normalize(vec3(1,1,1)); glm::rotate(30, v.x, v.y, v.z)

  25. Euler Angles More intuitive: represent rotations by 3 angles, one for each axis glm::rotate(anglex,1,0,0) * glm::rotate(angley,0,1,0) * glm::rotate(anglez,0,0,1); Think: if we have a torus unstranformed at the origin, what will the torus look like if you have anglex=90, angley=90, and anglez=90 Transformed

  26. Gimbal Lock Rotate around these Initial orientation (x=blue, y=green, z = red) How can we gain altitude here?

  27. Quaternions • Quaternions represent 3D rotations in 4D using imaginary numbers – a 4-tuple Q = (w,x,y,z) • IF YOU EVER RUN INTO GIMBAL LOCK: • 1) Convert to Quaternion from euler, matrix, or angle-axis • 2) Do rotations in quaternion form • 3) Convert to angle-axis or matrix • Quaternions are easy to use in GLM • http://glm.g-truc.net/api-0.9.0/a00184.html

  28. Matrix Multiplication • Multiplying a point by a matrix gives us a new point! • Let’s say that we want to rotate the point around the z axis by 90 degrees • Points are always stored with a 1 for the 4th element = -1 0

  29. Matrix Multiplication • Multiplying a point by a matrix gives us a new point! • Let’s say that we want to rotate the point around the z axis by 90 degrees • Points are always stored with a 1 for the 4th element =

  30. Matrix Multiplication • Multiplying a point by a matrix gives us a new point! • Let’s say that we want to rotate the point around the z axis by 90 degrees • Points are always stored with a 1 for the 4th element =

  31. Matrix Multiplication • Multiplying a point by a matrix gives us a new point! • Let’s say that we want to rotate the point around the z axis by 90 degrees • Points are always stored with a 1 for the 4th element = What IS real?

  32. Translation • It’s a piece of cake, because the 4th column is the translation!

  33. Translation • It’s a piece of cake, because the 4th column is the translation! • Imagine we want to move the point by (2, 1, -3). Then: =

  34. Translation • It’s a piece of cake, because the 4th column is the translation! • Imagine we want to move the point by (2, 1, -3). Then: =

  35. Translation • It’s a piece of cake, because the 4th column is the translation! • Imagine we want to move the point by (2, 1, -3). Then: =

  36. Translation • It’s a piece of cake, because the 4th column is the translation! • Imagine we want to move the point by (2, 1, -3). Then: =

  37. Translation • It’s a piece of cake, because the 4th column is the translation! • Imagine we want to move the point by (2, 1, -3). Then: = Why, oh why, didn’t I take the blue pill?

  38. Translation • It’s a piece of cake, because the 4th column is the translation! • Imagine we want to move the point by (2, 1, -3). Then: =

  39. SCALING • It’s a piece of cake too, because it’s the diagonal! • We can scale along just one axis, or more than one! • Imagine we want to scale the point by the values x, y and z. Then: = Trace program: running

  40. Matrix Multiplication • What if you want to rotate a point and then translate it? • Need a rotation matrix • Need a translation matrix • Returns a 4x4 matrix

  41. Matrix Multiplication • What if you want to rotate a point and then translate it? • Need a rotation matrix • Need a translation matrix • Returns a 4x4 matrix

  42. Matrix Multiplication • What if you want to rotate a point and then translate it? • Need a rotation matrix • Need a translation matrix • Returns a 4x4 matrix

  43. Matrix Multiplication • What if you want to rotate a point and then translate it? • Need a rotation matrix • Need a translation matrix • Returns a 4x4 matrix

  44. Matrix Multiplication • What if you want to rotate a point and then translate it? • Need a rotation matrix • Need a translation matrix • Returns a 4x4 matrix

  45. Matrix Multiplication • What if you want to rotate a point and then translate it? • Need a rotation matrix • Need a translation matrix • Returns a 4x4 matrix

  46. Matrix Multiplication • What if you want to rotate a point and then translate it? • Need a rotation matrix • Need a translation matrix • Returns a 4x4 matrix

  47. Matrix Multiplication • What if you want to rotate a point and then translate it? • Need a rotation matrix • Need a translation matrix • Returns a 4x4 matrix

  48. Matrix Multiplication • What if you want to rotate a point and then translate it? • Need a rotation matrix • Need a translation matrix • Returns a 4x4 matrix There is no spoon…

  49. Matrix Multiplication • What if you want to rotate a point and then translate it? • Need a rotation matrix • Need a translation matrix • Returns a 4x4 matrix ?

  50. Matrix Multiplication • What if you want to rotate a point and then translate it? • Need a rotation matrix • Need a translation matrix • Returns a 4x4 matrix =

More Related