I am writing some code to detect collision between different shapes, RectShape,CircShape,LineShape all inheriting from a base class Shape. My current implementation works really badly. First, I defined a bunch of case-specific functions:
bool LineLineCollision(LineShape& line1, LineShape& line2) {
// do some math
}
bool CircRectCollision(CircShape& circ, RectShape& rect) {
// some more math
}
// and so on for all cases
obviously all of these are required in any implementation. The issue is in the function bool TestCollision(Shape& shp1, Shape& shp2) which takes the two parameters, then goes through many if statements to compare typeid(shp1) and typeid(shp2) to the typeid of all my derived classes. It's dirty and messy but I didn't have any other ideas at the time. Now I'd like to clean it up. Adding a new type of shape is unimaginably annoying and time consuming.
My new idea right now is simply overloading TestCollision with arguments fitting different parameter types, but then I'll have to have TestCollision(CircShape&, RectShape&) and TestCollision(RectShape&, CircShape&) as two separate overloads and that's not maintainable. Another idea is to implement the functions inside the derived classes themselves, but it still results in duplication of code and is even less maintainable.
How would I tackle this issue?
Shapeis a pure abstract class)