The Mathematics of Gaming

advertisement
THE MATH LECTURE
(BOILED DOWN, YET LIGHTLY SALTED)
CGDD 4003
OVERVIEW
•
For 2D games, we use a lot of trigonometry
•
For 3D games, we use a lot of linear algebra and 2D concepts
•
For advanced concepts, we use a lot of calculus and 3D concepts
•
Math is used to:
• Translate geometry
• Rotate geometry
• Scale geometry
• Calculate light
• Determine the position of enemies (such as their “AI”)
• Run physics engines (collisions)
TRIG YOU PROBABLY ALREADY KNOW…
𝑦
β„Ž
π‘₯
cos θ =
β„Ž
𝑦
tan θ =
π‘₯
π‘₯
cot θ =
𝑦
sin θ =
y (opposite)
θ
x (adjacent)
•
Trig functions are defined by the relationships of the sides of a triangle
•
Commonly used: sine, cosine, tangent and cotangent
•
Not often used: secant, cosecant, cotangent
•
Can work in degrees or radians
•
π‘₯ 2 + 𝑦 2 = 𝑧 2 and 𝑧 2 = π‘₯ 2 + 2π‘₯π‘¦π‘π‘œπ‘ πœƒ
TRIG YOU PROBABLY KNOW
•
What is a radian? Look at π‘Ÿ below
•
𝐢 = 2πœ‹π‘Ÿ
•
There are 2πœ‹ radians in a circle!
•
Therefore
π‘Ÿ
• π‘Ÿπ‘Žπ‘‘π‘–π‘Žπ‘›π‘  =
πœ‹
180
× π‘‘π‘’π‘”π‘Ÿπ‘’π‘’π‘ 
• π‘‘π‘’π‘”π‘Ÿπ‘’π‘’π‘  =
180
πœ‹
× π‘Ÿπ‘Žπ‘‘π‘–π‘Žπ‘›π‘ 
π‘Ÿ
π‘Ÿ
THINGS YOU PROBABLY REMEMBER
(REVIEW OF SIN AND COS)
sin 90 = 1
sin 180 = 0
sin 0 = 0
sin 270 = −1
THINGS YOU PROBABLY REMEMBER
(REVIEW OF SIN AND COS)
cos 90 = 0
cos 180 = −1
cos 0 = 1
cos 270 = 0
ROTATING POINTS IN 2D
𝑦
𝑃′
Rotate point 𝑃 by
α degrees to get 𝑃’
π‘Ÿ
𝛼
𝑦 ′ = π‘Ÿπ‘ π‘–π‘›(𝛼)
𝑃
π‘₯ ′ = π‘Ÿπ‘π‘œπ‘ (𝛼)
Note: you may need to add an offset if the origin isn’t (0,0)
π‘₯
OTHER TRIG YOU SHOULD KNOW
•
sin −𝛼 = − sin 𝛼
•
cos −𝛼 = cos 𝛼
•
tan −𝛼 = − tan 𝛼
•
Cosine and sine produce the same values by adding/subtractng
πœ‹
• cos 𝛼 = sin 𝛼 + 2
•
If π‘₯ 2 + 𝑦 2 = β„Ž 2 , then
•
π‘₯2
β„Ž2
+
𝑦2
β„Ž2
= 1 and therefore…
• 𝑠𝑖𝑛 2 𝛼 + π‘π‘œπ‘  2 𝛼 = 1 and therefore…
• sin 𝛼 =
1 − π‘π‘œπ‘  2 (𝛼)
πœ‹
2
INVERSE TRIG
•
“Undoes” a trig function
•
If you know the sin 𝛼 = π‘£π‘Žπ‘™π‘’π‘’ and need 𝛼, then asin π‘£π‘Žπ‘™π‘’π‘’ = 𝛼
•
Also denoted as 𝑠𝑖𝑛 −1 (𝛼)
•
Common library operations:
• π‘Žπ‘ π‘–π‘› – or “arcsine”
• π‘Žπ‘π‘œπ‘  – or “arccosine”
• π‘Žπ‘‘π‘Žπ‘› – or “arctangent”
QUESTION
•
How would I describe the 2D difference in location of a player and an enemy?
You
Nazi-Bear-Zombie
QUESTION
•
How would I describe the 2D difference in location of a player and an enemy?
(x2, y2)
(x1, y1)
QUESTION
•
How would I describe the 2D difference in location of a player and an enemy?
(x2, y2)
(x1, y1)
𝑑𝑖𝑓𝑓 = [ π‘₯1 − π‘₯2 , 𝑦1 − 𝑦2 ]
…or 𝑑𝑖𝑓𝑓 = [βˆ†π‘₯, βˆ†π‘¦]
Note: a very useful 2D function is atan2 (y, x) which gives you the angle!
3D STUFF YOU PROBABLY ALREADY KNOW…
•
A 3D point contains x, y and z
•
A vertex is essentially a 3D point
•
An edge connects two vertices
•
Triangles are comprised of 3 vertices
•
Scalar values are just single numbers (e.g. 1, 2, 3)
•
We need to be able to:
• Translate (move) vertices
• Rotate vertices
• Scale vertices
π‘‰π‘’π‘π‘‘π‘œπ‘Ÿπ‘ 
•
We can store this difference into a vector
•
Used to store difference in points, velocities, forces, etc., in 3D graphics and audio
•
Vectors contain both:
• Direction
• Magnitude
•
Row representation [x, y, z] (multiple columns)
15 − 4 3
•
Column vector has multiple rows
15
−4
3
•
A vector is often denoted with an arrow above it (e.g. 𝑣) or 𝑽 (bold)
OPERATIONS ON VECTORS
•
Do this component-wise
•
Therefore, the vectors must be the same size
•
Adding example
•
1 3 5 + 10 4 − 3 = 11 7 2
• Subtraction works the same way
+
=
OPERATIONS ON VECTORS
•
Multiplying by a scalar
• 6 * 1 3 5 = 6 18 30 (note: -1 reverses direction)
•
What about multiplication?
• This isn’t really defined, but we have
• Dot product
• Cross product
MAGNITUDE AND NORMALIZATION OF VECTORS
•
Normalization is a fancy term for saying the vector should be of length 1
•
Magnitude is just its length, denoted 𝑉
•
Example for 1 3 5
• mag = π‘₯ 2 + 𝑦 2 + 𝑧 2
• mag = 12 + 32 + 52 = 35 = ~5.916079
•
To normalize the vector, divide each component by its magnitude
•
Example from above
•
1 3 5
5.916079
= 0.169 0.50709 0.84515
• Magnitude of 0.169 0.50709 0.84515 = ~1 denoted 𝑉
THE DOT PRODUCT
•
Also called the inner or scalar product (because result is a scalar)
•
Multiply component-wise, then sum together
•
Denoted using the dot operator 𝑒 βˆ™ 𝑣
•
Example
•
•
1 2 4 βˆ™ 8 1 − 2 = 1 ∗ 8 + 2 ∗ 1 + 4 ∗ −2 = 2.0
Why is this so cool?
• It will tell us the cosine of the angle θ between two vectors!
• Basis of almost all lighting calculations!
• First, they should be normalized
πœƒ
DOT PRODUCT EXAMPLE
•
Assume we have two vectors:
𝑣
• 𝑒= 1 0 0
• 𝑣= 0 1 0
•
These vectors are already normalized
•
We expect the angle to be 90°
•
Dot product is:
• π‘’βˆ™π‘£ = 1∗0 + 0∗1 + 0∗0 =0
• acos 0 = 90°
πœƒ
𝑒
DOT PRODUCT EXAMPLE 2
•
Assume we have two vectors:
• 𝑒= 1 0 0
• 𝑣 = −1 0 0
•
•
We expect the angle to be 180°
Dot product is:
πœƒ
𝑣
• 𝑒 βˆ™ 𝑣 = 1 ∗ −1 + 0 ∗ 0 + 0 ∗ 0 = −1
• acos −1 = 180°
𝑒
DOT PRODUCT EXAMPLE 3
•
Assume we have two vectors:
• 𝑒= 1 0 0
• 𝑣= 1 0 0
•
We expect the angle to be 0°
•
Dot product is:
• π‘’βˆ™π‘£ = 1∗1 + 0∗0 + 0∗0 =1
• acos 1 = 0°
𝑒 𝑣
PROJECTION
•
Can also be used to calculate the projection of one vector onto another
𝑉
𝛼
Length is:
π‘‰βˆ™π‘Š
𝑉 cos 𝛼 =
π‘Š
π‘Š
Then, multiply by normalized π‘Š/ π‘Š
π‘π‘Ÿπ‘œπ‘—π‘€ 𝑉 =
π‘‰βˆ™π‘Š
π‘Š
π‘Š 2
𝑐
CROSS PRODUCT
(AKA THE “VECTOR” PRODUCT)
•
Results in a new vector that is perpendicular to the other two
•
Denoted with the × operator
•
Calculations:
π‘Ž × π‘ = [π‘Žπ‘¦ 𝑏𝑧 − π‘Žπ‘§ 𝑏𝑦
π‘Ž
πœƒ
π‘Žπ‘§ 𝑏π‘₯ − π‘Žπ‘₯ 𝑏𝑧
𝑖
π‘Ž × π‘ = π‘Žπ‘₯
𝑏π‘₯
•
𝑗
π‘Žπ‘¦
𝑏𝑦
π‘Žπ‘₯ 𝑏𝑦 − π‘Žπ‘¦ 𝑏π‘₯ ]
π‘˜
π‘Žπ‘§
𝑏𝑧
Interesting:
• If π‘Ž × π‘ = 𝑐 then 𝑏 × π‘Ž = −𝑐
• The magnitude (length) of the new vector is the sine of the angle (if normalized)
• Used to calculate the normal of a plane
𝑏
MATRICES
•
The foundation of all geometric operations (translation, rotation, scaling, skewing…)
•
Have multiple rows and columns
•
Add and subtract similar to vectors, and must be same size
•
Below is an identity matrix 𝐼 (and is also diagonal)
1.0 0.0 0.0 0.0
0.0 1.0 0.0 0.0
0.0 0.0 1.0 0.0
0.0 0.0 0.0 1.0
•
We can multiply a matrix
• with another matrix
• with a vector of “appropriate” dimension
MATRICES
•
Have a transpose (denoted 𝑀 𝑇 ) where 𝑀 𝑇 𝑖𝑗 = 𝑀𝑗𝑖
•
There’s also an inverse matrix (denoted 𝑀 −1 ) where 𝑀𝑀 −1 = 𝐼
• Not all matrices are invertible
• Can check by getting the determinant of the matrix (looking for non-zero)
𝑑𝑒𝑑
π‘š11
π‘š21
π‘š31
π‘Ž
𝑐
π‘š12
π‘š22
π‘š32
𝑏
π‘Ž
=
𝑑
𝑐
𝑏
= π‘Žπ‘‘ − 𝑏𝑐
𝑑
π‘š13
π‘š
π‘š23 = π‘š11 22
π‘š32
π‘š33
π‘š23
π‘š21
−
π‘š
12 π‘š
π‘š33
31
π‘š23
π‘š21
+
π‘š
13 π‘š
π‘š33
31
π‘š22
π‘š32
A FEW SPECIAL MATRICES
(USED FOR ROTATION)
1
0
0
0 cos(𝛼) −sin(𝛼)
𝑅π‘₯ 𝛼 =
0 sin(𝛼) cos(𝛼)
0
0
0
cos(𝛽) 0 sin(𝛽)
0
1
0
𝑅𝑦 𝛽 =
−sin(𝛽) 0 cos(𝛽)
0
0
0
0
0
0
1
0
0
0
1
cos(𝛾) −sin(𝛾) 0
sin(𝛾) cos(𝛾) 0
𝑅𝑧 𝛾 =
0
0
1
0
0
0
0
0
0
1
MATRIX MULTIPLICATION
•
Calculating an element is just like the dot product!
•
Let’s say that we want to rotate the point [1 0 0] around the z axis by 90 degrees
•
Points are always stored with a 1 for the 4 th element
cos(𝛾)
sin(𝛾)
0
0
−sin(𝛾)
cos(𝛾)
0
0
0
0
1
0
0
0
0
1
1
?
0
?
=
0
?
1
?
MATRIX MULTIPLICATION
•
Calculating an element is just like the dot product!
•
Let’s say that we want to rotate the point [1 0 0] around the z axis by 90 degrees
•
Points are always stored with a 1 for the 4 th element
cos(90) −sin(90) 0 0
sin(90) cos(90) 0 0
0
0
1 0
0
0
0 1
1
0
0
?
=
0
?
1
?
= (1 ∗ cos 90 ) + (0 ∗ − sin 90 ) + 0 ∗ 0 + 0 ∗ 1 = 0
MATRIX MULTIPLICATION
•
Calculating an element is just like the dot product!
•
Let’s say that we want to rotate the point [1 0 0] around the z axis by 90 degrees
•
Points are always stored with a 1 for the 4 th element
cos(90) −sin(90) 0 0
sin(90) cos(90) 0 0
0
0
1 0
0
0
0 1
1
0
0
1
=
0
?
1
?
= (1 ∗ sin 90 ) + (0 ∗ cos 90 ) + 0 ∗ 0 + 0 ∗ 1 = 1
MATRIX MULTIPLICATION
•
Calculating an element is just like the dot product!
•
Let’s say that we want to rotate the point [1 0 0] around the z axis by 90 degrees
•
Points are always stored with a 1 for the 4 th element
cos(90) −sin(90) 0 0
sin(90) cos(90) 0 0
0
0
1 0
0
0
0 1
= (1 ∗ 0) + (0 ∗ 0) + 0 ∗ 1 + 0 ∗ 1 = 0
1
0
0
1
=
0
0
1
?
MATRIX MULTIPLICATION
•
Calculating an element is just like the dot product!
•
Let’s say that we want to rotate the point [1 0 0] around the z axis by 90 degrees
•
Points are always stored with a 1 for the 4 th element
cos(90) −sin(90) 0 0
sin(90) cos(90) 0 0
0
0
1 0
0
0
0 1
= (1 ∗ 0) + (0 ∗ 0) + 0 ∗ 0 + 1 ∗ 1 = 1
1
0
0
1
=
0
0
1
1
TRANSLATION
•
It’s a piece of cake, because the 4 th column is the translation!
•
Imagine we want to translate the point 10 5 − 7 by the values x, y and z. Then:
1
0
0
0
0
1
0
0
0 π‘₯
0 𝑦
1 𝑧
0 1
10 + π‘₯
10
5+𝑦
5
=
−7
−7 + 𝑧
1
1
Note: the 4th component must be a 1 for a point, but a 0 if it’s a direction!
SCALING
•
It’s a piece of cake too, because that’s the diagonal!
•
We can scale along just one axis, or more than one!
•
Imagine we want to scale the point 2 9 4 by the values x, y and z. Then:
π‘₯
0
0
0
0
𝑦
0
0
0
0
𝑧
0
0
0
0
1
2π‘₯
2
9𝑦
9
=
4
4𝑧
1
1
LINES
•
Lines are usually described using:
• A point 𝑺 in space
• A direction 𝑽 along which the line runs
•
Thus, the set of all points on the line is 𝑷 𝑑 = 𝑺 + 𝑑𝑽
•
Two lines are parallel if their vectors are the same
•
Non-parallel lines do not necessarily intersect (could be skew)
DISTANCE OF A POINT TO A LINE
𝑷
𝑷−𝑺
𝑑
𝑺
𝑽
π‘π‘Ÿπ‘œπ‘—π‘£ (𝑷 − 𝑺)
•
We can use the Pythagorean theorem
𝑑 2 = (𝑷 − 𝑺)2 − π‘π‘Ÿπ‘œπ‘—π‘£ 𝑷 − 𝑺
(𝑷 − 𝑺) βˆ™ 𝑽
= (𝑷 − 𝑺)2 −
𝑽
𝑽 2
•
… and then take the square root
2
2
𝑡
PLANES
•
Planes are usually described using:
• A point 𝑷 in space
𝑷
𝑸
• A normal vector 𝑡 passing through 𝑷
• Aka - the set of all points 𝑸 where 𝑡 βˆ™ 𝑸 − 𝑷 = 0
•
Usually written as:
• 𝐴π‘₯ + 𝐡𝑦 + 𝐢𝑧 + 𝐷 = 0
• Where 𝐴, 𝐡, and 𝐢 are the π‘₯, 𝑦 and 𝑧 components of 𝑡 and 𝐷 = −𝑡 βˆ™ 𝑷
• Also written as 𝐋 = 𝑡, 𝐷 . Very important later
•
Hey! What if we only have 3 points of a triangle?
DISTANCE OF POINT TO PLANE
•
If 𝑡 is normalized, then the distance 𝑑 of any point 𝑸 is given by
• 𝑑 =π‘΅βˆ™π‘Έ+𝐷
•
𝑡
If we add a 4 th dimension to our point 𝑸 – giving it a 1, then
• 𝑑 =π‘³βˆ™π‘Έ
• The point is in the plane if 𝑑 = 0
𝑷
𝑸
INTERSECTION OF A LINE AND PLANE
(LAST SLIDE – HURRAY!)
•
Where does this occur in games?
•
For any point P on a plane, it must satisfy 𝑳 βˆ™ 𝑷 = 0
•
Remember equation for line is 𝑃 𝑑 = 𝑆 + 𝑑𝑉
•
So the point that’s on the line AND the plane must mean:
π‘³βˆ™π‘· 𝑑 =0
•
Then
𝑳 βˆ™ 𝑺 + 𝑑𝑽 = 0
•
Solving for 𝑑:
π‘³βˆ™π‘Ί+𝑑 π‘³βˆ™π‘½ =0
−𝑳 βˆ™ 𝑺 = 𝑑 𝑳 βˆ™ 𝑽
π‘³βˆ™π‘Ί
−
=𝑑
π‘³βˆ™π‘½
•
Therefore:
𝑷 𝑑 =𝑺−
π‘³βˆ™π‘Ί
𝐕
π‘³βˆ™π‘½
Download