Simple design question here:

In a Shoot'Em Up, where you often have to deal with hundreds of bullets / missiles / weird flying stuff, who's in charge of detecting collisions? The weapon or the ship?

As there is usualy less ships than bullets displayed on screen, I tend to believe that would be smarter if ships could be in charge of it. But is it really smarter?

  • 1
    \$\begingroup\$ It really depends on the technology you are using. If there are a large amount of objects, I personally prefer to use manager classes that house all subtypes of an object and handle things on a group level. \$\endgroup\$ – Benjamin Danger Johnson Sep 21 '14 at 7:59
  • \$\begingroup\$ @BenjaminDangerJohnson according to bcrist's answer I will probably do a manager, an external object, dedicated to collision checking. Thank you for your suggestion. \$\endgroup\$ – lvictorino Sep 22 '14 at 7:43

The simple answer here is that as the developer, you are in charge of collision detection. But "who is responsible" is not really an important question when it comes to collision detection. The important question is: "How many pairs of objects must be tested for collision each frame?"

Now, in a shmup most collisions take place between a bullet/munition and a player/enemy unit. Collisions between two munitions and collisions between two units are usually ignored. This means all we need to do is keep two lists of objects - one for units and one for munitions. In fact, you can probably split up objects into even more groups, reducing the number of potential collisions further. For instance, you could separate player munitions from enemy munitions and player units from enemy units, since player munitions will probably only ever cause damage to enemies, and enemy munitions will only cause damage to players.

With this system, there's probably no optimization required. Each object in one list can be tested against every object in the other. If we have N units and M munitions then we end up doing N*M collision checks. We may have a very large M, on the order of several hundred or more, but N is probably on the order of 3-6, perhaps up to a dozen.

If you're working on mobile, or you have so many objects that you're still having performance issues, or you need more flexibility to allow collisions between objects of the same type, then chances are you'll want to use a broad-phase algorithm, utilizing a space partitioning data structure to reduce the set of potential collision-pairs before doing the actual collision-detection. You can reinvent the wheel yourself here if you like, but a far less painful approach is to simply use a physics library like Box2D or Bullet and let it take care of the collision detection for you.

  • \$\begingroup\$ Reassuring to know that performance won't be affected event without no further optimizations. Thank you for your answer. \$\endgroup\$ – lvictorino Sep 22 '14 at 7:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.