θ - SSUGames

Dot and Cross
Product
Lecture 4
dot product (2.11)
•
•
•
Symbolically: 𝒗●𝒘
Numeric Interpretation #1:
𝒗●𝒘 = 𝒗𝟏 ∗ 𝒘𝟏 + 𝒗𝟐 ∗ 𝒘𝟐 + ⋯ + 𝒗𝒏 ∗ 𝒘𝒏
Numeric Example:
4 3 −7 ● 0 2 5
= 4 ∗ 0 + 3 ∗ 2 + −7 ∗ 5
= 0 + 6 − 35
= 29
•
Observations
 Result is a scalar
 Commutative
 Obeys the distributive rule:
𝑣= 4 3
1
−8
−7 , 𝑤 = 0 2 5 , 𝑢 = 1 −8 6
𝑢● 𝑣 + 𝑤 = 𝑢●𝑣 + 𝑢●𝑤
6 ● 4 5 −2 = 4 − 24 − 42 + 0 − 16 + 30
4 − 40 − 12 = −62 + 14 = −48
D.P. Application #1
•
What is 𝑣●𝑣?
 v1*v1 + … + vn*vn
 Simply the magnitude of the vector squared!
 But much cheaper than calculating the magnitude and then squaring it (see why?)
𝑣●𝑣 = 𝑣
2
Problem*
•
Given:
o 𝑇 and 𝑈: two spheres (or circle, or hyper-sphere) centers.
o t and u: the radii of the spheres
•
Determine: does T intersect U, but…
o We need to improve our frame-rate and square roots are a big bottleneck
o so no square-roots!
o squares are OK, but minimize them.
𝑈
𝑇
*: I’m intentionally not going to put problem solutions in these slides – take good notes!
Dot Product, cont.

Numeric Interpretation #2:
𝒗●𝒘 = 𝒗 ∗ 𝒘 ∗ 𝐜𝐨𝐬(𝜽)


Where θ is the angle between v and w (if their tails are together)
The SAME number as above, just a different way of interpreting /
calculating it.

Why???

It’s due to the Law of Cosines (Geometry)…
C
A
β
γ
α
B
Law of Cosines is like the Pythagorean Theorem
for any type of triangle (not just right triangles)
𝑪𝟐 = 𝑨𝟐 + 𝑩𝟐 − 𝟐𝑨𝑩𝒄𝒐𝒔(𝜸)
Note: for right triangles, the last term is 0…
Not convinced?

Let 𝑣 = 10

Let’s draw a picture
5
0 𝑎𝑛𝑑 𝑤 = 5
𝑣
θ
θ is ~55 degrees
𝑤
−3
0
𝑤
Example, continued
𝑣 = 10
5 0 𝑎𝑛𝑑 𝑤 = 5 −3
Interpretation#1:
Interpretation#2:
0
Theta is ~55 degrees.
𝑣 • 𝑤 = 10 ∗ 5 + 5 ∗ −3 + 0 ∗ 0 = 50 − 15 = 35
𝑣 • 𝑤 = 𝑣 ∗ 𝑤 ∗ cos(θ)
𝑣 = 102 + 52 + 02 = 100 + 25 = 125 ≈ 11.18
𝑤 = 52 + (−3)2 +02 = 25 + 9 = 31 ≈ 5.57
𝑣 • 𝑤 = 𝑣 ∗ 𝑤 ∗ cos θ = 11.18 ∗ 5.57 ∗ cos 55
= 35.75
(we estimated the angle (it’s more like 55.8 degrees) and rounded off
the magnitudes, otherwise they'd be identical)
Application of D.P #2
(Calculation of θ)
•
We can come up with an exact value for θ, given any two vectors using a
little algebra and our two definitions of dot product.
   
v  w  v * w * cos( )
 
 
v * w * cos( )
v w
  
 
v *w
v *w
 
v w
   cos( )
v *w
 
v w
1
cos (   )  cos 1 (cos( ))
v *w
θ=
𝑐𝑜𝑠 −1 (
𝑣⦁𝑤
)
𝑣 ∗ 𝑤
Application of Dot Product #3

θ is the angle between v and w. In each of these cases, think of what
cos(θ) would be…
w
Acute
cos(120)=-0.5
θ≈12
θ0
w
θ
θ≈45
cos(45)=0.707
v
v
θ
v
θ≈12 cos(120)=-0.5
0
w
w
Obtuse
θ≈18 cos(180)=-1
0
θ
θ
v
NOTE: We never have to deal
with the case of θ > 180.
Why??
w
θ≈90
cos(90)=0
v
Right
.
D.P. application #3
•
We can classify the relationship between two (non-zero) vectors cheaply:
If 𝑣●𝑤 < 0, the vectors make an obtuse angle
If 𝑣●𝑤 > 0, the vectors make an acute angle
If 𝑣●𝑤 = 0, the vectors make a right angle
Problem
•
Given:
o 𝐺: a guard’s position
o θ: the guard’s orientation (if in 2d, this is the angle he is facing; if in 3d, this is a rotation
about the y axis, where y is up and the x/z plane is the ground).
o Note the guard can see to her left / right (and up/down if in 3d) forming a line (or plane in 3d)
dividing the non-visible from visible areas).
o 𝑃: the player’s position
o
Problem: Symbolically determine if the guard can see the player.
o Restriction: no inverse trig calls!
Application #4 (Projection)
•
When you project one vector onto another, you produce two new vectors: a
perpendicular and a parallel.
•
Say we're projecting 𝑤 onto 𝑣
•
We'll get:
o 𝒘|| : the parallel projection of w onto v.
o 𝒘┴ : the perpendicular projection of …
o such that 𝒘 = 𝒘|| + 𝒘┴
𝒘┴
𝑤
𝒘||
𝑣
Application #4 (Projection)
•
Recall our trig identities:
sin 𝜃 =
•
𝑂
𝐻
cos 𝜃 =
𝐴
𝐻
tan 𝜃 =
𝑂
𝐴
So…in the picture below:


𝑤 is the length of the hypotenuse (H)
𝑤┴ is the length of the opposite side (O)

𝑤|| is the length of the adjacent side (A)
•
Also recall that 𝑣●𝑤 = 𝑣
•
If we substitute cos 𝜃 = 𝐻 =
𝑤 cos(𝜃)
𝐴
 𝑣●𝑤 = 𝑣
𝑤 ∗
𝑤||
𝑤
𝑤||
𝑤
into the d.p. form…
𝒘┴
 [cont. on next slide]
𝑤
𝒘||
θ
𝑣
Application #4 (Projection)
In summary:
𝑣|| =
•
•
[from last slide…]
𝑣●𝑤 = 𝑣
𝑤 ∗
𝑣┴ = 𝑣 − 𝑣||
𝑤||
𝑤
 Now, some algebra produces:
𝑤|| =
If we actually want the vector 𝑤|| …
𝑣●𝑤
𝑤|| =
∗𝑣
𝑣
•
𝑣⦁𝑤
𝑣
𝑣⦁𝑣
𝑣●𝑤
𝑣
=
𝑣•𝑤
𝑣
𝑣•𝑣
Recall (from vector addition):
 𝑤 = 𝑤|| + 𝑤┴
 Which makes it easy to calculate 𝑤┴ :
𝒘┴
𝑤
𝑤┴ = 𝑤 − 𝑤||
𝒘||
θ
𝑣
Application #4, cont.
•
It works even if they make an obtuse angle
𝑟
𝑟┴
𝑟||
𝑠
Problem
•
Given:
o 𝐵: a “beamos”’s position
o α: beamos’s rotation
o 𝐿: link’s (feet) position
o n: link’s bounding radius
o Problem: Symbolically determine if the beamos’s beam hits link
(regardless of distance). You can assume this is a 2d problem (although if in
3d, think of α as the y-axis rotation of the beamos)
o Bonus: Determine the closest intersection point, 𝑃, where the laser hits
link’s bounding circle / sphere.
Challenge Problem
•
You are given:
o𝑪: the position of your character
o 𝑫: the direction your character is facing
o𝑬: the position of an enemy
oΘ: the half-spread of a shotgun blast (around D). < 45◦
(the full blast-spread is 2 Θ)
on: the distance at which the shotgun does no damage
om: the damage the shotgun blast does at point-blank
range (the damage falls off linearly the farther away we
go)
•
Problem: Symbolically find the actual amount of damage the shotgun does to
the enemy. Bonus: minimize the use of trig functions and
don’t use any inverse trig (both are very slow).
Problem (picture)
n
θ
Intermission: “Handedness”𝑦 of 3d coordinate systems
•
By convention, most people draw a 2d coordinate system this way:
•
In 3d, we’ll have a third axis
 By convention, it’s called the “z” axis.
 Which way does it go?
 Towards you?
𝑥
𝑦
𝑥
𝑧 (OpenGL, Linear Algebra texts)
 This is called a right-handed system
 Away from you?
 This is called a left-handed system (DirectX)
𝑦
𝑧
𝑥
 Note: The axes might not always look just like this (think blender) – the key is the relationship between axes.
•
Test:




Line thumb up with x
Line index finger up with y
z will point in the direction of your middle finger.
[Try a few on the board]
Cross Product (2.12)
•
Symbolically: 𝒗 𝒙 𝒘
•
•
•
•
•
Again, NOT, NOT, NOT vector multiplication!
The result is a vector.
Note: cross product is not commutative.
Only makes sense in 3d (at least for us)
Numerically:
𝑣𝑥
𝑣𝑦
𝑣𝑧 𝑥 𝑤𝑥
𝑤𝑦
𝑤𝑧
𝑣𝑦 ∗ 𝑤𝑧 − 𝑣𝑧 ∗ 𝑤𝑦
= 𝑣𝑧 ∗ 𝑤𝑥 − 𝑣𝑥 ∗ 𝑤𝑧
𝑣𝑥 ∗ 𝑤𝑦 − 𝑣𝑦 ∗ 𝑤𝑥
𝑇
(the little T means "transpose" – flip it sideways [it's easier to read this way])
• [Do a numeric example on the board]
• Note: If v and w are parallel or anti -parallel, the result is the zero vector.
• [ D o a n u m e r i c e x a m pl e o f t h i s t o o ]
• Cross Product is anti-commutative
• 𝑣 𝑥 𝑤 = −(𝑤 𝑥 𝑣)
C.P. mnemonic
result x  v y * wz  vz * wy
Increase the subscripts by 1, “wrapping” around from z=>x
result y  vz * wx  vx * wz
Increase the subscripts by 1, “wrapping” around from z=>x
result z  vx * wy  v y * wx
Memorize Me
Cross Product, cont.
•
Graphically:
 Note that 𝑣 and 𝑤 (3d, non-parallel, non-zero) define a plane.
 The cross product is a vector perpendicular to that plane.
 [Discuss the “curl” method to determine which way it goes]
 The length of that vector is: 𝑣
𝑤 sin(θ)
 Note: If v and w are perpendicular, sin(θ) is just one.
 We’ll use this later in ray-triangle hit-detection. For now, the direction is all we care
about.
𝑤
𝑣
𝑤 sin(θ)
θ
𝑣
Practice Problem
•
Given: 𝐴, 𝐵, and 𝐶, which are three ordered points along a character’s path
(all of them are on the xy plane).
•
Problem: Did the character take a left / right / straight turn at point B?
Turned left
Turned right
<= Which is it? =>
𝐶1
𝐶2
y
𝐵
𝐴
x
𝐵
𝐴
Coordinate Spaces
•
Up to now, we consider everything in global coordinates
•
Sometimes it is more convenient to answer questions in a local coordinate
space.
 Is an enemy in front of me?
 How many units to my left is this wall?
 …
•
We can use our new tools to assist w/ local / global coordinates
 [Calculating a 3rd axis from two direction]
 Hint: Uses cross product
 [Converting from local => world position]
 Hint: Uses lecture 2 stuff only.
 [Converting from world => local position]
 Hint: I’d recommend dot-product here