driver.love/0127-collide

15 lines
588 B
Plaintext

-- returns the _minimum separation vector_ if there's a collision
-- requires a pos+halfsize representation for nodes
collide = function(a, b)
local delta = {x=a.pos.x-b.pos.x, y=a.pos.y-b.pos.y}
local abs_delta = {x=math.abs(a.pos.x-b.pos.x), y=math.abs(a.pos.y-b.pos.y)}
local size = {x=a.hs.x+b.hs.x, y=a.hs.y+b.hs.y}
local abs_amount = {x=size.x-abs_delta.x, y=size.y-abs_delta.y}
if abs_amount.x > 0 and abs_amount.y > 0 then
if abs_amount.x <= abs_amount.y then
return {x=abs_amount.x*sign(delta.x), y=0}
else
return {x=0, y=abs_amount.y*sign(delta.y)}
end
end
end