How to detect collision for multiple objects

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
austingae
Prole
Posts: 2
Joined: Thu Jun 06, 2019 7:25 am

How to detect collision for multiple objects

Post by austingae » Thu Jun 06, 2019 7:26 am

How do I detect collision for multiple objects? :awesome:

User avatar
veethree
Inner party member
Posts: 820
Joined: Sat Dec 10, 2011 7:18 pm

Re: How to detect collision for multiple objects

Post by veethree » Fri Jun 07, 2019 11:45 am

1. Loop through all the objects
2. For each object, Loop through all the objects again. This is called a nested loop.
3. Make sure the 2 objects you're checking aren't the same object.
4. Check if the 2 objects are colliding


In code this looks something like this. Note that this code is completely untested, and unoptimized.

Code: Select all

for i,a in ipairs(objects) do
		for o,b in ipairs(objects) do
			if o ~= i then --This makes sure a and b aren't the same object.
				if collision(a, b) then --This would be a function that checks if 2 objects are colliding.
					--Do collision things here.
				end
			end
		end
	end
Something like this would be fine if you just have a few objects, But if you have alot of objects you'd need to optimize it somehow.

TheHUG
Citizen
Posts: 59
Joined: Sun Apr 01, 2018 4:21 pm

Re: How to detect collision for multiple objects

Post by TheHUG » Fri Jun 07, 2019 1:21 pm

austingae wrote:
Thu Jun 06, 2019 7:26 am
How do I detect collision for multiple objects? :awesome:
Love.physics has collision detection (and so do wrappers for it https://github.com/HDictus/breezefield)

But if all you need is collision detection then HardonCollider is more minimalistic https://github.com/vrld/HC

If you want to implement it from scratch you can do what veethree said if out only have a few objects, otherwise you need some way to spatially index them (in 3d you'd use an octree, in 2d you can do something similar with a quadtree)

User avatar
pgimeno
Party member
Posts: 2340
Joined: Sun Oct 18, 2015 2:58 pm

Re: How to detect collision for multiple objects

Post by pgimeno » Fri Jun 07, 2019 1:25 pm

Normally there's no need to check collision between A and B and then between B and A. This way, the number of collision checks can be cut in half just by doing the second loop over all objects with an index greater then the current one:

Code: Select all

for i = 1, #objects - 1 do
  local object1 = objects[i]
  for j = i + 1, #objects do
    local object2 = objects[j]
    if collision(object1, object2) then
      -- handle collision
    end
  end
end
Still, yeah, for lots of objects you need some partitioning scheme like grid partitioning or quad trees, so you don't check collisions between objects that are far away enough as to not be possibly colliding.

User avatar
veethree
Inner party member
Posts: 820
Joined: Sat Dec 10, 2011 7:18 pm

Re: How to detect collision for multiple objects

Post by veethree » Fri Jun 07, 2019 8:19 pm

pgimeno wrote:
Fri Jun 07, 2019 1:25 pm
Normally there's no need to check collision between A and B and then between B and A. This way, the number of collision checks can be cut in half just by doing the second loop over all objects with an index greater then the current one:

Code: Select all

for i = 1, #objects - 1 do
  local object1 = objects[i]
  for j = i + 1, #objects do
    local object2 = objects[j]
    if collision(object1, object2) then
      -- handle collision
    end
  end
end
Still, yeah, for lots of objects you need some partitioning scheme like grid partitioning or quad trees, so you don't check collisions between objects that are far away enough as to not be possibly colliding.
how come my stupid ass never thoght of that

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 24 guests