VECTORS JEFF CHASTINE 1 ππππ‘πππ • A mathematical structure that has more than one “part” (e.g. an array) • 2D vectors might have x and y • 3D vectors might have x, y and z • 4D vectors might have x, y, z and w • Vectors can represent a point in space • Vectors commonly represent both: • Direction • Magnitude JEFF CHASTINE 2 ππππ‘πππ • A vector is often denoted with an arrow above it (e.g.π’) • Row representation [x, y, z] (multiple columns) 15 − 4 3 • Column vector has multiple rows 15 −4 3 • Vectors will be used in lighting equations JEFF CHASTINE 3 EXAMPLE • How would I describe the 2D difference in location of a player and an enemy? You Man-Bear-Pig JEFF CHASTINE 4 EXAMPLE • How would I describe the 2D difference in location of a player and an enemy? (x2, y2) (x1, y1) JEFF CHASTINE 5 EXAMPLE • 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! 6 INTERPRETATION • ππππ has both magnitude and direction (x2, y2) (x1, y1) Magnitude (length) JEFF CHASTINE 7 INTERPRETATION • ππππ has both magnitude and direction (x2, y2) (x1, y1) (βx, βy) (0, 0) JEFF CHASTINE 8 ADDING/SUBTRACTING 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 + JEFF CHASTINE = 9 MULTIPLICATION? • Multiplying by a scalar (a single number) • 6 * 1 3 5 = 6 18 30 • What about multiplication? • This isn’t really defined, but we do have • Dot product • Cross product JEFF CHASTINE 10 MAGNITUDE AND NORMALIZATION OF VECTORS • Normalization is a fancy term for saying the vector should be of length 1 • Magnitude is just its length and 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 JEFF CHASTINE 11 THE DOT PRODUCT • Also called the inner or scalar product • 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? • If normalized, it’s the cosine of the angle θ between the two vectors! • Use acos(π) to “undo” that π • Basis of almost all lighting calculations! JEFF CHASTINE 12 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° JEFF CHASTINE 13 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° JEFF CHASTINE 14 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° JEFF CHASTINE 15 PROJECTION • Can also be used to calculate the projection of one vector onto another π πΌ Length of projection is: πβπ π cos πΌ = π JEFF CHASTINE π Then, multiply by normalized π/ π πππππ€ π = πβπ π π 2 16 π CROSS PRODUCT • Gives us 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) JEFF CHASTINE 17 π CROSS PRODUCT • Gives us 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) JEFF CHASTINE 18 π CROSS PRODUCT • Gives us 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) JEFF CHASTINE 19 π CROSS PRODUCT • Gives us 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) JEFF CHASTINE 20 FUN QUESTIONS • How do we find the normal of a triangle? • How can we determine if a polygon is facing away from the camera? JEFF CHASTINE 21 FUN QUESTIONS • How do we find the normal of a triangle? • How can we determine if a polygon is facing away from the camera? P0 P1 P2 JEFF CHASTINE 22 FUN QUESTIONS • How do we find the normal of a triangle? • How can we determine if a polygon is facing away from the camera? π’ P1 P0 π£ P2 Make some vectors… JEFF CHASTINE 23 FUN QUESTIONS • How do we find the normal of a triangle? • How can we determine if a polygon is facing away from the camera? P0 π’ P1 π£ P2 Make some vectors… JEFF CHASTINE 24 FUN QUESTIONS • How do we find the normal of a triangle? • How can we determine if a polygon is facing away from the camera? π’ × π£ P0 π’ P1 π£ P2 Take the cross product JEFF CHASTINE 25 FUN QUESTIONS • How do we find the normal of a triangle? • How can we determine if a polygon is facing away from the camera? P0 π P1 P2 JEFF CHASTINE 26 FUN QUESTIONS • How do we find the normal of a triangle? • How can we determine if a polygon is facing away from the camera? P0 π P1 P2 JEFF CHASTINE 27 FUN QUESTIONS • How do we find the normal of a triangle? • How can we determine if a polygon is facing away from the camera? P0 π camera P1 P2 JEFF CHASTINE 28 FUN QUESTIONS • How do we find the normal of a triangle? • How can we determine if a polygon is facing away from the camera? P0 π camera P1 P2 JEFF CHASTINE 29 FUN QUESTIONS • How do we find the normal of a triangle? • How can we determine if a polygon is facing away from the camera? πΌπ acos π β πππ < 90°, ππ‘ ′ π π£ππ ππππ P0 π camera P1 P2 JEFF CHASTINE Assuming π and πππ are normalized 30 A FINAL NOTE • Can multiply a matrix and vector to: • Rotate the vector • Translate the vector • Scale the vector • Etc.. • This operation returns a new vector JEFF CHASTINE 31 THE END Image of a triangle facing away from the camera JEFF CHASTINE 32