How to detect collision for multiple objects
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
How to detect collision for multiple objects
How do I detect collision for multiple objects?
Re: How to detect collision for multiple objects
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.
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.
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
Re: How to detect collision for multiple objects
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
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:
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.
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
Re: How to detect collision for multiple objects
how come my stupid ass never thoght of thatpgimeno 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:
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.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
Who is online
Users browsing this forum: Bing [Bot] and 5 guests