This is with respect to a physics engine. Once a collision occurs, it returns me the information that two gameObjects are colliding. All entities, like player, monster, bullet etc are derived (inherit) from GameObject.
What is a good way to identify which specific gameObjects are colliding ?
Enums: I have thought about using enums, but as soon as you start adding more entities, it becomes un-manageable to keep a track of all the possible combinations.
EDIT: I found a potential solution here for C#: http://blogs.msdn.com/b/laurionb/archive/2009/08/13/multimethods-in-c-4-0-with-dynamic.aspx
But still, if I have to do this in C++, how would I do it ?
The typical solution is to do two levels of dispatch like this:
It gets a bit cumbersome as the number of different classes goes up, but it has some things going for it:
The downside is:
A more extensible solution is to build a 2D dispatch table where each cell indentifies a pair of colliding classes. In each cell, you put a function that handles collision between those two class types. That works well too, but it cheats the type system a bit. You'll end up doing some casting and you'll have to come up with a good way to identify a class.
I would not use C#'s
dynamic
support. My strong hunch is that it will be too slow for a physics engine.I'll also point out that there's a Game Development Stack Exchange that's much better at answering questions like this. See you there!