9

The code given is for example purposes, not copypasta from my codebase

I'm writing a primitive, cross-platform Snake game in C++ with Boost & SDL, and I'm wondering what the best way to implement collision-handling is (not collision-detection). So far, I've been using a single-dispatch idea, with ugly spaghetti code, like so:

void Snake::CollisionHandler(const WorldObject& obj)
{
    // collided with self
    if(obj.GetObjectType() == snake)
        Die();
    ...
    ...
}

Also, I have a "global" collision handler, which does things involved in a collision, which are independent of each object, for example:


void GameWorld::CollisionHandler(WorldObject& obj1, WorldObject& obj2)
{
    if(obj1.GetObjectType() == snake && obj2.GetObjectType() == snake)
        PlayDeathSound();
    ...
    ...
}

To avoid things like sounds being played twice for collisions.

I've also considered double-dispatch, like so:


void Snake::CollisionHandler(WorldObject& obj) const
{
    // invoke obj's collision handler with a const Snake& parameter
    obj.CollisionHandler(*this);
}
// collided with self
void Snake::CollisionHandler(const Snake& obj)
{
    Die();
}

This also includes a similar global collision handler like above.

Then there's the approach of only having a global collision handler (which is a friend function to all game objects), like so:


void GameWorld::CollisionHandler(WorldObject& obj1, WorldObject& obj2)
{
    // snake collided with self
    if(obj1.GetObjectType() == snake && obj2.GetObjectType() == snake)
    {
        obj1.Die();
        obj2.Die();
        PlayDeathSound();
    }
    ...
    ...
}

Are there any strategies I'm missing? Which of these ends up being the nicest? They all seem to have some ugly code involved, and the single- and double- dispatch ones involve polymorphism, which I personally try to shy away from.

2
  • In your last sample, the comment reads "snake collides with self", and then you call Die() twice. I suspect that would be A Bad Thing. PS: No need to "friend" anything, just make the functions that it needs public. Dec 6, 2010 at 18:51
  • PPS: To avoid duplicating code, I tend to only put the effects on that object in it's "I collided with a Foo" function. EX: When a ship collides with a bullet, it takes some damage. When a bullet collides with a ship, it Goes Away. Dec 6, 2010 at 18:56

2 Answers 2

5

Collision handling is an area where C++/Java-style object orientation (with single dispatch) is not flexible enough. The result of a collision depends on the types of both colliding objects, so you need multiple dispatch to handle it. (It's no coincidence that the motivating example on Wikipedia's multiple dispatch article is collision handling!)

I think that a global collision handler that then dispatches to individual object methods is the best workaround for this inadequacy in C++.

4
  • That's essentially what I have now. Actually, there are a few hacky ways of getting double-dispatch in C++, in Effective C++. One of them is shown in my original post (the third block of code)
    – bfops
    Dec 5, 2010 at 21:13
  • Yes, but I think that's a weaker workaround in this case. If you have code in class A for collisions with class B and separate code in class B for collisions with class A then there's the possibility that you might inadvertently make them inconsistent. (Perhaps in your game only snakes can collide with things so this would not happen in practice, but in a more general context it would be a risk worth thinking about.) Dec 5, 2010 at 21:17
  • I don't see what you mean; everything handles its own collisions with everything else. A only handles its own end of things on collision with B, and B only handles its end in collisions with A. I'm not sure I understand what you mean by "inconsistent"
    – bfops
    Dec 6, 2010 at 20:44
  • It would be inconsistent if you made a mistake (for example, you decide to change some aspect of A/B collisions and you change it in class A but not in class B). Obviously if you never make mistakes then it doesn't matter how you organize your code, but for fallible mortals like me it can help to keep related code together. Dec 6, 2010 at 20:50
1

IMO the last approach of letting the game class completely handle collisions and other global events is the wisest and possibly the most "correct" way of doing it.
For example take a chess program. It is pretty obvious that the game class should handle the case of win or lose by one of the players, and not the player class.
It is also more more effective practically. Think of a case where you want to add more classes (e.g another type of snake), instead of duplicating code or overriding CollisionHandler you can just let the game class handle it with much less effort.

However, if your only purpose is a simple snake game it is probably not that important how you choose to implement it.

3
  • If I add another kind of Snake class, wouldn't it be simpler to use the double-dispatch method? No code duplication necessary, plus it's separated into modules nicely.
    – bfops
    Dec 5, 2010 at 18:19
  • 1
    oops. There is minor code duplication for the one-line generic CollisionHandler(WorldObject&) function
    – bfops
    Dec 5, 2010 at 18:26
  • 1
    If you will inherit from WorldObject you will need to duplicate code. Plus, as a thumb rule it is better not to use function overloading when you don't have to.
    – stnr
    Dec 5, 2010 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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