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: π· π‘ =πΊ− π³βπΊ π π³βπ½