Object Collision

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.
User avatar
fridays18
Citizen
Posts: 90
Joined: Tue Nov 01, 2022 3:24 pm

Object Collision

Post 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 :3

(Side note: im using Classic to do all the objects)
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Object Collision

Post by pgimeno »

  • For each bullet
    • For each enemy
      • Check if the bullet collides with the enemy.
    • End loop of enemies
  • End loop of bulets
KalevTait
Prole
Posts: 7
Joined: Sun Aug 19, 2018 10:28 am

Re: Object Collision

Post 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.
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: Object Collision

Post 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.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
fridays18
Citizen
Posts: 90
Joined: Tue Nov 01, 2022 3:24 pm

Re: Object Collision

Post 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)
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Object Collision

Post 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.
User avatar
fridays18
Citizen
Posts: 90
Joined: Tue Nov 01, 2022 3:24 pm

Re: Object Collision

Post 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!
User avatar
fridays18
Citizen
Posts: 90
Joined: Tue Nov 01, 2022 3:24 pm

Re: Object Collision

Post 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
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Object Collision

Post 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
User avatar
fridays18
Citizen
Posts: 90
Joined: Tue Nov 01, 2022 3:24 pm

Re: Object Collision

Post 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?
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest