Zilarrezko wrote:I'm commenting on this so it'll bump up and the omniscients will take over.
It worked indeed.
Well, The Great
Robin is the only omniscient here.
RyanImperion wrote:What I'm trying to do is have every mob check with other mobs to see if they're colliding.
I took a quick peek at your code, and I can suggest a few things on the overall. But let us focus on the collision issue for now.
For such a problem, you normally won't need to use love.physics. Instead, you can boil down to problem to tinier pieces, as follows.
First of all, you want to detect if two entities (two zombies, here) are colliding. Each zombie is represented with a square. I'll assume that
x,y refers to the top-left position of the entity sprite, and
w,h refers to its width and height.
That said, you can use a simple utility function which checks if two AABBs (axis-aligned bounding boxes), meaning here "rectangles", are overlapping. It will take as input the two entities tables and perform the check. The following is just a rewrite of
this sample on the wiki.
Code: Select all
function collides(entityA, entityB)
return entityA.x < entityB.x + entityB.w
and entityB.x < entityA.x + entityA.w
and entityA.y < entityB.y + entityB.h
and entityB.y < entityA.h + entityA.h
end
In case you need to deal with some extra space, it remains fairly simple to tweak the previous function to achieve that. You will just have to increase accordingly the with and height of the entities bounding boxes, and maybe shift to the left and to the top the x,y in case you want to "center" the bounding box inside the extra space.
Second, the collision check actually. Since you keep maintained a list of all the entities, you can perform this check very easily :
Code: Select all
for i, entityA in ipairs(listOfEntities) do
for j, entityB in pairs(listOfEntities) do
if entityA ~= entityB and collides(entityA, entityB) then
-- stop moving for both entityA and entityB
-- optionally, you can calculate their displacement vector and shift them appart so that they do not overlap
end
end
end
The above should work, but it has two flaws, mainly: it is not really efficient and not clever at all. Because of one simple thing: collision is commutative. Meaning that the statement "A collides with B" is strictly equivalent to "B collides with A". Therefore, you can notice that with the previous implementation, we are performing two times some checks.
To fix, this, we can proceed as follows:
Code: Select all
for i = 1, #listOfEntities-1 do
for j = i+1, #listOfEntities do
local entityA = listOfEntities[i]
local entityB = listOfEntities[j]
if collides(entityA, entityB) then
-- stop moving for both entityA and entityB
-- optionally, you can calculate their displacement vector and shift them appart so that they do not overlap
end
end
end
This reduces fairly well the number of collision checks and should be faster than the previous.
Hope this helps.