Public Group

# C++: Simple 2D Sprite Explosions

This topic is 3817 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm about to implement explosion effects when an enemy is destroyed in my current project (a Space Invaders clone) but first I want some advice on how to do it. For the animation itself, I'll need a timer, correct? An "Explosion" object with "int frame" or whatever that keeps track of the frame of animation and every 250 ms or so the frame is incremented and rendered based on the frame. That's pretty much the only way to do it, right? As for creating the actual Explosion objects... I was thinking about vectors. I have 55 enemies that are inside a static array of form: Enemy X [Y][Z]. Mostly because that's just how I'd done everything up to that point and I hadn't learned much about vectors. So obviously I COULD make an array of 55 explosions in the same way, or give every Enemy object a corresponding Explosion object but is this a very good example of where a vector would be useful? Obviously not every enemy can be exploding at the same time. The explosion might last 2 seconds and maybe in extreme circumstances you could get up to 7/8 displayed at once. Since I really don't know how many I need, is this the perfect place for a vector?

##### Share on other sites
One way to do it is as you described, just using an animated sprite. It is probably the easiest way of doing it, however it will produce explosions that looks exactly the same all the time (unless you use different animated sprites and randomize between them), which is perhaps not in the nature of explosions.

Another way to do it is to use a particle system. The idea is to have many small particles that together create the effect that you want. These particles can be randomly initiated (inside a certain boundary of course) making each explosion a bit different from the one before.
One of the advantage of a particle system is that it's possible to create a wide range of effects (like explosions, snow storms, water etc) with only small code changes.
If you happen to code in OpenGL there's an excellent tutorial on particle systems over at NeHe.

##### Share on other sites
Quote:
 Original post by Sean_SeanstonFor the animation itself, I'll need a timer, correct? An "Explosion" object with "int frame" or whatever that keeps track of the frame of animation and every 250 ms or so the frame is incremented and rendered based on the frame. That's pretty much the only way to do it, right?

That's what animations boil down to, yeah.

Quote:
 As for creating the actual Explosion objects... I was thinking about vectors. I have 55 enemies that are inside a static array of form: Enemy X [Y][Z]. Mostly because that's just how I'd done everything up to that point and I hadn't learned much about vectors. So obviously I COULD make an array of 55 explosions in the same way, or give every Enemy object a corresponding Explosion object but is this a very good example of where a vector would be useful?

This is where vectors are usefull, yes. You don't always have 55 enemies, right? That is, if you're a decent player, the number of enemies gradually decreases. Why should you keep those dead enemies around in your data structure? I'd use a vector for the enemies and one for the explosions. You add an explosion to your vector when an enemy dies, and whenever an explosion has ended, you remove it from your vector. Whether you have 5-10 explosions live at a single moment or 100-200 doesn't matter much to the vector. Why don't you just try it and see how it works out? :)

##### Share on other sites
Sorry I got a bit side tracked there. To answer your original question: Yes in order to create an sprite explosion just keep track of the time passed and then after a certain time update to the next frame. Roughly it could look something like.

expoFrames[30];currentFrame = 0;endExpo = false;animTime = 0.0f;...while(!endExpo){    Draw(expoFrame[currentFrame]);    if(TimeNow() - animTime >= animSpeed)    {        currentFrame++;            if(currentFrame >= 30)            endExpo = true;                animTime = TimeNow();    }}

Of course you won't want to implement the animation with a while loop like this, since it'll do nothing but render your animation. In your game it would probably be an if clause inside the main game loop somewhere.

Your idea to use a vector to store the information of the different explosions sounds fine, either that or a linked list. Just keep in mind that you don't need several instances of the sprite, just the variables needed to keep track of where in the animation the different explosions are (currentFrame, animTime, endExpo).

• 33
• 12
• 10
• 9
• 9
• ### Forum Statistics

• Total Topics
631352
• Total Posts
2999486
×