What would be better:

  1. An explosion class which gets created when needed, does it's stuff, then is deleted. (one instance per explosion)
  2. An explosion class which persists through the whole game, and creates a new explosion in the appropriate place when needed. (one instance per game)

Probably the first option, as the second option implies you can only have a single explosion in any given frame. Unless you're talking about having a factory produce the explosion objects in the second option, in which case the two options are not mutually exclusive and you probably should, in fact, do both.

Regardless, the only downside to the first option (at this rough level of abstraction) is the cost of allocation churn. This can be alleviated using an allocation pool for your explosion object instances (for example, in the factory that produces explosions), which will will allow you to have the logically correct model of explosions (that is, there can be multiple explosions and each explosion has an associated object instance) without incurring the overhead of frequent allocations during runtime.

  • \$\begingroup\$ Of course, going with the factory solution is identical to the second option, just using a more buzzword-compliant name. :) \$\endgroup\$ – Trevor Powell Dec 6 '11 at 3:00

There is a game programming pattern that I think can be applied to your particle/explosion system. Put simply, you maintain a list of explosion classes allocated at startup, and when you want an explosion to occur you pick an unused explosion from the pool of classes and give it back when it's finished displaying.

This makes showing explosions fast and avoids memory fragmentation (which you want to avoid if you're writing for a console). You might not display explosions as often as particles as the pattern is intended for (well, depends on your game, Explosion Simulator II?) but it may be helpful.

Here is the article documenting the pattern, complete with example code, by Robert Nystrom:

Game Programming Patterns - Object Pool

I suggest you check his other work in progress articles too, they're great reads!

  • \$\begingroup\$ this is a really great solution, especially when you're using something like C# that lacks the ability to create memory pools in the way C++ does it. Of course, you do still need to account for the performance hit you will take to render the explosions. \$\endgroup\$ – SirYakalot Jan 16 '13 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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