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.
Continuous collision detection techniques attempt to find when two bodies collided between the previous and the current step of the simulation. They compute the time of impact, so we can then go back in time and process the collision at that point.
Collision detection is used to detect the contact of objects within your game. There are a multitude of ways that this can be done. The technique that is used depends on the circumstance. Often the overriding consideration is the maintenance of frame rate within your game.
One of the most primitive ways of doing collision detection is to approximate each object or a part of the object with a sphere, and then check whether spheres intersect each other. This method is widely used even today because it's computationally inexpensive.
What Does Collision Detection Mean? Collision detection algorithmically calculates impact time by identifying two or more object intersection points. Collision detection is also a virtual interface that determines user and object distance for collision prevention.
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++.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With