Collision reaction

I’m stuck trying to create a framework for my game objects’ collision reaction in a flexible way…

Assuming collision detection is already found, I’m now left with two objects which need to affect each other. Currently i’ve got 4 main catergories of object: Player, AI, Projectile & Powerup. All have a common base class for shared functionality.

Now what? Ideally, i’d like to be able to write a new class, such as a Grenade, which would be of type Projectile, and not have to alter any other classes. Yet all existing classes should interact with the new class properly. In short, adding a new weapon/enemy/powerup should be a single drop-in class.

So far I’ve realised I probably need some sort of priority system for determining which of the two objects takes responsibility of the collision, but beyond that i’m out of ideas. Anyone got any good advice on the subject?

Have a matrix where rows and columns are concrete classes. New write handler objects for each possible collision pair (some might be generic) and register them at the appropriate cell in the matrix.

On a collision event, look up the collision handler from the classes in play and execute it.

If you populate the matrix symmetrically (handler_ij == handler_ji), order might not be important.

If you have a new class, extend the matrix by one row and one column and register handlers describing how the other behave on collision.

class GrenadeAnyCollisionHandler …

While a nice idea, that unfortunatly leads to exactly the situation i’m trying to avoid: specific handler code for each and every combination. Ideally two people could write additional objects and not have to worry about the interaction between the two other than in general terms.

I’m currently leaning towards soemthing like:


if (dynamicObject.reactsTo(hitObject) && hitObject.reactsTo(dynamicObject)
{
    // Find relative priorities

    // Forward to object with highest priority for handling
}

And providing a default behaviour of reactsTo() for each catergory of object (AI reacts to player, projectiles, but not powerups, etc.) but with them being overridable to allow specific behaviour to be added.

Have a quick look through the XAP dev diary for a concise and complete explanation about collision detection. I’m doing exactly what you want to do.

Cas :slight_smile:

i’d use either an interface Collisionable or an abstact class describing all the things they have in common.

then would have a method called reactToCollision (either in the interface or the abstract class. if it’s in the class, the method should be declared abstract).

using this approach you would then have something like this:


public void reactToCollision(Collisionable c) {
    c.doIt(this);
}

perhaps it’s not the most clear description you have read, but it’s pretty basic. i’m also pretty sure this is a well known design pattern, but i can’t remember the name and i don’t have the GOF-book here either (visiting my parents over the weekend. no the book is not visiting my parents, I am)

This is one of the double-dispatch schemes. Could be implemented by the ‘visitor’ pattern.

But doesn’t take you very far. First, it is not symmetric and second you end up very soon with


   if ( c instanceof Bullet )
   ....
   else if ( c instanceof SpaceStation )
   .....
   

This suffers from the option to implement new Collisionables without touching the existant.

Also, kind of collision, location, time, rel. speeds,… are missing for response evaulation. This would lead to an VERY bloated Collisionable interface that anticipates everything you could invent in the future.

As Cas said, check out the XAP diary. He’s using a double-handshake collision processing system that doesn’t require any instanceofs.

Adding a new collidable-with item to his system requires adding another default do-nothing method in the superclass, and then implementing that method on objects that want to do something other than the default.

make different methods that move the object in different ways, and then call them as necesary

Try checking out the collision detection routines in my GAGE library. A lot of people have commented that it’s a very good solution to the traditionally complex issue of collision detection.