# Collision Detection Game Design and Architecture

I've reading some articles about collision detection. My question here is about ideas on the design for it.

Baically I have a C++ game that has a main loop with entities with an update method. Based on keyboard input, these characters updates their positions.

My question is not about how to detect collisions, it's about getting ideas in which is the best way to implement this. The game has a main character but also enemies that have to collide between them, so I'm not sure where to make all the iterations for checking collisions and if the right way is to check everything against everything.

Thanks in advance.

-
There are many approaches, one would be to check every entity against every other entity and instead of calculating if they already collided, calculate when they will collide (if at all), and add the collision event to some array. Next sort the array by the collision time, take the first element, apply post-collision effects (change their positions and velocities) and either recalculate the rest of the array or do some advanced stuff on calculating which entities can be affected by this collision resolution and recalculate collision times only for them. – Markus von Broady Nov 8 '12 at 18:52

## 3 Answers

Depends a bit how in depth you want to go. For simplicity I would suggest either a Observer Pattern for a CollisionDetector class or a simple Aggregation (the CollisionDetector has a reference to the world which contains everything)

Basically you would have to check everything against everything. In an optimization you could use a Sweep and Prune approach to reduce computation effort. If your game is simple and has not too many actors you can skip that though.

A simple implementation would look something like this:

``````class CollisionDetector:
void Update():
for gameobject in world:
for otherGameobject in world:
if gameobject == otherGameobject: continue

collision = GetCollision(gameobject, otherGameobject)
if collision:
gameobject.Collide(collision)
otherGameobject.Collide(collision)
``````

of course the different collision detection approaches and the collision response need to depend on the colliding game objects.

-
Thanks, this will be very useful – Chompas Nov 8 '12 at 17:22

This will check each object against each other object once:

``````Object objects[];
for(i=0;i<objects.length;i++)
{
for(j=i+1;j<objects.length;j++)
{
//check for collision between objects[i] and objects[j]
}
}
``````
-
Thanks for your reply. My main question was about the architectural design of this. – Chompas Nov 8 '12 at 17:23

Well. It's a matter of figuring out if:

1. There is no chance that these two objects collided
2. You don't need to know.

For 1, it's mostly a matter of distance. You'll still need to compare the positions of two objects, but if they're far from each other, you can safely assume that they're not colliding, thus saving a few calculations.

2 depends on your game. Like, if you're only calculating collision between enemies in order to not render them on top of each other, then you probably don't care about collision between enemies that are outside the screen.

You may also be able to completely ignore certain groups of objects - like, if you know that an entire, distinguished group of enemies are in the opposite end of the game world, you could just ignore those until the player gets closer - maybe even leave them out of your update loop, because they're very far away.

-