Page 1 of 1

Collision with multiple enemies problem

Posted: Sat Mar 14, 2015 3:13 pm
by BluBillz
I am using Boundingbox.lua for collision. I have multiple enemies spawning from the same table they load from.

My question is how can i have collision with the enemies from the same table? so that the enemies will collide with each other?

Re: Collision with multiple enemies problem

Posted: Sat Mar 14, 2015 5:25 pm
by ivan
Collisions are usually checked in pairs, something like:

Code: Select all

for i = 1, #enemies do
  for j = i + 1, #enemies do
     check_pair(enemies[i], enemies[j])
  end
end
Just don't remove or insert new objects in the table during the iteration or the indexes will get out of sync.

Re: Collision with multiple enemies problem

Posted: Sat Mar 14, 2015 11:55 pm
by BluBillz
ivan wrote:Collisions are usually checked in pairs

hmm i don't know what you mean by checked in pairs. What is this?

Re: Collision with multiple enemies problem

Posted: Sun Mar 15, 2015 12:06 am
by Kingdaro
He just means that they're checked two at a time. You go through all the enemies as you're going through all the enemies, therefore checking every single one against every single other, like in the code example ivan gave.

An extra note, you'll want to make sure you aren't checking an enemy against itself:

Code: Select all

for i = 1, #enemies do
  for j = i + 1, #enemies do
    if enemies[i] ~= enemies[j] then
      check_pair(enemies[i], enemies[j])
    end
  end
end

Re: Collision with multiple enemies problem

Posted: Sun Mar 15, 2015 12:55 am
by BluBillz
Kingdaro wrote:He just means that they're checked two at a time. You go through all the enemies as you're going through all the enemies, therefore checking every single one against every single other, like in the code example ivan gave.

An extra note, you'll want to make sure you aren't checking an enemy against itself:

Code: Select all

for i = 1, #enemies do
  for j = i + 1, #enemies do
    if enemies[i] ~= enemies[j] then
      check_pair(enemies[i], enemies[j])
    end
  end
end
I'm not understanding what check_pair is still to write it as a function..because i still don't understand what you mean by that?

Re: Collision with multiple enemies problem

Posted: Sun Mar 15, 2015 11:34 am
by Robin
Kingdaro wrote:An extra note, you'll want to make sure you aren't checking an enemy against itself:
That's not necessary. Look at the loop indices.

The point of starting j at i + 1 is that no enemy collides with itself, and different enemies don't collide twice.

Re: Collision with multiple enemies problem

Posted: Sun Mar 15, 2015 1:29 pm
by Kingdaro
Robin wrote:
Kingdaro wrote:An extra note, you'll want to make sure you aren't checking an enemy against itself:
That's not necessary. Look at the loop indices.

The point of starting j at i + 1 is that no enemy collides with itself, and different enemies don't collide twice.
Ah, neat. Didn't see that.
BluBillz wrote:I'm not understanding what check_pair is still to write it as a function..because i still don't understand what you mean by that?
That's just a placeholder function for whatever you want to do with those two enemies. Just forget about the terminology, it's pretty much exactly what you'll want.

Re: Collision with multiple enemies problem

Posted: Fri Mar 27, 2015 1:55 pm
by dubem
Hey gang!

I tried to do the collision check between entities in the same table but it won't work for me.

Maybe someone with a better understanding than me can check my code.

btw: first post, i love this forum, started with love2d a few weeks ago, hi to everyone here!!!

Re: Collision with multiple enemies problem

Posted: Fri Mar 27, 2015 5:49 pm
by Kingdaro
There's a mistake in your checkCollision function.

Code: Select all

function checkCollision(a, b)

	return a.x < b.x + b.w
	and b.x < a.x + a.w
	and a.y < b.y + b.h
	and b.y < a.y + a.h
	-- previously: and b.y < a.h + a.h

end
Also, in the update loop, the logic of setting the boxes' overlap properties is faulty. With the way you have it, the game will find two blocks that are colliding, and set their overlap to true. But then it'll find two blocks that aren't colliding, and it'll set them to false whether they are or not. Here's a fixed update method:

Code: Select all

function love.update(dt)

	for i = 1, #box do
		moveBox(box[i],dt)
		box[i].overlap = false
	end

	for i=1,#box-1 do

		for j=i+1,#box do

			if checkCollision(box[i],box[j]) then
				box[i].overlap = true
				box[j].overlap = true
			end
		end
	end

end
This instead sets all box overlaps to false, then reverts any of them to true if need be.

Re: Collision with multiple enemies problem

Posted: Fri Mar 27, 2015 7:54 pm
by dubem
You are my hero Kingdaro! I nearly lost my mind on this one.

Thank you very much