Page 1 of 1

How to detect collision for multiple objects

Posted: Thu Jun 06, 2019 7:26 am
by austingae
How do I detect collision for multiple objects? :awesome:

Re: How to detect collision for multiple objects

Posted: Fri Jun 07, 2019 11:45 am
by veethree
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.

Re: How to detect collision for multiple objects

Posted: Fri Jun 07, 2019 1:21 pm
by TheHUG
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)

Re: How to detect collision for multiple objects

Posted: Fri Jun 07, 2019 1:25 pm
by pgimeno
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.

Re: How to detect collision for multiple objects

Posted: Fri Jun 07, 2019 8:19 pm
by veethree
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