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?
Collision with multiple enemies problem
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Collision with multiple enemies problem
Collisions are usually checked in pairs, something like:
Just don't remove or insert new objects in the table during the iteration or the indexes will get out of sync.
Code: Select all
for i = 1, #enemies do
for j = i + 1, #enemies do
check_pair(enemies[i], enemies[j])
end
end
Re: Collision with multiple enemies problem
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
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:
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
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?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
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Collision with multiple enemies problem
That's not necessary. Look at the loop indices.Kingdaro wrote:An extra note, you'll want to make sure you aren't checking an enemy against itself:
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.
Re: Collision with multiple enemies problem
Ah, neat. Didn't see that.Robin wrote:That's not necessary. Look at the loop indices.Kingdaro wrote:An extra note, you'll want to make sure you aren't checking an enemy against itself:
The point of starting j at i + 1 is that no enemy collides with itself, and different enemies don't collide twice.
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.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?
Re: Collision with multiple enemies problem
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!!!
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 154 times
Re: Collision with multiple enemies problem
There's a mistake in your checkCollision function.
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:
This instead sets all box overlaps to false, then reverts any of them to true if need be.
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
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
Re: Collision with multiple enemies problem
You are my hero Kingdaro! I nearly lost my mind on this one.
Thank you very much
Thank you very much
Who is online
Users browsing this forum: Ahrefs [Bot] and 10 guests