Skip to main content
1 of 2
CiscoIPPhone
  • 5.3k
  • 3
  • 33
  • 39

For my answer I'll assume you are managing lists of objects.

list<Object> objects;

It sounds to me like you are checking for collision by checking if each object has collided with any other object, for example if you have three objects in the list it would do nine comparisons - if you had four objects (A,B,C,D) you would check for collisions with the following combinations:

AB, AC, AD,
BA, BC, BD,
CA, CB, CD
DA, DB, DC

This isn't required, you only need to check for collisions that haven't already been compared - in the above algorithm AB and BA is being compared, which isn't necessary.

In the case of four objects ABCD you need to check for collisions with the following combinations:

AB, AC, AD
BC, BD
CD

Pseudocode for the above algorithm:

for(objectIterator i=list.begin; i!= list.end; i++)
{
  objectIterator n=i; n++;
  for(; i!=list.end; i++)
  {
     // check for collision here between i and n and call the delegate of either.
  }
}
CiscoIPPhone
  • 5.3k
  • 3
  • 33
  • 39