Recently ive wanted to up my love2d knowledge and started using class's, well in my most recent game theres 2 reoccurring(Theres multiple of the same object in action) classes, enemies and bullets, im trying to find a way to detect if one of the many possible bullets on the screen hits one of the many possible enemies remove the enemy and bullet from the list they are held in. I know how to to 1 on 1 collision where everything is already defined but I have no clue how to detect collision between a bullet and enemy when there can be a ton on screen so theres no defining variables or anything, any help would be very appreciated
(Side note: im using Classic to do all the objects)
Object Collision
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Object Collision
- For each bullet
- For each enemy
- Check if the bullet collides with the enemy.
- End loop of enemies
- For each enemy
- End loop of bulets
Re: Object Collision
Just to add to the above, you'll need to keep a list of all bullets and a list of all enemies, and whenever either is created it gets added to its respectie list, and when you detect a collision remove the bullet from the list of bullets and the enemy from the list of enemies.
However, you shouldn't edit the contents of a list while iterating through it, so you'll need to create temporary lists of bullets to remove and enemies to remove, and once you've found all collisions, iterate through these two lists to do the actual removals from your original lists.
You'll also want to iterate through these lists to draw and update your bullets and enemies, so that when they are removed, they automatically stop being drawn.
However, you shouldn't edit the contents of a list while iterating through it, so you'll need to create temporary lists of bullets to remove and enemies to remove, and once you've found all collisions, iterate through these two lists to do the actual removals from your original lists.
You'll also want to iterate through these lists to draw and update your bullets and enemies, so that when they are removed, they automatically stop being drawn.
Re: Object Collision
If the sequence is not important, then the fast removing is just:
Also it can be important to define what enemy is near to the bullet and removing the nearest enemy, not the first one.
Code: Select all
local i = 1
while tabls[i] do
if tabls[i].valid then
i = i+1 -- go to next
else
tabls[i], tabls[#tabls] = tabls[#tabls], nil -- erasing with the last
end
end
Re: Object Collision
I appreciate the responses but what I was mainly asking for is how I would detect a collision between the bullets and enemys(both on lists)
Re: Object Collision
Do you know how to iterate through the elements of a list? You said you know how to detect a 1 on 1 collision, hence my response. You basically iterate through all enemies and all bullets, and check if there's a 1 on 1 collision between each of the bullets you're iterating through and each of the enemies you're iterating through.
Re: Object Collision
Ooooh okay, I think I understand, ill try it soon thanks for breaking it down for me!pgimeno wrote: ↑Tue Nov 15, 2022 8:36 pmDo you know how to iterate through the elements of a list? You said you know how to detect a 1 on 1 collision, hence my response. You basically iterate through all enemies and all bullets, and check if there's a 1 on 1 collision between each of the bullets you're iterating through and each of the enemies you're iterating through.
Re: Object Collision
Im still kind of lost, all the loops I have for the lists (for i,v in ipairs(listOfBullets) do) and the same for enemies im not quite sure how I would make it workfridays18 wrote: ↑Tue Nov 15, 2022 11:52 pmOoooh okay, I think I understand, ill try it soon thanks for breaking it down for me!pgimeno wrote: ↑Tue Nov 15, 2022 8:36 pmDo you know how to iterate through the elements of a list? You said you know how to detect a 1 on 1 collision, hence my response. You basically iterate through all enemies and all bullets, and check if there's a 1 on 1 collision between each of the bullets you're iterating through and each of the enemies you're iterating through.
Re: Object Collision
'v' is each bullet. You need to use an identifier different to 'v' for enemies, for example v2, and then check if there's a collision between v and v2.
For example:
Code: Select all
for i,v in ipairs(listOfBullets) do
for i2, v2 in ipairs(listOfEnemies) do
-- check if there's a collision between v and v2, and do something if so
end
end
Re: Object Collision
ooo okay so fore examplepgimeno wrote: ↑Wed Nov 16, 2022 7:49 pm'v' is each bullet. You need to use an identifier different to 'v' for enemies, for example v2, and then check if there's a collision between v and v2.
For example:Code: Select all
for i,v in ipairs(listOfBullets) do for i2, v2 in ipairs(listOfEnemies) do -- check if there's a collision between v and v2, and do something if so end end
Code: Select all
for i,v in ipairs(listOfBullets) do
for i2, v2 in ipairs(listOfEnemies) do
x = basicCollisionFunction(v.x,v2.x,v.y,v2.y)
end
Who is online
Users browsing this forum: Bing [Bot] and 6 guests