Interaction between objects from one list

Monkey Forums/Monkey Programming/Interaction between objects from one list

lom(Posted 2015) [#1]
I have a list of 10 objects. What is a proper way to detect collisions between these objects?


Nobuyuki(Posted 2015) [#2]
elaborate a "collision".

Making a check between 10 objects per frame by normal means will run through 55 checks unless you use a way to partition the objects to reduce that using something, like a quadtree. The "proper way" depends on what your application is. With such a small amount of objects you don't have to be super-efficient. You may want to use a different container, though, since lists aren't a great way to make comparisons between all objects contained within them due to the way you need to access them. An array or Stack<T> would be better. Then, you can check using a nested loop. Pseudocode:

For local i:Int = 0 until list.Length -1
  For local j:Int = i+1 until list.Length
   If list[i].CollidesWith( list[j] ) Then 'do collision code here
  Next
Next



lom(Posted 2015) [#3]
Nobuyuki,

Thanks, I'll try this!


Gerry Quinn(Posted 2015) [#4]
For such a short list the best optimisation is probably to speed up testing on each pair. So make sure your collision checking method starts with a quick check in case they are a long way apart on the x or y axis, in which case you can return false straight away. You only need to get fancy when the objects are close together.


Amon(Posted 2015) [#5]
For local i:Int = 0 until list.Length
  For local i2:Int = 0 until list.Length

   If i <> i2
      Exit
   Else
     Collision Code Here
   EndIf
  Next
Next