Let's say I have two different types of colliders, circles and boxes, that are derived from the same base collider class. I have an entity class that contains a pointer to a collider that can be a circle collider or a box collider.
class Collider {};
class CircleCollider : public Collider
{
// Defines a circle
};
class BoxCollider : public Collider
{
// Defines a rectangle
};
class Entity
{
Collider* collider;
};
I want to make a collision handler that I can just pass a bunch of entities and let it figure out how to resolve their collisions but in order to do that the handler needs to know what types of colliders it is dealing with.
When the problem is framed liked this, the only solution appears to be downcasting but I am wondering whether I am simply approaching it the wrong way. This seems like a common scenario but I am having trouble finding solutions which makes me suspect that either there is some other approach that I am not seeing or that this is a case where one simply has to make use of downcasting.
Since the collision handling is specific to different pairs of colliders, it doesn't seem like I can make use of the visitor pattern here or am I wrong?