Consider the following:
There is an abstract class Collider
and some more classes that extend this class e.g. RectCollider
and CircleCollider
.
Multiple instances of these are all in the same List<Collider> colliders
.
Now, my question is how I should approach checking for collisions between these colliders while keeping compatability between them. For example, I want to check for collisions between a RectCollider
and another RectCollider
differently from checking a RectCollider
and a CircleCollider
.
My idea so far was using a method to check which types the colliders are of (via instanceof) and then calling different methods depending on the result:
public Collision getCollision(Collider a, Collider b) {
if(a instanceof RectCollider) {
if (b instanceof CircleCollider) {
return rectCircle(a, b);
}
else if (b instance of RectCollider) {
return rectRect(a, b);
}
}
else if(a instanceof CirlceCollider) {
if (b instanceof CircleCollider) {
return cirlceCircle(a, b);
}
else if (b instance of RectCollider) {
return rectCircle(b, a);
}
}
else {
return null;
}
}
The way this method is called at the moment:
getCollision(colliders.get(i), colliders.get(i+1));
However, I'm not sure whether this really is the fastest/best approach, since this method becomes overly complicated real fast when more Collider types are added.
This way, your method size scales quadratically when you add new subclasses. Whether you can simply this, depends on the methods you are calling (rectRect, rectCircle etc.).
Are all of these methods uncorrelated, or does each method with the same shape share a common behaviour? In the former case, you're out of luck. In the latter case, you might want to define this behaviour in the subclasses of Collider and add a general method that extracts this behaviour. This could then be of constant size, e.g.:
public Collision getCollision(Collider a, Collider b) {
Force f1 = a.getCollisionInfo();
Force f2 = b.getCollisionInfo();
return new Collision(f1, f2);
}
Regarding execution performance, it doesn't matter. The JVM optimizes it all for you.