My physics engine is coming along quite nicely, and I'm ready to start working on some more advanced features. I'm trying to set up my collision engine so that an arbitrary delegate can be notified when a collision occurs.

Say we have object A, object B, and object C in the physics simulation. I want to be able to inform a delegate about a collision between A and B, and also inform a potentially different delegate about a collision between A and C.

I have a known interface for the delegate, I have the potential of storing a state for my collision detector, although I currently do not, and have the ability to store states in the objects, themselves. Similarly, I use this delegate model to handle collision resolution, simply setting the physics engine as the delegate for all objects by default, and allowing the user to change the delegate if desired.

I have already tried having each object store it's own collision delegate, that would be informed when a collision occurred. This didn't work, because when the objects had the same collision delegate, the same collision was handled twice. When I switched to only using the delegate of the first object (however that was decided), the order of simulation became an issue. I want to use a dictionary, but that introduces a significant amount of overhead. However, that seems like the direction I need to be heading.

How do I handle my collision resolution using a delegate scheme?

  • \$\begingroup\$ So I solved the issue after some thought: only collisions between different types of objects really matter. So I created a collision manager that has a collision delegate for pairs of classes. This seems to work for now. Thanks anyway! \$\endgroup\$
    – Grimless
    Nov 6, 2010 at 2:07

2 Answers 2


I'm not sure I completely understand your problem, but it seems easy. When your engine is handling a collision, you must have a list of colliding objects. And that list is probably only 2 objects 99% of the time. So, you can simply compare all of the objects' delegates right here. For example: (pseudocode)

// When ObjA and ObjB collide
ObjA.OnCollision(ObjA, ObjB);
    ObjB.OnCollision(ObjA, ObjB);

Order of the simulation shouldn't matter here, because either the delegates are the same anyway, or both will be called.


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:


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:


Pseudocode for the above algorithm:

for(objectIterator i=list.begin; i!= list.end; i++)
  objectIterator n=i;
  for(; n!=list.end; n++)
     // check for collision here between i and n and call the delegate of either.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .