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