Collision with multiple enemies problem

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
BluBillz
Prole
Posts: 46
Joined: Tue Oct 29, 2013 6:02 pm

Collision with multiple enemies problem

Post 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?
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Collision with multiple enemies problem

Post 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.
User avatar
BluBillz
Prole
Posts: 46
Joined: Tue Oct 29, 2013 6:02 pm

Re: Collision with multiple enemies problem

Post 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?
User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

Re: Collision with multiple enemies problem

Post 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
User avatar
BluBillz
Prole
Posts: 46
Joined: Tue Oct 29, 2013 6:02 pm

Re: Collision with multiple enemies problem

Post 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?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Collision with multiple enemies problem

Post 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.
Help us help you: attach a .love.
User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

Re: Collision with multiple enemies problem

Post 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.
dubem
Prole
Posts: 8
Joined: Fri Mar 27, 2015 1:42 pm

Re: Collision with multiple enemies problem

Post 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!!!
Attachments
collision.love
(851 Bytes) Downloaded 143 times
User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

Re: Collision with multiple enemies problem

Post 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.
dubem
Prole
Posts: 8
Joined: Fri Mar 27, 2015 1:42 pm

Re: Collision with multiple enemies problem

Post by dubem »

You are my hero Kingdaro! I nearly lost my mind on this one.

Thank you very much
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], CleoCommunist, Google [Bot] and 4 guests