Call overload with an unknown pointer type

So, I have a base class PhysicsObject, a subclass, Collidingand two classes that stem from this again, Staticand Newtonian.

When checking for collisions, I write all collisions in std::vector< std::pair< Colliding*, Colliding*> > collisionVector. (Collision detection is quite simple, and this is not part of the question.)

Now, after detecting all the collisions, I iterate over collisionVectorand call the static method collide, 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 which objects I encounter every time, and it is possible that I introduce additional subclasses Colliding.

How can I decide which overload needs to be caused in a simple way?


Current code:

This is what my code looks like. Not really.

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);
        }
    }
}
+4
1

, ++, ( ...).

2 ( ), , . , , N ^ 2.

- Colliding, , . .

+1

Source: https://habr.com/ru/post/1539248/


All Articles