I have a function that takes two objects and uses a switch on the enum types of the objects.
The problem I have is this is getting painful to manage as I add more and more primitive types and i need two case statements for each variation.
My switch is setup like this:
switch (obj1.Type, obj2.Type){ case (GeometryType.Polygon, GeometryType.Polygon): return GetIntersectingPoints(obj1 as Polygon, obj2 as Polygon, contactPoints); case (GeometryType.Polygon, GeometryType.Circle): return GetIntersectingPoints(obj1 as Polygon, obj2 as Circle, contactPoints); case (GeometryType.Polygon, GeometryType.Arc): return GetIntersectingPoints(obj1 as Polygon, obj2 as Arc, contactPoints); case (GeometryType.Polygon, GeometryType.Line2D): return GetIntersectingPoints(obj1 as Polygon, obj2 as LineSegment, contactPoints); case (GeometryType.Polygon, GeometryType.BiArc): return GetIntersectingPoints(obj1 as Polygon, obj2 as BiArc, contactPoints); //same as second case but the other way around case (GeometryType.Circle, GeometryType.Polygon): return GetIntersectingPoints(obj2 as Polygon, obj1 as Circle, contactPoints); // and so on Since the objects provided could either be A and B or B and A every time i add a new primitive type this switch becomes painfully longer and longer and its just a huge hassle to deal with.
Is there a smarter design pattern to do this that requires less management? How do people usually handle this situation in physics collision design ?
GetIntersectingPointsis commutative, you can deal with "the other way around" issue by swappingobj1andobj2if necessary so thattype(obj1) <= type(obj2)(you may need to define an ordering for your types. It doesn't matter what it is so long as it's consistent). This won't fix your combinatorial explosion problem though.