Code: Select all
function CollisionManager:collision()
local isCollide = false
for i = 0, self.colCount - 1, 1 do -- start for1
isCollide = false
for j = 0, self.colCount - 1, 1 do --start for2
if j ~= i and self.colTable[i].colNum ~= self.colTable[j].colNum then -- start if1
if self.colTable[i].box:isContact(self.colTable[j].box) then -- start if2
isCollide = true
if self.colTable[i].collide ~= nil then start --if3
if type(self.colTable[i].collide) == "function" then --start if4
self.colTable[i]:collide(self.colTable[j])
end --end if4
end --end if3
end -- end if2
if isCollide == false and j ~= i then -- start if5
if self.colTable[i].collide ~= nil then -- start if6
if type(self.colTable[i].collide) == "function" then --start if7
self.colTable[i]:collide(nil)
end --end if7
end -- end if6
end -- end if5
end --end if if1
end --end for2
end --end for1
end --end function
(pastebin version here)
Here's what's happening:
The nested loop (for1 and for2) is designed to scroll through an array (or a metatable in this case) and compare each index with every other index in the table EXCEPT itself. As the code is pretty obvious, the idea here is to check for collisions.
My problem is this: if1 is correct and successfully skips executing if2 (therefore if3 and if4 are also skipped); however, when if2 is skipped, if5 *always* returns true UNLESS isCollide == true -- this is true even when i and j are equal!
When if2 is not skipped, if5 behaves as I'm expecting it to.
I know I *could* just check for nil and the objects own ID int he member:collide function, but dammit that wouldn't explain why I'm getting this behavior.
Any sane help for an insane function?