So, I have a base class PhysicsObject
, a sub-class Colliding
, and two classes that derive from that again, Static
and Newtonian
.
When checking for collisions, I write all collisions to a std::vector< std::pair< Colliding*, Colliding*> > collisionVector
. (The collision detection is fairly basic and it's not really part of the question.)
Now, after all collisions were detected, I iterate over the collisionVector
and call the static collide
method, which has these four overloads:
void collide(Newtonian* first, Newtonian* second);
void collide(Newtonian* object, Static* obstacle);
inline void collide(Static* obstacle, Newtonian* object){ collide(object, obstacle); }
void collide(Static* first, Static* second);
The problem is that I have to check what kind of objects I'm dealing with every single time, and it is possible that I introduce more subclasses of Colliding
.
How do I decide which overload to call in an easy manner?
Current code:
This is how my code currently looks. Not pretty.
for (pair<Collision*,Collision*> coll : collisionVector){
Colliding* first = get<0>(coll);
Colliding* second = get<1>(coll);
if (Newtonian* firstNewt = dynamic_cast<Newtonian*>(first)){
if (Newtonian* secNewt = dynamic_cast<Newtonian*>(second)){
collide(firstNewt, secNewt);
}
else if(Static* secStat = dynamic_cast<Static*>(second)){
collide(firstNewt, secStat);
}
}
else if(Static* firstStat = dynamic_cast<Static*>(first)){
if (Newtonian* secNewt = dynamic_cast<Newtonian*>(second)){
collide(firstStat, secNewt);
}
else if(Static* secStat = dynamic_cast<Static*>(second)){
collide(firstStat, secStat);
}
}
}