Top Banner
Geometric Intuition Randy Gaul
58

Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Dec 25, 2015

Download

Documents

Victoria George
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: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Geometric Intuition

Randy Gaul

Page 2: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

• Vectors, Points and Basis Matrices• Rotation Matrices• Dot product and how it’s useful• Cross product and how it’s useful• Extras

Talk Outline

Page 3: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

• Matrix multiplication• How to apply dot/cross product• Vector normalization• Basic understanding of sin/cos• Basic idea of what a plane is

Prerequisites

Page 4: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

• Formal definition– Input or output from a function of vector algebra

• Informal definition– Scalar components representing a direction and

magnitude (length)– Vectors have no “location”

Vectors

Page 5: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Points and Vectors

• A point P and Q is related to the vector V by:– P – Q = V

• This implies that points can be translated by adding vectors

Page 6: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Points and Vectors (2)

• Lets define an implied point O– O represents the origin

• Any point P can be expressed with a vector V:– P = O + V

• Vectors point to points, when relative to the origin!

Page 7: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

• Standard Euclidean Basis (called E3)– Geometrically the x, y and z axes

Euclidean Basis

𝐸3=[1 0 00 1 00 0 1]

Page 8: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

• Given 3 scalars i, j and k, any point in E3 can be represented

Euclidean Basis (2)

Page 9: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Linear Combination• Suppose we have a vector V– Consists of 3 scalar values– Below V written in “shorthand” notation

Page 10: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Linear Combination (2)• V can be written as a linear combination of E3

– This is the “longhand” notation of a vector

Page 11: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Basis Matrices• E3 is a basis matrix– Many matrices can represent a basis

• Any vector can be represented in any basis

http://en.wikipedia.org/wiki/Change_of_basis

Note: Different i, j and kvalues are used on leftand right

Page 12: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Rotation Matrices• A rotation matrix can rotate vectors– Constructed from 3 orthogonal unit vectors– Can be called an “orthonormal basis”

• E3 is a rotation matrix!

Page 13: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Rotation Matrices (2)• Rotation matrices consist of an x, y and z axis– Each axis is a vector

[𝑎 𝑑 𝑔𝑏 𝑒 h𝑐 𝑓 𝑖 ]X y z

Page 14: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Rotation Matrices (3)• Multiplying a rotation and a vector rotates the

vector• This is a linear combination

Page 15: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Dot Product• Comes from law of cosines• Full derivation here• For two vectors u and v:

Page 16: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Shortest Angle Between 2 Vectors• Assume u and v are of unit length

• Result in range of 0, 1• No trig functions required

Page 17: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

How Far in a Given Direction?• Given point P and vector V– How far along the V direction is P?

P

V

x

y

Page 18: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

How Far in a Given Direction? (2)• Normalize V• Dot V and P (treat P as vector)

• Explanation:– Cosine scaled by the length of P is equivalent

to the distance P travels in the direction V

𝑑𝑜𝑡 (𝑉 ,𝑃 )=cos𝛾∨𝑃∨¿

Page 19: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Planes• Here’s the 3D plane equation

Page 20: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Planes• Here’s the 3D plane equation

• WAIT A SECOND

Page 21: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Planes• Here’s the 3D plane equation

• WAIT A SECOND• THAT’S THE DOT PRODUCT

Page 22: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Planes (2)

• a, b and c form a vector, called the normal• d is magnitude of the vector– Represents distance of plane from origin

Page 23: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Planes (3)

• d is interesting– d is scaled by length n

Page 24: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Planes and the Dot Product• Dot P with the plane normal:

• is distance along normal P travels• is scaled by length of normal

𝑃 : {𝑥 𝑦 𝑧 }∗[𝑎𝑏𝑐 ]=𝛿

Page 25: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Planes and the Dot Product (2)• 2D visualization, assume normal () is unit

x

y

P

plane

�̂�𝑑𝑜𝑡 (𝑃 , �̂�)=𝛿=¿∨¿

Page 26: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Signed Distance P to Plane• Take subtract d

• Explanation:– is distance P travels along normal– Subtract d (distance of plane from origin)– This gives distance of P to the plane

Page 27: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Project P onto Plane• 2D visualization, assume normal () is unit

P−(�̂�∗(𝑑𝑜𝑡 (𝑃 , �̂�)−𝑑 ))

x

y

P

plane

�̂� =

Page 28: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Rotation Matrices and Dot Product• Given matrices A and B• A * B is to dot the rows of A with columns of B• Lets assume A and B are rotation matrices

http://en.wikipedia.org/wiki/Matrix_multiplication

Page 29: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Rotation Matrices and Dot Product (2)

• That’s a lot of dot products!• Try: A * B * vector V– V is rotated by A, and then rotated by B

• What does A * B mean?– Each element is cos between a column of A and

row of B

Page 30: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Rotation Matrices and Dot Product (3)

• A = , B = • Lets start multiplying these guys

Page 31: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Rotation Matrices and Dot Product (4)

• We start by performing: • is the x axis of A

• is x components of the x, y and z axis of B

Page 32: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Rotation Matrices and Dot Product (5)

• What does do?– Computes the contribution of B’s x components

along the x axis of A!

• Every single element of A * B can be thought of in this way

Page 33: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Point in Convex Hull• Test if point is inside of a hull– Compute plane equation of hull faces– Compute distance from plane with plane equation– If all distances are negative, point in hull– If any distance is positive, point outside hull

• Works in any dimension• Can by used for basic frustum culling

Page 34: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Point in OBB• An OBB is a convex hull!– Hold your horses here…

• Lets rotate point P into the frame of the OBB– OBB is defined with a rotation matrix, so invert it

and multiply P by it– P is now in the basis of the OBB

• The problem is now point in AABB

Page 35: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Point in Cylinder• Rotate cylinder axis to the z axis• Ignoring the z axis, cylinder is a circle on the xy plane• Test point in circle in 2D

– If miss, exit no intersection• Get points A and B. A is at top of cylinder, B at bottom• See if point’s z component is less than A’s and greater than B’s

– Return intersection• No intersection

Page 36: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Bumper Car Damage?• Two bumper cars hit each other• One car takes damage• How much damage is dealt, and to whom?

Page 37: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Bumper Car Damage Answer• Take vector from one car to another, T• Damage dealt:– 1.0 - Abs( Dot( velocity, T ) * collisionDamage )

Page 38: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Distance Point P to Line

http://www.randygaul.net/2014/07/23/distance-point-to-line-segment/

Page 39: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Closest Point to Segment from P• Take and compute a plane C• C’s normal is / ||, and offset d is:– d = dot( / ||, A )

• Compute distance P from C– If C is negative, closest point is A

• Repeat process for B

Page 40: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Visualization in 2D

C �⃑�𝐵

/ ||

P

Page 41: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Cross Product• Operation between vectors

• Produces a vector orthogonal to both input vectors

|𝑣×𝑤| =|𝑣|∨𝑤∨𝑠𝑖𝑛 (𝛾)

Page 42: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Cross Product Handedness

http://en.wikipedia.org/wiki/Cross_product

Page 43: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Cross Product Details

http://upload.wikimedia.org/wikipedia/commons/thumb/6/6e/Cross_product.gif/220px-Cross_product.gif

Page 44: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Plane Equation from 3 Points• Given three points A, B and C• Calculate normal with: Cross( C – A, B – A )• Normalize normal• Compute offset d: dot( normal, A (or B or C) )

Page 45: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Intersection Segment and Plane• Compute distance A to Plane: da• Compute distance B to Plane: db• If da * db < 0– Intersection = A + (da / (da – db)) * (B – A)

• Else– No intersection

Page 46: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Segment Intersects Triangle UVW• If intersects plane triangle’s plane

– Compute intersection point P with plane– For each edge along UVW, cross Va and P– Dot result with plane normal– If dot result < 0

• No intersection

– Hit back side of UVW if dot( , normal ) > 0• Else

– No intersection

Page 47: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Segment Intersects Triangle UVW (2)

�⃑�𝐵

P

normal

Page 48: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Affine Transformations• Given matrix A, point x and vector b• An affine transformation is of the form:– Ax + b

Page 49: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Affine Transformations• Given 3x3 matrix A, point x and vector b• An affine transformation is of the form:– Ax + b

• With a 4x4 matrix we can represent Ax + b in block formation:

[ 𝐴 𝑏0𝑇 1 ][𝑥1 ]=[𝐴 00 𝐴10

𝐴 01 𝐴11𝐴 02 𝐴120 0

𝐴20 𝑏0𝐴21 𝑏1𝐴22 𝑏20 1

] [𝑥 0𝑥 1𝑥 21

]

Page 50: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Translation• Construct an affine transformation such that when

multiplied with a point, translates the point by the vector b

• I is the identity matrix, and means no rotation (or scaling) occurs

[ 𝐼 𝑏0𝑇 1 ][𝑥1 ]

Page 51: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Translation (2)

• The 1 is important, it means it is a point• If we had a zero here b wouldn’t affect x

[ 𝐼 𝑏0𝑇 1 ][𝑥1 ]

Page 52: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Translation (3)• Proof that translation doesn’t affect vectors:– Translate from P to Q by T• P – Q = T• = (P + T) – (Q + T)• = (P – Q) + (T – T)• = P – Q

Page 53: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Rotation• Orthonormal basis into the top left of an affine

transformation, without any translation vector:

[ 𝑅 00𝑇 1 ][𝑥1 ]

Page 54: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Scaling• Given scaling vector S• Take the matrix A• Scale A’s x axis’ x component by S0

• Scale A’s y axis’ y component by S1

• Scale A’s z axis’ z component by S2

Page 55: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Scaling (2)

[𝐴 00∗𝑆 0 𝐴10𝐴 01 𝐴 11∗𝑆1𝐴 02 𝐴120 0

𝐴 20 𝑏0𝐴21 𝑏1

𝐴22∗𝑆2 𝑏20 1

] [𝑥 0𝑥1𝑥21

]

Page 56: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Camera - LookAt• Given Point P• Calculate vector V from Camera to LookAt( P )• Cross V and up vector , this is Right vector• Construct rotation matrix with V, Up and Right

– These vectors (once normalized) form an orthonormal basis– This is the A matrix

• Negated camera position is the b vector:[ 𝐴 𝑏0𝑇 1 ][𝑥1 ]

Page 57: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Camera – LookAt (2)• Won’t work when player tries to look straight up or

down– Parallel vectors crossed result in the zero vector

• Possible solutions:– Snap player’s view away from up/down– Use an if statement and cross with a different up vector– More solutions exist!

Page 58: Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.

Barycentric Coordinates• Slightly out of scope of this presentation• See Erin Catto’s GDC 2010 lecture• Idea:– Like a linear combination, try affine combinations of points

• Useful for:– Voronoi region identification, collision detection, certain

graphics or shader effects