/* * Find the vertex of p1 that is the furthest from its center in * direction dir, and the vertex of p2 that is the furthest from * its center in direction -1 * dir. Return these two points and * the vector between them, all stored in a MinkDiff object. */ MinkDiff support(Polygon p1, Polygon p2, Vector2 dir) { GameObject a = new GameObject("MinkDiff"); MinkDiff minkdiff = a.AddComponent <MinkDiff>() as MinkDiff; Destroy(a); minkdiff.s1Point = p1.getFurthest(dir); minkdiff.s2Point = p2.getFurthest(-1 * dir); minkdiff.diff = minkdiff.s1Point - minkdiff.s2Point; return(minkdiff); }
/* * Method that executes the GJK algorithm for finding the shortest distance * between two polygons. * Returns an array with two points: the point on each polygon that is closest * to the other. */ public Vector2[] BARG(Polygon p1, Polygon p2) { dir = p1.vertices [0] - p2.vertices [0]; List <MinkDiff> simplex = new List <MinkDiff> (2); simplex.Add(support(p1, p2, dir)); simplex.Add(support(p1, p2, dir)); dir = closestPointToOrigin(simplex [0].diff, simplex [1].diff); for (int i = 0; i < 30; i++) { dir = -1 * dir; if (dir.magnitude == 0) { return(new Vector2[2] { Vector2.zero, Vector2.zero }); } MinkDiff c = support(p1, p2, dir); if (Vector2.Dot(c.diff, dir) - Vector2.Dot(simplex [0].diff, dir) < TOL) { Vector2[] closestPoints = findClosestPoints(simplex); return(closestPoints); } Vector2 v1 = closestPointToOrigin(simplex [0].diff, c.diff); Vector2 v2 = closestPointToOrigin(simplex [1].diff, c.diff); if (v1.magnitude < v2.magnitude) { simplex [1] = c; dir = v1; } else { simplex [0] = c; dir = v2; } } Vector2[] closestPointss = findClosestPoints(simplex); return(closestPointss); }
/* * Method that performs the GJK algorithm between two convex polygons * return true if they are interpenetrating false otherwise. * A lot of the stuff in here is drawing code for the tutorial since Unity * wont allow us to separate this */ public bool GARB(Polygon p1, Polygon p2) { dir = p1.vertices [0] - p2.vertices [0]; List <MinkDiff> simplex = new List <MinkDiff> (); MinkDiff a = support(p1, p2, dir); simplex.Add(a); if (tutorialMode && currIterations < maxIterations) { foreach (GameObject gameobj in GameObject.FindObjectsOfType <GameObject>()) { if (gameobj.name == "TutorialDot") { Destroy(gameobj); } } a.draw(0); } dir = -1 * dir; loopcounter = 0; while (true && loopcounter < 1000 && true && !false || false) { if (tutorialMode) { currIterations++; if (currIterations > maxIterations) { return(false); } } a = support(p1, p2, dir); simplex.Add(a); if (tutorialMode) { a.draw(loopcounter + 1); } if (tutorialMode) { List <Vector3> minkidiffipoints = jarvis(simplex); minkidiffipoints.Add(minkidiffipoints [0]); line.numPositions = minkidiffipoints.Count; line.SetPositions(minkidiffipoints.ToArray()); } if (!canContainOrigin(a.diff, dir)) { return(false); } else if (containsOrigin(simplex)) { return(true); } loopcounter++; } p1.GetComponent <Renderer> ().material.color = Color.magenta; p2.GetComponent <Renderer> ().material.color = Color.magenta; return(false); }