Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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)
share|improve this question

2 Answers 2

up vote 8 down vote accepted

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.

share|improve this answer
    
Of course, going with the factory solution is identical to the second option, just using a more buzzword-compliant name. :) –  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!

share|improve this answer
    
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. –  Asher Einhorn Jan 16 '13 at 16:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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