Reference: http://www.dyn4j.org/2010/05/epa-expanding-polytope-algorithm/
An online Demo: HTTP://SANDBOX.RUNJS.CN/SHOW/XSEOJPFA
GJK can determine whether two convex shapes overlap, EPA can find the smallest vector separating two graphs based on GJK's work.
If the origin is inside the Minkowski, then two graphs overlap. The vector of the point closest to the origin at the Minkowski boundary is the smallest separation vector.
The EPA expands the simplex at the end of the gjk, selecting vertices from the Minkowski's boundary with the same getsupportpoint function as GJK, until the expanded polygon contains the nearest edge from the origin of the Minkowski.
Getpenetration:function (tolerance) { var EPA = new Contact.epa ( this._simplexa, This._simplexb, This._simplexc, this , tolerance ); return this.penetration = Epa.solve ();}
Getclosestpointtoorigin:function (A, b) { var ab = B.sub (a). Norm (), ao = a.negate (); Return A.add (Ab.mul (Ab.dot (AO)); Projecting the origin onto AB.}
Getpenetration:function (tolerance) {var EPA = new Contact.epa (This._simplexa, This._simplexb, This._simplexc, this, tolerance); return this.penetration = Epa.solve ();} Getclosestpointtoorigin:function (A, b) {var ab = B.sub (a). Norm (), AO = A.negate (); Return A.add (Ab.mul (Ab.dot (AO));} Epa:pngx. Class (null, function (Simplexa, SIMPLEXB, simplexc, contact, tolerance) {this.vertices = [Simplexa, simplex B, Simplexc]; This.contact = contact; This.tolerance = Tolerance | | 0.05; }, {vertices:null, contact:null, Tolerance:null, Closestindex:null, Closestnorm Al:null, Closestdistance:null, Penetration:null, Findclosestedge:function () {//Find out what is closest to the origin in the current extended polygon One side var bestdistance = Infinity, Bestnormal, bestindex, vertices = this.vertices, len = vertices.length; for (var i = 0; i < len; ++i) {var v0 = vertices[i], a vertex of the polygon, and a vector that points from the origin to the outside of the polygon V1 = vertices[(i + 1)% len], v01 = V1.sub (v0), normal = Pngx. Vector2d.tripleproduct (V01, V0, v01). Norm (),//take a vector pointing to the outside of the polygon, perpendicular to the v0-v1 of the edge, distance = V0.dot (normal); The distance from the origin to the current edge if (distance < bestdistance) {bestdistance = distance; Bestnormal = normal; Bestindex = i; }} this.closestindex = Bestindex; This.closestnormal = Bestnormal; This.closestdistance = bestdistance; }, Insert:function (index, point) {var vertices = this.vertices, len = vertices.length; Vertices.splice ((index + 1)% len, 0, point); }, Solve:function () {var contact = this.contact, tolerance = this.tolerance; for (;;) {//Most cases can be completed two or three times to complete This.findclosestedge ();//Take the normal vector of the nearest edge of the extended polygon from the origin to the search direction, in this direction it is most likely to find the edge of the Minkowski on the closest to the origin. var normal = This.closestnormal, point = Contact.getsupportpoint (normal), dis tance = Point.dot (normal); The newly found vertex to the origin of the distance if (distance-this.closestdistance <= tolerance) {//New find point is very close to the last spot found, point is close enough to Minkowski difference The point closest to the origin on the boundary is return this.penetration = Normal.mul (distance); } this.insert (This.closestindex, point); } } })
EPA for embedding depth and embedding direction (fix)