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 fails …
DEMO!
Outline 1. Point to line segment 2. Point to triangle 3. Point to convex polygon 4. Convex polygon to convex polygon
Concepts 1. Voronoi regions 2. Barycentric coordinates 3. GJK distance algorithm 4. Minkowski difference
Section 1 Point to Line Segment
A line segment A B
Query point Q A B
Closest point Q P A B
Projection: region A Q A B
Projection: region AB Q A B
Projection: region B Q A B
Voronoi regions region A region AB region B A B
Barycentric coordinates A G B G(u,v)=uA+vB u+v=1
Fractional lengths v=0.5 u=0.5 A G B G(u,v)=uA+vB u+v=1
Fractional lengths v=0.25 u=0.75 A G B G(u,v)=uA+vB u+v=1
Fractional lengths u=1.25 v=-0.25 G A B G(u,v)=uA+vB u+v=1
Unit vector A B n B-A n = B-A
(u,v) from G A G B v u G-A n B-G n v= u= B-A B-A
(u,v) from Q Q A G B v u Q-A n B-Q n v= u= B-A B-A
Voronoi region from (u,v) v > 0 u > 0 A G B u > 0 and v > 0 region AB
Voronoi region from (u,v) u > 0 v < 0 G A B v <= 0 region A
Voronoi region from (u,v) u < 0 v > 0 A B G u <= 0 region B
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
Aside: center of mass A COM B mass A = mass B
Aside: center of mass A COM B mass A > mass B
Aside: center of mass A COM B mass A < mass B
Aside: center of mass massA massB COM A B massA massB massA massB massA>0 massB>0
Section 2 Point to Triangle
Triangle B A C
Closest feature: vertex Q P=B A C
Closest feature: edge B A P Q C
Closest feature: interior B A P=Q C
Voronoi regions B Region B Region AB A Region A Region ABC Region BC Region CA C Region C
3 line segments Q u ,v AB AB B A C u ,v BC BC u ,v CA CA
Vertex regions u 0 AB B v 0 BC A u 0 CA v 0 AB C u 0 Using line segment uv’s BC v 0 CA
Edge regions u 0 AB v 0 B AB ? A C Line segment uv’s are not sufficient
Interior region B A ? C Line segment uv’s don’t help at all
Triangle barycentric coordinates B A Q Q uA vB wC C u v w 1
Linear algebra solution A B C u Q x x x x A B C v = Q y y y y 1 1 1 w 1
Fractional areas B A Q C
The barycenctric coordinates are the fractional areas B w Area(ABQ) A w Q u v u Area(BCQ) v Area(CAQ) C C
Barycentric coordinates B w 0 A Q u 1 v 0 C
Barycentric coordinates area(QBC) u area(ABC) area(QCA) v area(ABC) area(QAB) w area(ABC)
Barycentric coordinates are fractional line segment : fractional length triangles : fractional area tetrahedrons : fractional volume
Computing Area C B A 1 signed area= cross B-A,C-A 2
Q outside the triangle B A Q C C
P outside the triangle B A w 0 Q v 0 v+w>1 C C
P outside the triangle B A Q u 0 C C
Voronoi versus Barycentric Voronoi regions != barycentric coordinate regions The barycentric regions are still useful
Barycentric regions of a triangle B A C
Interior B A Q u 0, v 0, w 0 C
Negative u B A Q u 0 C
Negative v B A Q v 0 C
Negative w Q B w 0 A C
The uv regions are not exclusive B A P C Q
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 order
First: vertex regions u 0 AB B v 0 BC A v 0 AB u 0 CA C u 0 BC v 0 CA
Second: edge regions u 0 AB v 0 B AB ? A C
Second: edge regions solved u 0 AB v 0 B AB w 0 ABC A C
Third: interior region B A u > 0 ABC v > 0 ABC w > 0 ABC C
Closest point Find the Voronoi region for point Q Use the barycentric coordinates to compute the closest point Q
Example 1 Q B A C
Example 1 Q B A C uAB <= 0
Example 1 Q B A C uAB <= 0 and vBC <= 0
Example 1 Q P=B A Conclusion: C P = B
Example 2 Q B A C
Example 2 Q B A C Q is not in any vertex region
Example 2 Q B A C uAB > 0
Example 2 Q B A C uAB > 0 and vAB > 0
Example 2 Q B A C uAB > 0 and vAB > 0 and wABC <= 0
Example 2 Q B A P C Conclusion: P = uAB*A + vAB*B
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 …
Testing the vertex regions // Region A if (vAB <= 0 && uCA <= 0) P = A return // Similar tests for Region B and C
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
Testing the interior region // Region ABC assert(uABC > 0 && vABC > 0 && wABC > 0) P = Q return
Section 3 Point to Convex Polygon
Convex polygon B C A D E
Polygon structure struct Polygon { Vec2* points; int count; };
Convex polygon: closest point B Query point Q C A Q D E
Convex polygon: closest point B Closest point Q C A Q P D E
How do we compute P?
What do we know? Closest point to point Closest point to line segment Closest point to triangle
Simplex 0-simplex 1-simplex 2-simplex
Idea: inscribe a simplex B C A Q D E
Idea: closest point on simplex B P = C A Q D E
Idea: evolve the simplex B C A Q D E
Simplex vertex struct SimplexVertex { Vec2 point; int index; float u; };
Simplex struct Simplex { SimplexVertex vertexA; SimplexVertex vertexB; SimplexVertex vertexC; int count; };
We are onto a winner!
The GJK distance algorithm Computes the closest point on a convex polygon Invented by Gilbert, Johnson, and Keerthi
The GJK distance algorithm Inscribed simplexes Simplex evolution
Starting simplex B Start with arbitrary vertex. Pick E. C This is our starting A simplex. Q D E
Recommend
More recommend