Vectors.pptx

advertisement
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
Download