I'd like to write an efficient Collision Response code, that does not use instanceof/type checks that often (if possible)

my current implementation is something like this:

GameObject.collidesWith(GameObject other)
    if(other instanceof ConcreteType1)
        //do response with Concrete Type 1
    }else if (other instanceof ConcreteType2)
        // do response with Concrete Type 2

GameObject is the base class of everything. Is there a possibility to avoid instanceof and type checks?

  • \$\begingroup\$ Make base classes like, BasicConcrete, then all new Concrete must extend this class, unless if you want different behavior for each concrete type \$\endgroup\$
    – DH.
    Commented Jan 20, 2016 at 11:12
  • \$\begingroup\$ What is your requirement about this? Why don't you want to use instanceof? What language are you using? \$\endgroup\$
    – Vaillancourt
    Commented Jan 21, 2016 at 4:29
  • \$\begingroup\$ Take a look at en.wikipedia.org/wiki/Double_dispatch , maybe you can use this idiom in your programming language. \$\endgroup\$
    – Exilyth
    Commented Feb 19, 2016 at 18:59

1 Answer 1


This is the part I currently need to work on for my own game..

What I've reasoned out so far is that I can have a CollisionBox on the GameObject which contains all the collision boundary information. Collisions are checked by GameObject.isColliding(other) - where the two CollisionBoxes compare whether they intersect.

if (gameObject.isColliding(other))

then the GameObject has a doCollision method which is overridden for various GameObjects

public GameObject
    CollisionBox collisionBox;
    CollisionType collisionType;

    public void doCollision(GameObject other)
            case Inelastic: //etc
            case Elastic: //etc
            case Consumable: //(this one is for bullets, upgrades, pickups - 
                // things you collide with but the collision doesn't affect motion)

As you can maybe guess, CollisionType is an enum which is assigned to GameObject.

That was my initial plan anyway. Now I hope to take some inspiration from Collision detection and response in an Entity System - though I'll still probably end up with some type of "collisionType enum" to broadly group a type of collision.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .