title: Managing collision detection in games
body: <p>So my question isn't about the how-to of collision detection, but is more of a broad 'what code should own collision detection'. I've written some game in the past(relatively simple 2D Flash games) and it got me thinking of which code should own collision detection?</p>
  
  <p>Let me clarify - say in a game I have a group of enemies and a group of projectiles the player has fired. So in the past I've had say an EnemyManager class, that every frame updates the positions of the enemies and likewise for the player projectiles had a PlayerProjectilesManager class that moved around the bullets. That's cool - everything is fine and dandy. But then, I decide I want the bullets to <em>affect</em> the enemies(crazy I know!). So that means somewhere in the code I need to:  </p>
  
    <ol><li>Figure out which bullets and enemies are colliding(I don't care how for this question)</li><li>Figure out the response to each collision </li></ol>
  
  <p>So basically what I've done in the past is just have the EnemyManager class take 'ownership' of the collisions, and during its update loop it finds the player bullets that collide with enemy bullets(i.e. Step 1) and also calls code for both objects to handle the collision (e.g. the enemy losses health, the bullet disappears) (i.e. Step 2). So I've given control of the collision detection and collision 'reaction' to the EnemyManager. </p>
  
  <p>A couple comments about that:</p>
  
  <ul><li>It feels vary arbitrary to me that the EnemyManager is in 'control' instead of the PlayerProjectilesManager</li>
  <li>Both collision detection and collision 'reaction' are handled by the same owner, this isn't a requirement from my point of view</li>
  </ul>
  
  <p>What's forming in my mind is a 3rd party entity managing collision detection. For instance have a CollisionManager which has code that know's which other Managers need to have collisions detected. That leads to other questions like what interfaces do the 'Managers' need to expose for efficient collision detection without exposing too many innards to the CollisionManager. Then I suppose the CollisionManager what broadcast some sort of an event, containing which 2 objects collided etc... and perhaps the EnemyManager/PlayerProjectilesManager could separately listen for these events and react accordingly and separately. Starting to make sense in my mind. :)</p>
  
  <p>Thoughts? Almost every game has collision detection, so I'm sure this has been discussed before. :)</p>