SlideShare une entreprise Scribd logo
1  sur  39
Introduction to Game Physics with Box2D
2. Mathematics for Game Physics
           Lecture 2.1: Basic Math


               Ian Parberry
    Dept. of Computer Science & Engineering
            University of North Texas
Contents of This Lecture
1. Geometry
2. Vectors
3. Orientation




Chapter 2         Introduction to Game Physics with Box2D   2
René Descartes
• 1596 – 1650, French
  philosopher, physicist,
  physiologist, & mathematician.
• Famous for (among other
  things) recognizing that linear
  algebra and geometry are the
  same thing.
• Particularly useful for us,
  because the CPU does linear
  algebra and what we see on
  the screen is geometry.

Chapter 2        Introduction to Game Physics with Box2D   3
Geometry




Chapter 2   Introduction to Game Physics with Box2D   4
2.1.3                  2.2.2                     2.2.1
                          Vector               Ball to Line              Ball to Wall
              3-7                                                                          3,4
                         Magnitude              Collision                 Collision
            Chapters                                                                     Chapters
                       2.1.3                                 2.2.2              2.1.4
                         Theorem of            Vector Dot                  Vector
                                                                                          4, 5, 8
                         Pythagoras             Product                  Orientation
                                                                                         Chapters
                                           2.1.3                                 2.1.4
                                      Law of
                                                                  6,8
                                      Cosines
                                                             Chapters

                       2.1.3                                 2.2.3
                        Pythagorean                Ball to Ball           Quadratic
                          Identity                  Collision             Equations




                                                       3,4

                                                   Chapters


Chapter 2                       Introduction to Game Physics with Box2D                             5
Things You Might Remember From School




Chapter 2     Introduction to Game Physics with Box2D   6
Trig Functions




Chapter 2    Introduction to Game Physics with Box2D   7
Mnemonics
1. Sohcahtoa
2. Some Old Horse Caught Another Horse
   Taking Oats Away.
3. Some Old Hippy Caught Another Hippy
   Toking On Acid.




Chapter 2      Introduction to Game Physics with Box2D   8
Theorem of Pythagoras




Chapter 2        Introduction to Game Physics with Box2D   9
Proof of the Theorem of Pythagoras




Chapter 2             Introduction to Game Physics with Box2D   10
Pythagorean Identity




Chapter 2       Introduction to Game Physics with Box2D   11
Law of Cosines




Chapter 2   Introduction to Game Physics with Box2D   12
Proof of the Law of Cosines




Chapter 2         Introduction to Game Physics with Box2D   13
More Useful Trig Identities




Chapter 2          Introduction to Game Physics with Box2D   14
Vectors




Chapter 2   Introduction to Game Physics with Box2D   15
What’s Our Vector, Victor?




Chapter 2          Introduction to Game Physics with Box2D   16
Programming Vectors
• Vectors correspond very naturally to an array
  in most programming languages.
• D3DX has a structure D3DXVECTOR2 that we
  will use to implement 2D vectors in code.
• A D3DXVECTOR2 v has two floating point
  fields v.x and v.y.



Chapter 2       Introduction to Game Physics with Box2D   17
Chapter 2   Introduction to Game Physics with Box2D   18
Vector Multiplication by a Scalar




Chapter 2 Notes   3D Math Primer for Graphics & Game Dev   19
Vector Addition: Algebra




Chapter 2 Notes         3D Math Primer for Graphics & Game Dev   20
Vector Addition: Geometry

                                          =


                                          =



Chapter 2          Introduction to Game Physics with Box2D   21
Vector Addition in Code
     D3DXVECTOR2 u, v, w;
     v = D3DXVECTOR2(3.1415f, 7.0f);
     u = 42.0f * v;
     w = u + D3DXVECTOR2(v.x, 9.0f);
     u += w;




Chapter 2         Introduction to Game Physics with Box2D   22
Vector Magnitude




Chapter 2     Introduction to Game Physics with Box2D   23
Vector Normalization




Chapter 2       Introduction to Game Physics with Box2D   24
Vector Magnitude in Code
• D3DXVec2Normalize normalizes a
  D3DXVECTOR2, that is, makes its length 1.
• D3DXVec2Length computes the length of a
  D3DXVECTOR2.
      – Square roots are expensive.
      – Often it is just as easy to work with squares of
        vector lengths as with lengths.
      – If so, use the faster D3DXVec2LengthSq
        function instead of D3DXVec2Length.

Chapter 2             Introduction to Game Physics with Box2D   25
Orientation




Chapter 2   Introduction to Game Physics with Box2D   26
Relative Orientation




Chapter 2       Introduction to Game Physics with Box2D   27
Orientation




Chapter 2   Introduction to Game Physics with Box2D   28
Length & Orientation to Vector
                                             Step 1.




 Step 2.                                     Step 3.




Chapter 2       Introduction to Game Physics with Box2D   29
Vector to Orientation
                                             Step 2.


 Step 1.

                                             Step 3.




Chapter 2       Introduction to Game Physics with Box2D   30
A Gotcha




Chapter 2   Introduction to Game Physics with Box2D   31
Rotating a Vector




Chapter 2     Introduction to Game Physics with Box2D   32
Rotating a Vector




Chapter 2     Introduction to Game Physics with Box2D   33
Rotating a Vector




Chapter 2     Introduction to Game Physics with Box2D   34
Vector Rotation Code
  void Rotate(const D3DXVECTOR2& u,
    D3DXVECTOR2& v, float beta){
    float alpha = atan2(u.y, u.x);
    v.x = cos(alpha + beta);
    v.y = sin(alpha + beta);
  } //Rotate

This works but we can do better. The atan2
can be optimized out.

Chapter 2       Introduction to Game Physics with Box2D   35
Optimizing the Rotation Code




Chapter 2           Introduction to Game Physics with Box2D   36
Optimized Vector Rotation Code
  void Rotate(const D3DXVECTOR2& u,
   D3DXVECTOR2& v, float beta){
   v.x = u.x * cos(beta) - u.y * sin(beta);
   v.y = u.x * sin(beta) + u.y * cos(beta);
  } //Rotate


We’ve replaced an arctangent with four floating
point multiplications, which is faster in practice.


Chapter 2        Introduction to Game Physics with Box2D   37
Conclusion




Chapter 2   Introduction to Game Physics with Box2D   38
Suggested Reading
Section 2.1



            Suggested Activities
  Problems 1-4 from Section 2.5.



Chapter 2        Introduction to Game Physics with Box2D   39

Contenu connexe

Tendances

M2 Internship report rare-earth nickelates
M2 Internship report rare-earth nickelatesM2 Internship report rare-earth nickelates
M2 Internship report rare-earth nickelates
Yiteng Dang
 

Tendances (20)

n -Path Graph
n -Path Graphn -Path Graph
n -Path Graph
 
Icmtea
IcmteaIcmtea
Icmtea
 
Construction of BIBD’s Using Quadratic Residues
Construction of BIBD’s Using Quadratic ResiduesConstruction of BIBD’s Using Quadratic Residues
Construction of BIBD’s Using Quadratic Residues
 
AA Section 2-9
AA Section 2-9AA Section 2-9
AA Section 2-9
 
D03402029035
D03402029035D03402029035
D03402029035
 
honn
honnhonn
honn
 
Steven Duplij, Raimund Vogl, "Polyadic Braid Operators and Higher Braiding Ga...
Steven Duplij, Raimund Vogl, "Polyadic Braid Operators and Higher Braiding Ga...Steven Duplij, Raimund Vogl, "Polyadic Braid Operators and Higher Braiding Ga...
Steven Duplij, Raimund Vogl, "Polyadic Braid Operators and Higher Braiding Ga...
 
SPLIT BLOCK SUBDIVISION DOMINATION IN GRAPHS
SPLIT BLOCK SUBDIVISION DOMINATION IN GRAPHSSPLIT BLOCK SUBDIVISION DOMINATION IN GRAPHS
SPLIT BLOCK SUBDIVISION DOMINATION IN GRAPHS
 
Steven Duplij, Raimund Vogl, "Polyadic braid operators and higher braiding ga...
Steven Duplij, Raimund Vogl, "Polyadic braid operators and higher braiding ga...Steven Duplij, Raimund Vogl, "Polyadic braid operators and higher braiding ga...
Steven Duplij, Raimund Vogl, "Polyadic braid operators and higher braiding ga...
 
M2 Internship report rare-earth nickelates
M2 Internship report rare-earth nickelatesM2 Internship report rare-earth nickelates
M2 Internship report rare-earth nickelates
 
S. Duplij, "Higher braid groups and regular semigroups from polyadic binary c...
S. Duplij, "Higher braid groups and regular semigroups from polyadic binary c...S. Duplij, "Higher braid groups and regular semigroups from polyadic binary c...
S. Duplij, "Higher braid groups and regular semigroups from polyadic binary c...
 
International journal of engineering issues vol 2015 - no 2 - paper5
International journal of engineering issues   vol 2015 - no 2 - paper5International journal of engineering issues   vol 2015 - no 2 - paper5
International journal of engineering issues vol 2015 - no 2 - paper5
 
K mean-clustering
K mean-clusteringK mean-clustering
K mean-clustering
 
International Journal of Computational Engineering Research(IJCER)
International Journal of Computational Engineering Research(IJCER)International Journal of Computational Engineering Research(IJCER)
International Journal of Computational Engineering Research(IJCER)
 
Radix-3 Algorithm for Realization of Discrete Fourier Transform
Radix-3 Algorithm for Realization of Discrete Fourier TransformRadix-3 Algorithm for Realization of Discrete Fourier Transform
Radix-3 Algorithm for Realization of Discrete Fourier Transform
 
K-means Clustering Algorithm with Matlab Source code
K-means Clustering Algorithm with Matlab Source codeK-means Clustering Algorithm with Matlab Source code
K-means Clustering Algorithm with Matlab Source code
 
K means
K meansK means
K means
 
i(G)-Graph - G(i) Of Some Special Graphs
i(G)-Graph - G(i) Of Some Special Graphsi(G)-Graph - G(i) Of Some Special Graphs
i(G)-Graph - G(i) Of Some Special Graphs
 
Neural nw k means
Neural nw k meansNeural nw k means
Neural nw k means
 
Nonlinear constitutive equations for gravitoelectromagnetism
Nonlinear constitutive equations for gravitoelectromagnetismNonlinear constitutive equations for gravitoelectromagnetism
Nonlinear constitutive equations for gravitoelectromagnetism
 

Intro to Game Physics with Box2D Chapter 2 Part 1

  • 1. Introduction to Game Physics with Box2D 2. Mathematics for Game Physics Lecture 2.1: Basic Math Ian Parberry Dept. of Computer Science & Engineering University of North Texas
  • 2. Contents of This Lecture 1. Geometry 2. Vectors 3. Orientation Chapter 2 Introduction to Game Physics with Box2D 2
  • 3. René Descartes • 1596 – 1650, French philosopher, physicist, physiologist, & mathematician. • Famous for (among other things) recognizing that linear algebra and geometry are the same thing. • Particularly useful for us, because the CPU does linear algebra and what we see on the screen is geometry. Chapter 2 Introduction to Game Physics with Box2D 3
  • 4. Geometry Chapter 2 Introduction to Game Physics with Box2D 4
  • 5. 2.1.3 2.2.2 2.2.1 Vector Ball to Line Ball to Wall 3-7 3,4 Magnitude Collision Collision Chapters Chapters 2.1.3 2.2.2 2.1.4 Theorem of Vector Dot Vector 4, 5, 8 Pythagoras Product Orientation Chapters 2.1.3 2.1.4 Law of 6,8 Cosines Chapters 2.1.3 2.2.3 Pythagorean Ball to Ball Quadratic Identity Collision Equations 3,4 Chapters Chapter 2 Introduction to Game Physics with Box2D 5
  • 6. Things You Might Remember From School Chapter 2 Introduction to Game Physics with Box2D 6
  • 7. Trig Functions Chapter 2 Introduction to Game Physics with Box2D 7
  • 8. Mnemonics 1. Sohcahtoa 2. Some Old Horse Caught Another Horse Taking Oats Away. 3. Some Old Hippy Caught Another Hippy Toking On Acid. Chapter 2 Introduction to Game Physics with Box2D 8
  • 9. Theorem of Pythagoras Chapter 2 Introduction to Game Physics with Box2D 9
  • 10. Proof of the Theorem of Pythagoras Chapter 2 Introduction to Game Physics with Box2D 10
  • 11. Pythagorean Identity Chapter 2 Introduction to Game Physics with Box2D 11
  • 12. Law of Cosines Chapter 2 Introduction to Game Physics with Box2D 12
  • 13. Proof of the Law of Cosines Chapter 2 Introduction to Game Physics with Box2D 13
  • 14. More Useful Trig Identities Chapter 2 Introduction to Game Physics with Box2D 14
  • 15. Vectors Chapter 2 Introduction to Game Physics with Box2D 15
  • 16. What’s Our Vector, Victor? Chapter 2 Introduction to Game Physics with Box2D 16
  • 17. Programming Vectors • Vectors correspond very naturally to an array in most programming languages. • D3DX has a structure D3DXVECTOR2 that we will use to implement 2D vectors in code. • A D3DXVECTOR2 v has two floating point fields v.x and v.y. Chapter 2 Introduction to Game Physics with Box2D 17
  • 18. Chapter 2 Introduction to Game Physics with Box2D 18
  • 19. Vector Multiplication by a Scalar Chapter 2 Notes 3D Math Primer for Graphics & Game Dev 19
  • 20. Vector Addition: Algebra Chapter 2 Notes 3D Math Primer for Graphics & Game Dev 20
  • 21. Vector Addition: Geometry = = Chapter 2 Introduction to Game Physics with Box2D 21
  • 22. Vector Addition in Code D3DXVECTOR2 u, v, w; v = D3DXVECTOR2(3.1415f, 7.0f); u = 42.0f * v; w = u + D3DXVECTOR2(v.x, 9.0f); u += w; Chapter 2 Introduction to Game Physics with Box2D 22
  • 23. Vector Magnitude Chapter 2 Introduction to Game Physics with Box2D 23
  • 24. Vector Normalization Chapter 2 Introduction to Game Physics with Box2D 24
  • 25. Vector Magnitude in Code • D3DXVec2Normalize normalizes a D3DXVECTOR2, that is, makes its length 1. • D3DXVec2Length computes the length of a D3DXVECTOR2. – Square roots are expensive. – Often it is just as easy to work with squares of vector lengths as with lengths. – If so, use the faster D3DXVec2LengthSq function instead of D3DXVec2Length. Chapter 2 Introduction to Game Physics with Box2D 25
  • 26. Orientation Chapter 2 Introduction to Game Physics with Box2D 26
  • 27. Relative Orientation Chapter 2 Introduction to Game Physics with Box2D 27
  • 28. Orientation Chapter 2 Introduction to Game Physics with Box2D 28
  • 29. Length & Orientation to Vector Step 1. Step 2. Step 3. Chapter 2 Introduction to Game Physics with Box2D 29
  • 30. Vector to Orientation Step 2. Step 1. Step 3. Chapter 2 Introduction to Game Physics with Box2D 30
  • 31. A Gotcha Chapter 2 Introduction to Game Physics with Box2D 31
  • 32. Rotating a Vector Chapter 2 Introduction to Game Physics with Box2D 32
  • 33. Rotating a Vector Chapter 2 Introduction to Game Physics with Box2D 33
  • 34. Rotating a Vector Chapter 2 Introduction to Game Physics with Box2D 34
  • 35. Vector Rotation Code void Rotate(const D3DXVECTOR2& u, D3DXVECTOR2& v, float beta){ float alpha = atan2(u.y, u.x); v.x = cos(alpha + beta); v.y = sin(alpha + beta); } //Rotate This works but we can do better. The atan2 can be optimized out. Chapter 2 Introduction to Game Physics with Box2D 35
  • 36. Optimizing the Rotation Code Chapter 2 Introduction to Game Physics with Box2D 36
  • 37. Optimized Vector Rotation Code void Rotate(const D3DXVECTOR2& u, D3DXVECTOR2& v, float beta){ v.x = u.x * cos(beta) - u.y * sin(beta); v.y = u.x * sin(beta) + u.y * cos(beta); } //Rotate We’ve replaced an arctangent with four floating point multiplications, which is faster in practice. Chapter 2 Introduction to Game Physics with Box2D 37
  • 38. Conclusion Chapter 2 Introduction to Game Physics with Box2D 38
  • 39. Suggested Reading Section 2.1 Suggested Activities Problems 1-4 from Section 2.5. Chapter 2 Introduction to Game Physics with Box2D 39

Notes de l'éditeur

  1. These lecture notes accompany “Introduction to Game Physics with Box2D” by Ian Parberry. They are not a substitute for the book, however, so don’t get any ideas.
  2. Here’s what we’re going to cover today: Geometry, vectors, and orientation. Most of this class should be vaguely familiar to you from high school and freshman math. If so, treat this as a reminder. There may be some things that your teachers forgot to teach you.
  3. The Cartesian Plane is named after him.“I think, therefore I am” is one of his epistemological gems. Monty Python’s “The Philosopher’s Song” immortalized it in the refrain “I drink, therefore I am”.
  4. First section: Geometry.
  5. The brown boxes are the starting points from which we derive the other results. We’re mostly trying to get to the collision boxes because they’re used in other chapters, but vector magnitude will be used throughout the rest of the book.
  6. Hopefully you’ll remember from high school how to find the root of quadratic equations and the basic trig functions.
  7. In case you’ve forgotten.
  8. Some handy ways of remembering them. Do you know any more?
  9. Proof in a moment. Did anybody prove it to you? Proving things helps you remember them or reconstruct them.
  10. How to prove it. Instructor, please fill in the gaps. This is just a visual aid.
  11. This follows from his Theorem. Go through the 2 steps.
  12. This is the statement of the law of cosines. It’s like a Pythagorean Theorem for non-right triangles.Point out the symmetry in the labeling of the side-lengths, the vertices, and the angles of the triangles.Point out the symmetries in the equations. That’s a handy way to memorize them, if necessary.Next we prove one of them. All it takes is the Pythagorean Theorem and the Pythagorean Identity.
  13. First line uses the Theorem of Pythagoras on the green triangle, BCD.Second line simply multiplies out the two squares.Third line uses the Pythagorean Identity.
  14. Not going to prove them, but there’s a proof for the first one in Appendix A. The proof for the second one is summarized in the diagram here. We’ll use these identities later in the lecture to optimize some vector rotation code.
  15. Second section, vectors.
  16. Just read this.
  17. This is for DirectX. Substitute OpenGL if you’re picky.
  18. Here’s the vector [5,3]
  19. Read me.
  20. Vector addition and subtraction. Point out the head-to-tail and the tail-to-tail thing.
  21. 42 appears a lot in this book.
  22. Section 3, orientation. That is, which way things are pointing.
  23. Remind them about radians, how many radians in a circle, etc.Radians are good for computers, degrees are good for people.Explain to them why there are 360 degrees in a circle. It’s a bit baffling why we use base 60 for compasses and clocks. If you don’t know this story, there’s always Wikipedia. Look it up real time in class while teaching for an extra zing… students will pay attention to this kind of thing.
  24. OK. Remember this. Measure positive angles going counterclockwise (anticlockwise for Brits) from the positive x-axis.This direction is also known as “widdershins”. Again, go to wikipedia for a history lesson.
  25. Given a length and an orientation, find the vector of that length pointing in that direction. Easy with length 1.What about other lengths? Ask the audience. Should be a nice “duh” moment to see if they’ve been following along.
  26. Now to go backwards, given a vector, find its orientation.Which of the 3 choices in Step 3 shall we use? Theoretically they’re all good. But theta = atan(b/a) is numerically better. The other two are throwing away precision by not using all of the available data, both a and b.
  27. There’s an animation on this slide. Hit the space bar to see a ring around the wrong answer from arctan and the right answer on the diagram below.
  28. The hats mean length 1.
  29. Can you see where we are going? We’re going to need the identity for trig functions of the sum of two angles that we stated without proof on slide 14 or thereabouts.
  30. Go slowly here.
  31. Of course I’m assuming that we’ve #included <math.h>. Some smart-aleck may remind you in class.
  32. Go through this slowly and carefully. The red guideposts will help you keep on track.
  33. Much simpler code. Perhaps you could set a programming assignment in which they code this up and test it against the slow version? Depends on your computer architecture I suppose.