Rectangle Collision Function Help
Posted: Mon Apr 29, 2013 10:04 pm
Hello so I am making a game where you eat smaller fish to grow larger to eat bigger fish and so I need a way for the player to collide with fish. I tried to make myself a collision function but it doesn't work. Can someone show me a rectangle collision function?
My collision function:
My full program code is below
My collision function:
Code: Select all
function player:collidiesWith(other)
if not other.rect then
other.rect = makeRect(other)
end
local lMost, rMost, bMost, tMost
if self.rect.left < other.rect.left then
lMost = self.rect
rMost = other.rect
else
lMost = other.rect
rMost = self.rect
end
if self.rect.top < other.rect.top then
tMost = self.rect
bMost = other.rect
else
tMost = other.rect
bMost = self.rect
end
local xdist = rMost.left - lMost.right
local ydist = bMost.top - tMost.bottom
return xdist <= 0 and ydist <= 0
end