Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Hey guys! My physics engine is coming along quite nicely (thanks for asking!) and I'm ready to start working on some even more advanced junk. Case in point, I'm trying to set up my collision engine so that an arbitrary delegate can be notified when a collision occurs. Let me set up a scenario for you:

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 inform a potentially DIFFERENT delegate about a collision between A and C.

A little background information: I have a known interface for the delegate, I have the potential of storing state for my collision detector (but don't atm), and have the ability to store state 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, allowing the user to change the delegate if desired.

Now, I 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.

So here's the question: fight to the death over a suitable solution. How would YOU solve this problem?

share|improve this question
    
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! –  Grimless Nov 6 '10 at 2:07
add comment

2 Answers 2

up vote 3 down vote accepted

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);
if(ObjA.OnCollision!=ObjB.OnCollision)
    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.

share|improve this answer
add comment

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(; n!=list.end; n++)
  {
     // check for collision here between i and n and call the delegate of either.
  }
}
share|improve this answer
    
Thanks Joe. Luckily for me you knew my intent :) –  CiscoIPPhone Nov 3 '10 at 13:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.