intro to game physics with box2d chapter 2 part 1
DESCRIPTION
Lecture notes for Chapter 2 (Part 1) of Ian Parberry's "Introduction to Game Physics with Box2D", AK Peters, 2013.TRANSCRIPT
![Page 1: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/1.jpg)
Introduction to Game Physics with Box2D
2. Mathematics for Game PhysicsLecture 2.1: Basic Math
Ian ParberryDept. of Computer Science & Engineering
University of North Texas
![Page 2: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/2.jpg)
Introduction to Game Physics with Box2D 2
Contents of This Lecture
1. Geometry2. Vectors3. Orientation
Chapter 2
![Page 3: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/3.jpg)
Introduction to Game Physics with Box2D 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
![Page 4: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/4.jpg)
Introduction to Game Physics with Box2D 4Chapter 2
Geometry
![Page 5: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/5.jpg)
Introduction to Game Physics with Box2D 5Chapter 2
Theorem of Pythagoras
Pythagorean Identity
Vector Magnitude
Law of Cosines
Ball to Ball Collision
Vector Dot Product
Quadratic Equations
Ball to Wall Collision
Ball to Line Collision
2.1.3
2.1.3
2.1.3
2.1.3
2.1.42.2.2
2.2.2 2.2.1
2.2.3
2.1.4
Vector Orientation
3-7
3,4
6,8
3,4
4, 5, 8
Chapters Chapters
Chapters
Chapters
Chapters
![Page 6: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/6.jpg)
Introduction to Game Physics with Box2D 6
Things You Might Remember From School
1. Finding roots of quadratic equations. The function when
2. Trigonometric functions.
Chapter 2
![Page 7: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/7.jpg)
Introduction to Game Physics with Box2D 7
Trig Functions
Chapter 2
h
𝜃𝑎
𝑜
![Page 8: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/8.jpg)
Introduction to Game Physics with Box2D 8
Mnemonics
1. Sohcahtoa2. Some Old Horse Caught Another Horse
Taking Oats Away.3. Some Old Hippy Caught Another Hippy
Toking On Acid.
Chapter 2
![Page 9: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/9.jpg)
Introduction to Game Physics with Box2D 9
Theorem of Pythagoras
𝑎2+𝑏2=𝑐2Chapter 2
𝑐
𝑎
𝑏
![Page 10: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/10.jpg)
Introduction to Game Physics with Box2D 10
Proof of the Theorem of Pythagoras
Chapter 2
𝑏𝑎𝑏
𝑎 𝑎
𝑎 𝑏
𝑏
𝑎𝑏
𝑎
𝑏
𝑐
𝑐 𝑐𝑐
𝑐𝑐
![Page 11: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/11.jpg)
Introduction to Game Physics with Box2D 11
Pythagorean Identity
Chapter 2
1
𝜃cos𝜃sin𝜃
sin2𝜃 +cos2𝜃=1
![Page 12: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/12.jpg)
Introduction to Game Physics with Box2D 12
Law of Cosines
Chapter 2
![Page 13: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/13.jpg)
Introduction to Game Physics with Box2D 13
Proof of the Law of Cosines
Chapter 2
𝐵
𝐶
𝑐sin
𝛼
𝑐 cos𝛼𝐷𝛼
𝑐 𝑎
𝐴
𝐵
𝐶
𝑐sin
𝛼
𝑏−𝑐 cos𝛼𝐷𝛼
𝑎
𝐴
𝑏
![Page 14: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/14.jpg)
Introduction to Game Physics with Box2D 14
More Useful Trig Identities
Chapter 2
![Page 15: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/15.jpg)
Introduction to Game Physics with Box2D 15Chapter 2
Vectors
![Page 16: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/16.jpg)
Introduction to Game Physics with Box2D 16
What’s Our Vector, Victor?
• The Linear Algebra definition of a vector is a list of scalars, typically written with square brackets around them like this [0, 1].
• The dimension of a vector is the number of numbers in the list. For example, [0, 1] has 2 dimensions and [0, 3, 42] has 3 dimensions.
• We mostly use 2-dimensional vectors here because that’s enough to describe 2D space.
• Notation: .Chapter 2
![Page 17: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/17.jpg)
Introduction to Game Physics with Box2D 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
![Page 18: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/18.jpg)
Introduction to Game Physics with Box2D 18Chapter 2
Geometric definition of the vector .
![Page 19: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/19.jpg)
3D Math Primer for Graphics & Game Dev 19
Vector Multiplication by a Scalar
• Result is a vector of the same dimension.• To multiply a vector by a scalar, multiply each
component by the scalar.• For example, .• Vector negation is the same as multiplying by the
scalar –1.• Division by a scalar same as multiplication by the
scalar multiplicative inverse.• That’s the linear algebra definition. The geometry
definition is that a scalar multiplication by stretches a vector by a factor of .
Chapter 2 Notes
![Page 20: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/20.jpg)
3D Math Primer for Graphics & Game Dev 20
Vector Addition: Algebra
• Can add two vectors of the same dimension.• Result is a vector of the same dimension.• To add two vectors, add their components.• For example, • Subtract vectors by adding the negative of the
second vector.
Chapter 2 Notes
![Page 21: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/21.jpg)
Introduction to Game Physics with Box2D 21Chapter 2
Vector Addition: Geometry
+¿ =𝑣�⃗�
�⃗�+ �⃗�
− =𝑣�⃗�
�⃗�−𝑣�⃗�
�⃗�𝑣
𝑣
![Page 22: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/22.jpg)
Introduction to Game Physics with Box2D 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
![Page 23: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/23.jpg)
Introduction to Game Physics with Box2D 23
Vector Magnitude
By the Theorem of Pythagoras, the length of vector , denoted , is .
Chapter 2
√𝑎2 +𝑏
2
𝑎
𝑏
𝑣
![Page 24: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/24.jpg)
Introduction to Game Physics with Box2D 24
Vector Normalization
• A vector is described as being normalized if it has length 1.
• A normalized vector is usually written with a circumflex over it, like this: .
• To normalize a vector simply do a scalar division by its magnitude.
Chapter 2
![Page 25: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/25.jpg)
Introduction to Game Physics with Box2D 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
![Page 26: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/26.jpg)
Introduction to Game Physics with Box2D 26Chapter 2
Orientation
![Page 27: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/27.jpg)
Introduction to Game Physics with Box2D 27
Relative Orientation
Given two vectors and , the orientation from to is the angle in the range to that must be rotated counterclockwise to make it parallel to and pointing in the same direction as .
Chapter 2
𝑣
𝛽
�⃗�
The orientation from to here is .
![Page 28: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/28.jpg)
Introduction to Game Physics with Box2D 28
Orientation
The orientation of a single vector is defined to be the orientation from to .
Chapter 2
𝑣
𝛽
The orientation of here is .
![Page 29: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/29.jpg)
Introduction to Game Physics with Box2D 29
Length & Orientation to Vector
Find the vector of length 1 and orientation .
Chapter 2
1
𝜃cos𝜃
sin𝜃
𝑣
1
𝜃
𝑣
𝑣=[cos𝜃 , sin 𝜃 ]
Step 1.
Step 2. Step 3.
![Page 30: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/30.jpg)
Introduction to Game Physics with Box2D 30
Vector to Orientation
Find the orientation of .
Chapter 2
𝑣=[𝑎 ,𝑏 ]
𝜃
1
𝑎
𝑏
Step 1.
Step 2.
Step 3.
![Page 31: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/31.jpg)
Introduction to Game Physics with Box2D 31
A Gotcha
C++ has a function arctan, but it is only good in the first quadrant.
Orientation of Orientation of
Answer: use atan2(a,b) instead.
Chapter 2
![Page 32: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/32.jpg)
Introduction to Game Physics with Box2D 32
Rotating a Vector
Find the vector that results from rotating vector by angle .
Chapter 2
�̂�
𝛽
�̂�=[𝑢𝑥 ,𝑢𝑦 ]
![Page 33: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/33.jpg)
Introduction to Game Physics with Box2D 33
Rotating a Vector
is the vector that results from rotating vector by angle , where is the orientation of .
Chapter 2
𝛼
�̂�
𝛽
�̂�=[𝑢𝑥 ,𝑢𝑦 ]
[1,0]
![Page 34: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/34.jpg)
Introduction to Game Physics with Box2D 34
Rotating a Vector
Therefore, and
Chapter 2
𝛼
�̂�
𝛽
�̂�=[𝑢𝑥 ,𝑢𝑦 ]
[1,0]
![Page 35: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/35.jpg)
Introduction to Game Physics with Box2D 35Chapter 2
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.
Vector Rotation Code
![Page 36: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/36.jpg)
Introduction to Game Physics with Box2D 36
Optimizing the Rotation Code
Since , ,and similarly .
From Appendix A (and an earlier slide),
Therefore,
From Appendix A,
Therefore,Chapter 2
![Page 37: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/37.jpg)
Introduction to Game Physics with Box2D 37
Optimized Vector Rotation Code
Chapter 2
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.
![Page 38: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/38.jpg)
Introduction to Game Physics with Box2D 38Chapter 2
Conclusion
![Page 39: Intro to Game Physics with Box2D Chapter 2 Part 1](https://reader033.vdocument.in/reader033/viewer/2022061217/54b4814c4a795966028b46c0/html5/thumbnails/39.jpg)
Introduction to Game Physics with Box2D 39
Suggested ReadingSection 2.1
Chapter 2
Suggested Activities
Problems 1-4 from Section 2.5.