Top Banner
Our Friend the Dot Product Jim Van Verth ([email protected])
36

Our Friend the Dot Product Jim Van Verth ([email protected])

Mar 26, 2015

Download

Documents

Lauren Hahn
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Our Friend the Dot Product

Jim Van Verth ([email protected])

Page 2: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

So… Why The Dot Product?

• Probably the single most important vector operation

• The Ginzu™ knife of graphics It slices, it dices, it has 1001 uses

• Understanding is a 3-D sword… • So

Define Utilize

Page 3: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Background

• Assume you know something about vectors

• Will be skipping some steps for time

• Just follow along

Page 4: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

• One of a class of vector functions known as inner products Means that it satisfies various axioms

Useful for dot product math

What is the Dot Product?

Page 5: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

• In Euclidean space, define dot product of vectors a & b as

where

|| || == length

== angle between a & b

Definition #1: Coordinant-Free

a

b

Page 6: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Definition #2: Coordinant-Dependant

• By using Law of Cosines, can compute coordinate-dependant definition in 3-space:

or 2-space:

Page 7: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Use #1: Measure Angles

• Look at def #1:

• Can rearrange to compute angle between vectors:

• A little expensive – cheaper tests available

Page 8: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Use #1: Measure Angles

• Rather than determining exact angle, can use sign to classify angle

• ||a|| and ||b|| are always non-negative, so sign depends on cos , therefore a • b > 0 if angle < 90° a • b = 0 if angle = 90° (orthogonal) a • b < 0 if angle > 90°

Page 9: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Angles: View Testing

• Simple view culling

• Suppose have view vector v and vector t to object in scene (t = o - e)

• If v • t < 0, object behind us, cullv

to

e

Page 10: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Angles: View Testing

• Note: doesn’t work well for large objects objects close to view plane

• Best for AI, not renderingv

t

o

e

Page 11: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Angles: Collision Response

• Have normal n (from object A to object B), relative velocity va-vb

• Three cases of contact: Separating

(va-vb) • n < 0

Colliding(va-vb) • n > 0

Resting(va-vb) • n = 0

Page 12: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Angles: View Cone

• Test against view cone – store cosine of view angle and compare

• If d • L < cos v, cull

• Nice for spotlights

• Must normalize L

d

v

^ ^

^

^

Page 13: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Angles: Collinear Test

• Test for parallel vectors If v and w parallel & pointing same

direction, v • w ~ 1 If v and w parallel & pointing opposite

direction, v • w ~ -1 Has problems w/floating point, though And you have to normalize

^ ^^ ^

^ ^^ ^

Page 14: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Use #2: Projection

• Suppose want to project a onto b

• Is part of a pointing along b

• Represented as

a

b

Page 15: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Use #2: Projection

• From trig

• Now multiply by normalized b, so

a

b

Page 16: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Use #2: Projection

• So have

• If b is already normalized (often the case), then becomes

Page 17: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Projection: OBB Collision

• Idea: determine if separating plane between boxes exists

• Project box extent onto plane normal, test against projection btwn centers

cvbvav

a b

c

Page 18: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Projection: OBB Collision

• To ensure maximum extents, take dot product using only absolute values

• Check against axes for both boxes, plus cross products of all axes

• See Gottschalk for more details

Page 19: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Use #2: Projection

• Can use this to break a into components parallel and perpendicular to b

a

b

Page 20: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Projection: Line-Point Distance

• Line defined by point P and vector v

• Break vector w = Q – P into w and w||

• w|| = (w v) v

• ||w||2 = ||w||2 – ||w||||2

^

^ ^

v

Q

P

w

w||

w

^

Page 21: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Projection: Line-Point Distance

• Final formula:

• If v isn't normalized:

Page 22: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Use #3: Hidden Dot Products

• There are dot products everywhere… just need to know to look for them

• Examples: Plane equation Matrix multiplication Luminance calculation

Page 23: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Planes

• Defined by normal n = (a, b, c) point on plane P0=(x0, y0, z0)

• Plane equation ax+by+cz+d = 0 d=-(ax0 + by0 + cz0)

• Dot products!

Page 24: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Planes

• Why a dot product?

• Plane is all points P = (x, y, z) passing through P0 and orthogonal to normal

• Can express asn

PP0

Page 25: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Planes

• Plane is all points P = (x, y, z) such that

• Can rewrite as

or

Page 26: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Planes

• Can use plane equation to test locality of point

• If n is normalized, gives distance to plane

n ax+by+cz+d > 0

ax+by+cz+d = 0

ax+by+cz+d < 0

Page 27: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Planes

• Project point to plane

• Assuming n normalized,

n

vv||

P

P0 P’

Page 28: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

v - 2v|| -v||

Planes

• Want to mirror point across a plane

• Take dot product of vector with normal

• Subtract twice from vectorn

vv||

Page 29: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Matrix Product

• In general, element ABij is dot product of row i from A and column j from B

or

Page 30: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Matrix Product

• So what does this mean?

• Beats me… but it’s cool

Page 31: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Luminance

• Can convert RGB color to single ‘luminance’ value by computing

• In this case, a projection of sorts

• “Luminance” vector not normalized

Page 32: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Use #4: Perpendicular Dot Product

• 2D cross product, sort of

• Perpendicular takes 2D vector and rotates it 90 ccw

• Take another vector and dot it, get

• Looks like z term from cross product

Page 33: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Perpendicular Dot Product

• Result is

• So if you want to compute signed angle from v0 to v1, could do

(works for 3D, too, just need to get length of cross product)

Page 34: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

• Another use: sign of perp dot product indicates turning direction

• Have velocity v, direction to target d

• If v• d is positive, turn left

• If negative, turn right

Perpendicular Dot Product

vd d

v

v v

Page 35: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

Conclusion

• Dot product is cool

• Dot product has many uses

• Use it heavily and often

Page 36: Our Friend the Dot Product Jim Van Verth (jim@essentialmath.com)

Essential Math for Games

References

• Van Verth, James M. and Lars Bishop, Essential Mathematics for Games and Interactive Applications, Morgan Kaufmann, 2004.

• Anton, Howard and Chris Rorres, Elementary Linear Algebra, 7th Ed, Wiley & Sons, 1994.

• Axler, Sheldon, Linear Algebra Done Right, Springer Verlag, 1997.