I'm programming a game where various GameElement classes can collide with each other. All GameElements are stored in a main list. This list is iterated over to determine collisions, and then each GameElement in the main list is given a list of other GameElements it collided with. So far so good
I have several Classes derived from GameElement, for example Tree or Car.
Now when handling the collision between Tree and Car a Tree may wish to call getEngineType() if the GameElement it collided with is in fact a Car. Or the Car may wish to call getTrunkLength() if the GameElement it collided with is in fact a Tree.
Currently im calling instanceof to determine what subclass im dealing with, but this feels unclean. The only solution i can think of is to have seperate lists for Tree and Car objects, but this could get cumbersome as the number of classes grows.
Is there a best practice to deal with interactions between "similar" objects (GameElements) when these interactions depend on subclass specific methods and members?
EDIT: collision detection is not the issue! EDIT2: a similar question that uses the same instanceof solution i currently am Avoiding instanceof in Java