From adfe9a9a5dbd2466ff927113e52819543c5f082b Mon Sep 17 00:00:00 2001 From: Matthias Richter Date: Tue, 13 Aug 2013 15:34:58 +0200 Subject: [PATCH] Issue #25 (2): Collision detection sometimes hangs. Sometimes the expanding polytype algorithm takes a long time to terminate, e.g. in situations like this: .-. : +-:-+ '-' | | | +---+ The issue here is that the EPA adds vertices very close to each other, which makes the separation distance change very slowly. If this is the case, consider the current separation distance as approximately correct and continue. --- gjk.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/gjk.lua b/gjk.lua index ab05f0a..5a2d8f0 100644 --- a/gjk.lua +++ b/gjk.lua @@ -65,14 +65,17 @@ local function EPA(shape_a, shape_b, simplex) end -- the expanding polytype algorithm + local last_diff_dist = math.huge while true do local e = closest_edge(simplex) local px,py = support(shape_a, shape_b, e.nx, e.ny) local d = vector.dot(px,py, e.nx, e.ny) - if d - e.dist < 1e-6 then + local diff_dist = d - e.dist + if diff_dist < 1e-6 or last_diff_dist - diff_dist < 1e-10 then return -d*e.nx, -d*e.ny end + last_diff_dist = diff_dist -- simplex = {..., simplex[e.i-1], px, py, simplex[e.i] table.insert(simplex, e.i, py)