Skip to content

Commit

Permalink
Fix commit adfe9a9 breaking EPA.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Richter committed Sep 3, 2013
1 parent adfe9a9 commit 97ae2d8
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions gjk.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ THE SOFTWARE.

local _PACKAGE = (...):match("^(.+)%.[^%.]+")
local vector = require(_PACKAGE .. '.vector-light')
local huge, abs = math.huge, math.abs

local function support(shape_a, shape_b, dx, dy)
local x,y = shape_a:support(dx,dy)
Expand All @@ -34,7 +35,7 @@ end

-- returns closest edge to the origin
local function closest_edge(simplex)
local e = {dist = math.huge}
local e = {dist = huge}

local i = #simplex-1
for k = 1,#simplex-1,2 do
Expand Down Expand Up @@ -65,14 +66,14 @@ local function EPA(shape_a, shape_b, simplex)
end

-- the expanding polytype algorithm
local last_diff_dist = math.huge
local last_diff_dist = 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)

local diff_dist = d - e.dist
if diff_dist < 1e-6 or last_diff_dist - diff_dist < 1e-10 then
local diff_dist = d - e.dist
if diff_dist < 1e-6 or abs(last_diff_dist - diff_dist) < 1e-10 then
return -d*e.nx, -d*e.ny
end
last_diff_dist = diff_dist
Expand All @@ -88,9 +89,11 @@ end
-- : : in direction of the origin.
local function do_line(simplex)
local bx,by, ax,ay = unpack(simplex)

local abx,aby = bx-ax, by-ay

local dx,dy = vector.perpendicular(abx,aby)

if vector.dot(dx,dy, -ax,-ay) < 0 then
dx,dy = -dx,-dy
end
Expand Down Expand Up @@ -163,6 +166,7 @@ local function GJK(shape_a, shape_b)
if vector.dot(ax,ay, dx,dy) <= 0 then
return false
end

simplex[n+1], simplex[n+2] = ax,ay
simplex, dx, dy = do_line(simplex, dx, dy)
n = 4
Expand Down

0 comments on commit 97ae2d8

Please sign in to comment.