0
Introduction to Game Physics with Box2D2. Mathematics for Game Physics           Lecture 2.1: Basic Math               Ian...
Contents of This Lecture1. Geometry2. Vectors3. OrientationChapter 2         Introduction to Game Physics with Box2D   2
René Descartes• 1596 – 1650, French  philosopher, physicist,  physiologist, & mathematician.• Famous for (among other  thi...
GeometryChapter 2   Introduction to Game Physics with Box2D   4
2.1.3                  2.2.2                     2.2.1                          Vector               Ball to Line         ...
Things You Might Remember From SchoolChapter 2     Introduction to Game Physics with Box2D   6
Trig FunctionsChapter 2    Introduction to Game Physics with Box2D   7
Mnemonics1. Sohcahtoa2. Some Old Horse Caught Another Horse   Taking Oats Away.3. Some Old Hippy Caught Another Hippy   To...
Theorem of PythagorasChapter 2        Introduction to Game Physics with Box2D   9
Proof of the Theorem of PythagorasChapter 2             Introduction to Game Physics with Box2D   10
Pythagorean IdentityChapter 2       Introduction to Game Physics with Box2D   11
Law of CosinesChapter 2   Introduction to Game Physics with Box2D   12
Proof of the Law of CosinesChapter 2         Introduction to Game Physics with Box2D   13
More Useful Trig IdentitiesChapter 2          Introduction to Game Physics with Box2D   14
VectorsChapter 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 D...
Chapter 2   Introduction to Game Physics with Box2D   18
Vector Multiplication by a ScalarChapter 2 Notes   3D Math Primer for Graphics & Game Dev   19
Vector Addition: AlgebraChapter 2 Notes         3D Math Primer for Graphics & Game Dev   20
Vector Addition: Geometry                                          =                                          =Chapter 2  ...
Vector Addition in Code     D3DXVECTOR2 u, v, w;     v = D3DXVECTOR2(3.1415f, 7.0f);     u = 42.0f * v;     w = u + D3DXVE...
Vector MagnitudeChapter 2     Introduction to Game Physics with Box2D   23
Vector NormalizationChapter 2       Introduction to Game Physics with Box2D   24
Vector Magnitude in Code• D3DXVec2Normalize normalizes a  D3DXVECTOR2, that is, makes its length 1.• D3DXVec2Length comput...
OrientationChapter 2   Introduction to Game Physics with Box2D   26
Relative OrientationChapter 2       Introduction to Game Physics with Box2D   27
OrientationChapter 2   Introduction to Game Physics with Box2D   28
Length & Orientation to Vector                                             Step 1. Step 2.                                ...
Vector to Orientation                                             Step 2. Step 1.                                         ...
A GotchaChapter 2   Introduction to Game Physics with Box2D   31
Rotating a VectorChapter 2     Introduction to Game Physics with Box2D   32
Rotating a VectorChapter 2     Introduction to Game Physics with Box2D   33
Rotating a VectorChapter 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); ...
Optimizing the Rotation CodeChapter 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) -...
ConclusionChapter 2   Introduction to Game Physics with Box2D   38
Suggested ReadingSection 2.1            Suggested Activities  Problems 1-4 from Section 2.5.Chapter 2        Introduction ...
Upcoming SlideShare
Loading in...5
×

Intro to Game Physics with Box2D Chapter 2 Part 1

1,314

Published on

Lecture notes for Chapter 2 (Part 1) of Ian Parberry's "Introduction to Game Physics with Box2D", AK Peters, 2013.

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,314
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
26
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • 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.
  • 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.
  • 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”.
  • First section: Geometry.
  • 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.
  • Hopefully you’ll remember from high school how to find the root of quadratic equations and the basic trig functions.
  • In case you’ve forgotten.
  • Some handy ways of remembering them. Do you know any more?
  • Proof in a moment. Did anybody prove it to you? Proving things helps you remember them or reconstruct them.
  • How to prove it. Instructor, please fill in the gaps. This is just a visual aid.
  • This follows from his Theorem. Go through the 2 steps.
  • 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.
  • 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.
  • 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.
  • Second section, vectors.
  • Just read this.
  • This is for DirectX. Substitute OpenGL if you’re picky.
  • Here’s the vector [5,3]
  • Read me.
  • Vector addition and subtraction. Point out the head-to-tail and the tail-to-tail thing.
  • 42 appears a lot in this book.
  • Section 3, orientation. That is, which way things are pointing.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • The hats mean length 1.
  • 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.
  • Go slowly here.
  • Of course I’m assuming that we’ve #included <math.h>. Some smart-aleck may remind you in class.
  • Go through this slowly and carefully. The red guideposts will help you keep on track.
  • 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.
  • Transcript of "Intro to Game Physics with Box2D Chapter 2 Part 1"

    1. 1. Introduction to Game Physics with Box2D2. Mathematics for Game Physics Lecture 2.1: Basic Math Ian Parberry Dept. of Computer Science & Engineering University of North Texas
    2. 2. Contents of This Lecture1. Geometry2. Vectors3. OrientationChapter 2 Introduction to Game Physics with Box2D 2
    3. 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. 4. GeometryChapter 2 Introduction to Game Physics with Box2D 4
    5. 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 ChaptersChapter 2 Introduction to Game Physics with Box2D 5
    6. 6. Things You Might Remember From SchoolChapter 2 Introduction to Game Physics with Box2D 6
    7. 7. Trig FunctionsChapter 2 Introduction to Game Physics with Box2D 7
    8. 8. Mnemonics1. Sohcahtoa2. 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. 9. Theorem of PythagorasChapter 2 Introduction to Game Physics with Box2D 9
    10. 10. Proof of the Theorem of PythagorasChapter 2 Introduction to Game Physics with Box2D 10
    11. 11. Pythagorean IdentityChapter 2 Introduction to Game Physics with Box2D 11
    12. 12. Law of CosinesChapter 2 Introduction to Game Physics with Box2D 12
    13. 13. Proof of the Law of CosinesChapter 2 Introduction to Game Physics with Box2D 13
    14. 14. More Useful Trig IdentitiesChapter 2 Introduction to Game Physics with Box2D 14
    15. 15. VectorsChapter 2 Introduction to Game Physics with Box2D 15
    16. 16. What’s Our Vector, Victor?Chapter 2 Introduction to Game Physics with Box2D 16
    17. 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. 18. Chapter 2 Introduction to Game Physics with Box2D 18
    19. 19. Vector Multiplication by a ScalarChapter 2 Notes 3D Math Primer for Graphics & Game Dev 19
    20. 20. Vector Addition: AlgebraChapter 2 Notes 3D Math Primer for Graphics & Game Dev 20
    21. 21. Vector Addition: Geometry = =Chapter 2 Introduction to Game Physics with Box2D 21
    22. 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. 23. Vector MagnitudeChapter 2 Introduction to Game Physics with Box2D 23
    24. 24. Vector NormalizationChapter 2 Introduction to Game Physics with Box2D 24
    25. 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. 26. OrientationChapter 2 Introduction to Game Physics with Box2D 26
    27. 27. Relative OrientationChapter 2 Introduction to Game Physics with Box2D 27
    28. 28. OrientationChapter 2 Introduction to Game Physics with Box2D 28
    29. 29. Length & Orientation to Vector Step 1. Step 2. Step 3.Chapter 2 Introduction to Game Physics with Box2D 29
    30. 30. Vector to Orientation Step 2. Step 1. Step 3.Chapter 2 Introduction to Game Physics with Box2D 30
    31. 31. A GotchaChapter 2 Introduction to Game Physics with Box2D 31
    32. 32. Rotating a VectorChapter 2 Introduction to Game Physics with Box2D 32
    33. 33. Rotating a VectorChapter 2 Introduction to Game Physics with Box2D 33
    34. 34. Rotating a VectorChapter 2 Introduction to Game Physics with Box2D 34
    35. 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); } //RotateThis works but we can do better. The atan2can be optimized out.Chapter 2 Introduction to Game Physics with Box2D 35
    36. 36. Optimizing the Rotation CodeChapter 2 Introduction to Game Physics with Box2D 36
    37. 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); } //RotateWe’ve replaced an arctangent with four floatingpoint multiplications, which is faster in practice.Chapter 2 Introduction to Game Physics with Box2D 37
    38. 38. ConclusionChapter 2 Introduction to Game Physics with Box2D 38
    39. 39. Suggested ReadingSection 2.1 Suggested Activities Problems 1-4 from Section 2.5.Chapter 2 Introduction to Game Physics with Box2D 39
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×