Computing Distance Erin Catto Blizzard Entertainment Convex - - PowerPoint PPT Presentation

computing distance
SMART_READER_LITE
LIVE PREVIEW

Computing Distance Erin Catto Blizzard Entertainment Convex - - PowerPoint PPT Presentation

Computing Distance Erin Catto Blizzard Entertainment Convex polygons Closest points Overlap Goal Compute the distance between convex polygons Keep in mind 2D Code not optimized Approach simple complex Geometry If all else


slide-1
SLIDE 1

Computing Distance

Erin Catto Blizzard Entertainment

slide-2
SLIDE 2

Convex polygons

slide-3
SLIDE 3

Closest points

slide-4
SLIDE 4

Overlap

slide-5
SLIDE 5

Goal

 Compute the distance between convex

polygons

slide-6
SLIDE 6

Keep in mind

 2D  Code not optimized

slide-7
SLIDE 7

Approach

simple complex

slide-8
SLIDE 8

Geometry

slide-9
SLIDE 9

If all else fails …

slide-10
SLIDE 10

DEMO!

slide-11
SLIDE 11

Outline

  • 1. Point to line segment
  • 2. Point to triangle
  • 3. Point to convex polygon
  • 4. Convex polygon to convex polygon
slide-12
SLIDE 12

Concepts

  • 1. Voronoi regions
  • 2. Barycentric coordinates
  • 3. GJK distance algorithm
  • 4. Minkowski difference
slide-13
SLIDE 13

Point to Line Segment

Section 1

slide-14
SLIDE 14

A line segment

A B

slide-15
SLIDE 15

Query point

Q A B

slide-16
SLIDE 16

Closest point

Q P A B

slide-17
SLIDE 17

Projection: region A

Q A B

slide-18
SLIDE 18

Projection: region AB

Q A B

slide-19
SLIDE 19

Projection: region B

Q A B

slide-20
SLIDE 20

Voronoi regions

A B region A region AB region B

slide-21
SLIDE 21

Barycentric coordinates

G(u,v)=uA+vB u+v=1

A G B

slide-22
SLIDE 22

Fractional lengths

G(u,v)=uA+vB u+v=1

A G B

u=0.5 v=0.5

slide-23
SLIDE 23

Fractional lengths

G(u,v)=uA+vB u+v=1

A G B

u=0.75 v=0.25

slide-24
SLIDE 24

Fractional lengths

G(u,v)=uA+vB u+v=1

A G B

u=1.25 v=-0.25

slide-25
SLIDE 25

Unit vector

A B B-A = B-A n n

slide-26
SLIDE 26

(u,v) from G

A G B

u v

 

B-G n u= B-A 

 

G-A n v= B-A 

slide-27
SLIDE 27

(u,v) from Q

 

B-Q n u= B-A  A G B

u v

 

Q-A n v= B-A  Q

slide-28
SLIDE 28

Voronoi region from (u,v)

A G B u > 0 and v > 0 region AB

u > 0 v > 0

slide-29
SLIDE 29

Voronoi region from (u,v)

A G B v <= 0 region A

u > 0 v < 0

slide-30
SLIDE 30

Voronoi region from (u,v)

A G B u <= 0 region B

u < 0 v > 0

slide-31
SLIDE 31

Closet point algorithm

input: A, B, Q compute u and v if (u <= 0) P = B else if (v <= 0) P = A else P = u*A + v*B

slide-32
SLIDE 32

Aside: center of mass

COM mass A = mass B B A

slide-33
SLIDE 33

Aside: center of mass

mass A > mass B B A COM

slide-34
SLIDE 34

Aside: center of mass

mass A < mass B B A COM

slide-35
SLIDE 35

Aside: center of mass

massA massB COM A B massA massB massA massB     massA>0 massB>0

slide-36
SLIDE 36

Point to Triangle

Section 2

slide-37
SLIDE 37

Triangle

A B C

slide-38
SLIDE 38

Closest feature: vertex

A P=B C Q

slide-39
SLIDE 39

Closest feature: edge

A C Q B P

slide-40
SLIDE 40

Closest feature: interior

A C B P=Q

slide-41
SLIDE 41

Voronoi regions

A B C

Region AB Region CA Region BC Region ABC Region A Region B Region C

slide-42
SLIDE 42

3 line segments

A B C

 

AB AB

u ,v

 

CA CA

u ,v

 

BC BC

u ,v

Q

slide-43
SLIDE 43

Vertex regions

A B C

AB BC

u v  

CA AB

u v  

BC CA

u v   Using line segment uv’s

slide-44
SLIDE 44

Edge regions

A B C

AB AB

u v ?   Line segment uv’s are not sufficient

slide-45
SLIDE 45

Interior region

A B C ? Line segment uv’s don’t help at all

slide-46
SLIDE 46

Triangle barycentric coordinates

Q uA vB wC    1 w v u   

A C B Q

slide-47
SLIDE 47

Linear algebra solution

x x x x y y y y

A B C u Q A B C v = Q 1 1 1 w 1                              

slide-48
SLIDE 48

Fractional areas

A B C Q

slide-49
SLIDE 49

The barycenctric coordinates are the fractional areas

B A C Q

w u Area(BCQ) 

C v u

v Area(CAQ)  w Area(ABQ) 

slide-50
SLIDE 50

Barycentric coordinates

C A B

w  v 

u 1 

Q

slide-51
SLIDE 51

Barycentric coordinates

area(QBC) u area(ABC)  area(QCA) v area(ABC)  area(QAB) w area(ABC) 

slide-52
SLIDE 52

Barycentric coordinates are fractional

line segment : fractional length triangles : fractional area tetrahedrons : fractional volume

slide-53
SLIDE 53

Computing Area

A C B

 

1 signed area= cross B-A,C-A 2

slide-54
SLIDE 54

Q outside the triangle

C Q C A B

slide-55
SLIDE 55

P outside the triangle

C

v  w  v+w>1

C A B Q

slide-56
SLIDE 56

P outside the triangle

C u  C A B Q

slide-57
SLIDE 57

Voronoi versus Barycentric

 Voronoi regions != barycentric coordinate

regions

 The barycentric regions are still useful

slide-58
SLIDE 58

Barycentric regions of a triangle

A B C

slide-59
SLIDE 59

Interior

A B C

w 0, v 0, u   

Q

slide-60
SLIDE 60

Negative u

A B C

u 

Q

slide-61
SLIDE 61

Negative v

A B C

v 

Q

slide-62
SLIDE 62

Negative w

A B C

w 

Q

slide-63
SLIDE 63

The uv regions are not exclusive

A B C Q P

slide-64
SLIDE 64

Finding the Voronoi region

 Use the barycentric coordinates to identify

the Voronoi region

 Coordinates for the 3 line segments and the

triangle

 Regions must be considered in the correct

  • rder
slide-65
SLIDE 65

First: vertex regions

A B C

AB BC

u v  

AB CA

v u  

BC CA

u v  

slide-66
SLIDE 66

Second: edge regions

A B C

AB AB

u v ?  

slide-67
SLIDE 67

Second: edge regions solved

A B C

AB AB ABC

u v w   

slide-68
SLIDE 68

Third: interior region

A B C

ABC ABC ABC

u > 0 v > 0 w > 0

slide-69
SLIDE 69

Closest point

 Find the Voronoi region for point Q  Use the barycentric coordinates to compute

the closest point Q

slide-70
SLIDE 70

Example 1

A B C Q

slide-71
SLIDE 71

Example 1

A B C

uAB <= 0

Q

slide-72
SLIDE 72

Example 1

A B C

uAB <= 0 and vBC <= 0

Q

slide-73
SLIDE 73

Example 1

A P=B C

Conclusion: P = B

Q

slide-74
SLIDE 74

Example 2

A B C Q

slide-75
SLIDE 75

Example 2

A B C

Q is not in any vertex region

Q

slide-76
SLIDE 76

Example 2

A B C

uAB > 0

Q

slide-77
SLIDE 77

Example 2

A B C

uAB > 0 and vAB > 0

Q

slide-78
SLIDE 78

Example 2

A B C

uAB > 0 and vAB > 0 and wABC <= 0

Q

slide-79
SLIDE 79

Example 2

A B C

Conclusion: P = uAB*A + vAB*B

P Q

slide-80
SLIDE 80

Implementation

input: A, B, C, Q compute uAB, vAB, uBC, vBC, uCA, vCA compute uABC, vABC, wABC // Test vertex regions … // Test edge regions … // Else interior region …

slide-81
SLIDE 81

Testing the vertex regions

// Region A if (vAB <= 0 && uCA <= 0) P = A return // Similar tests for Region B and C

slide-82
SLIDE 82

Testing the edge regions

// Region AB if (uAB > 0 && vAB > 0 && wABC <= 0) P = uAB * A + vAB * B return // Similar for Regions BC and CA

slide-83
SLIDE 83

Testing the interior region

// Region ABC assert(uABC > 0 && vABC > 0 && wABC > 0) P = Q return

slide-84
SLIDE 84

Point to Convex Polygon

Section 3

slide-85
SLIDE 85

Convex polygon

A B C D E

slide-86
SLIDE 86

Polygon structure

struct Polygon { Vec2* points; int count; };

slide-87
SLIDE 87

Convex polygon: closest point

Q A B C D E

Query point Q

slide-88
SLIDE 88

Convex polygon: closest point

P

Closest point Q

A B C D E Q

slide-89
SLIDE 89

How do we compute P?

slide-90
SLIDE 90

What do we know?

Closest point to point Closest point to line segment Closest point to triangle

slide-91
SLIDE 91

Simplex

0-simplex 1-simplex 2-simplex

slide-92
SLIDE 92

Idea: inscribe a simplex

A B C D E Q

slide-93
SLIDE 93

Idea: closest point on simplex

A B P = C D E Q

slide-94
SLIDE 94

Idea: evolve the simplex

A B C D E Q

slide-95
SLIDE 95

Simplex vertex

struct SimplexVertex { Vec2 point; int index; float u; };

slide-96
SLIDE 96

Simplex

struct Simplex { SimplexVertex vertexA; SimplexVertex vertexB; SimplexVertex vertexC; int count; };

slide-97
SLIDE 97

We are onto a winner!

slide-98
SLIDE 98

The GJK distance algorithm

 Computes the closest point on a convex

polygon

 Invented by Gilbert, Johnson, and Keerthi

slide-99
SLIDE 99

The GJK distance algorithm

 Inscribed simplexes  Simplex evolution

slide-100
SLIDE 100

Starting simplex

Start with arbitrary

  • vertex. Pick E.

This is our starting simplex.

A B C D E Q

slide-101
SLIDE 101

Closest point on simplex

P is the closest point.

A B C D Q P=E

slide-102
SLIDE 102

Search vector

Draw a vector from P to Q. Call this vector d. d

A B C D P=E Q

slide-103
SLIDE 103

Find the support point

Find the vertex on polygon furthest in direction d. This is the support point. d

A B C D Q P=E

slide-104
SLIDE 104

Support point code

int Support(const Polygon& poly, const Vec2& d) { int index = 0; float maxValue = Dot(d, poly.points[index]); for (int i = 1; i < poly.count; ++i) { float value = Dot(d, poly.points[i]); if (value > maxValue) { index = i; maxValue = value; } } return index; }

slide-105
SLIDE 105

Support point found

C is the support point. d

A B C D E Q

slide-106
SLIDE 106

Evolve the simplex

Create a line segment CE. We now have a 1-simplex.

A B C D E Q

slide-107
SLIDE 107

Repeat the process

Find closest point P on CE.

A B C D E P Q

slide-108
SLIDE 108

New search direction

Build d as a line pointing from P to Q.

A B C D E

d

P Q

slide-109
SLIDE 109

New support point

D is the support point.

A B C D E

d

Q

slide-110
SLIDE 110

Evolve the simplex

Create triangle CDE. This is a 2-simplex.

A B C D E Q

slide-111
SLIDE 111

Closest point

Compute closest point on CDE to Q.

A B C D E P Q

slide-112
SLIDE 112

E is worthless

Closest point is on CD. E does not contribute.

A B C D E P Q

slide-113
SLIDE 113

Reduced simplex

We dropped E, so we now have a 1-simplex.

A B C D E P Q

slide-114
SLIDE 114

Termination

Compute support point in direction d. We find either C or

  • D. Since this is a

repeat, we are done.

Q A B C D E P

d

slide-115
SLIDE 115

GJK algorithm

Input: polygon and point Q pick arbitrary initial simplex S loop compute closest point P on S cull non-contributing vertices from S build vector d pointing from P to Q compute support point in direction d add support point to S end

slide-116
SLIDE 116

DEMO!!!

slide-117
SLIDE 117

Numerical Issues

 Search direction  Termination  Poorly formed polygons

slide-118
SLIDE 118

A bad direction

Q A B C D E P

d can be built from PQ. Due to round-off: dot(Q-P, C-E) != 0 d

slide-119
SLIDE 119

A real example in single precision

Line Segment A = [0.021119118, 79.584320] B = [0.020964622, -31.515678] Query Point Q = [0.0 0.0] Barycentric Coordinates (u, v) = (0.28366947, 0.71633047) Search Direction d = Q – P = [-0.021008447, 0.0] dot(d, B – A) = 3.2457051e-006

slide-120
SLIDE 120

Small errors matter

numerical direction exact direction correct support wrong support

slide-121
SLIDE 121

An accurate search direction

A B C D E

d Directly compute a vector perpendicular to CE. d = cross(C-E,z) Where z is normal to the plane.

Q

slide-122
SLIDE 122

Fixing the sign

A B C D E

d Flip the sign of d so that: dot(d, Q – C) > 0 Perk: no divides

Q

slide-123
SLIDE 123

Termination conditions

}

slide-124
SLIDE 124

Case 1: repeated support point

A B C D E

d

P Q

slide-125
SLIDE 125

Case 2: containment

We find a 2-simplex and all vertices contribute.

A B C D E P=Q

slide-126
SLIDE 126

Case 3a: vertex overlap

We will compute d=Q-P as zero. So we terminate if d=0.

A B C E P=Q=D

slide-127
SLIDE 127

Case 3b: edge overlap

d will have an arbitrary sign.

A B E P=Q C D

d

slide-128
SLIDE 128

Case 3b: d points left

If we search left, we get a duplicate support point. In this case we terminate.

A B E P=Q C D

d

slide-129
SLIDE 129

Case 3b: d points right

If we search right, we get a new support point (A).

A B E C D

d

Q=P

slide-130
SLIDE 130

Case 3b: d points right

But then we get back the same P, and then the same d. Soon, we detect a repeated support point or detect containment.

A B E C D

d

P=Q

slide-131
SLIDE 131

Case 4: interior edge

d will have an arbitrary sign.

A B E P=Q C D

d

slide-132
SLIDE 132

Case 4: interior edge

Similar to Case 3b

A B E P=Q C D

d

slide-133
SLIDE 133

Termination in 3D

 May require new/different conditions  Check for distance progression

slide-134
SLIDE 134

Non-convex polygon

Vertex B is non- convex

A E C D B

slide-135
SLIDE 135

Non-convex polygon

B is never a support point

A B E C D

slide-136
SLIDE 136

Collinear vertices

B, C, and D are collinear

A E B D C

slide-137
SLIDE 137

Collinear vertices

2-simplex BCD

A E B D C Q

slide-138
SLIDE 138

Collinear vertices

area(BCD) = 0

A E B D C Q

slide-139
SLIDE 139

Convex Polygon to Convex Polygon

Section 4

slide-140
SLIDE 140

X Y

Closest point between convex polygons

slide-141
SLIDE 141

What do we know?

GJK

slide-142
SLIDE 142

What do we need to know?

???

slide-143
SLIDE 143

Idea

 Convert polygon to polygon into point to

polygon

 Use GJK to solve point to polygon

slide-144
SLIDE 144

Minkowski difference

Y X

Y-X

Z

slide-145
SLIDE 145

Definition

 

j i i j

Z = y - x : x X, y Y  

slide-146
SLIDE 146

Building the Minkowski difference

Input: polygon X and Y array points for all xi in X for all yj in Y points.push_back(yj – xi) end end polygon Z = ConvexHull(points)

slide-147
SLIDE 147

Example point cloud

Y X

Y-X Compute Yi – Xj for i = 1 to 4 and j = 1 to 3

slide-148
SLIDE 148

Building the convex hull

Compute the convex hull by shrink wrapping the points.

slide-149
SLIDE 149

Building the convex hull

Compute the convex hull by shrink wrapping the points.

slide-150
SLIDE 150

Building the convex hull

Compute the convex hull by shrink wrapping the points.

slide-151
SLIDE 151

Building the convex hull

Compute the convex hull by shrink wrapping the points.

slide-152
SLIDE 152

Building the convex hull

Compute the convex hull by shrink wrapping the points.

slide-153
SLIDE 153

Building the convex hull

Compute the convex hull by shrink wrapping the points.

slide-154
SLIDE 154

Building the convex hull

Compute the convex hull by shrink wrapping the points.

slide-155
SLIDE 155

Building the convex hull

Compute the convex hull by shrink wrapping the points.

slide-156
SLIDE 156

Building the convex hull

Compute the convex hull by shrink wrapping the points.

slide-157
SLIDE 157

Z

The final polygon

slide-158
SLIDE 158

Z

Property 1: distances are equal

Y X

distance(X,Y) == distance(O, Y-X) O

slide-159
SLIDE 159

Property 2: support points

support(Z, d) = support(Y, d) – support(X, -d)

Z Y X

O d d

  • d
slide-160
SLIDE 160

Convex Hull?

slide-161
SLIDE 161

Modifying GJK

 Change the support function  Simplex vertices hold two indices

slide-162
SLIDE 162

Closest point on polygons

 Use the barycentric coordinates to compute

the closest points on X and Y

 See the demo code for details

slide-163
SLIDE 163

DEMO!!!

Download: box2d.org

slide-164
SLIDE 164

Further reading

 Collision Detection in Interactive 3D

Environments, Gino van den Bergen, 2004

 Real-Time Collision Detection, Christer

Ericson, 2005

 Implementing GJK:

http://mollyrocket.com/849, Casey Muratori.

slide-165
SLIDE 165

Box2D

 An open source 2D physics engine  http://www.box2d.org  Written in C++