Page 1 of 2
Object Collision
Posted: Mon Nov 14, 2022 4:48 pm
by fridays18
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)
Re: Object Collision
Posted: Mon Nov 14, 2022 9:28 pm
by pgimeno
- For each bullet
- For each enemy
- Check if the bullet collides with the enemy.
- End loop of enemies
- End loop of bulets
Re: Object Collision
Posted: Tue Nov 15, 2022 1:27 am
by KalevTait
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.
Re: Object Collision
Posted: Tue Nov 15, 2022 9:19 am
by darkfrei
If the sequence is not important, then the fast removing is just:
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
Also it can be important to define what enemy is near to the bullet and removing the nearest enemy, not the first one.
Re: Object Collision
Posted: Tue Nov 15, 2022 4:42 pm
by fridays18
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
Posted: Tue Nov 15, 2022 8:36 pm
by pgimeno
fridays18 wrote: ↑Tue Nov 15, 2022 4:42 pm
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)
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
Posted: Tue Nov 15, 2022 11:52 pm
by fridays18
pgimeno wrote: ↑Tue Nov 15, 2022 8:36 pm
fridays18 wrote: ↑Tue Nov 15, 2022 4:42 pm
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)
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.
Ooooh okay, I think I understand, ill try it soon thanks for breaking it down for me!
Re: Object Collision
Posted: Tue Nov 15, 2022 11:53 pm
by fridays18
fridays18 wrote: ↑Tue Nov 15, 2022 11:52 pm
pgimeno wrote: ↑Tue Nov 15, 2022 8:36 pm
fridays18 wrote: ↑Tue Nov 15, 2022 4:42 pm
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)
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.
Ooooh okay, I think I understand, ill try it soon thanks for breaking it down for me!
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 work
Re: Object Collision
Posted: Wed Nov 16, 2022 7:49 pm
by pgimeno
fridays18 wrote: ↑Tue Nov 15, 2022 11:53 pm
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 work
'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
Posted: Thu Nov 17, 2022 1:14 pm
by fridays18
pgimeno wrote: ↑Wed Nov 16, 2022 7:49 pm
fridays18 wrote: ↑Tue Nov 15, 2022 11:53 pm
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 work
'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
ooo okay so fore example
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
something like this?