Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] 2D Particles

Author
Message
AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 28th Mar 2009 23:26 Edited at: 28th Mar 2009 23:48
This is a collection about 40 functions you can use to create and manage 2D particle systems. I was bored today and I have several 2D games in mind I'd like to get started on, so I took the last couple hours to get this done.

Here's the include file:



Here's a rundown of the properties of the particle systems, however I'm pretty bad at explaining things so if you have any questions please ask:

Texture: The emitter's texture is the image used for its particles.
Delay: The emitters' delay is the amount of time between particle emissions, i.e. if you set it to 15, it will emit a particle every 15 ms, or 60 times per second.
Life: The length of time in ms that the each particle of the emitter will be alive/active.
Gravity: The amount of acceleration applied to the emitter's particles.
Initial Velocity: The initial velocity of the emitter's particles.
Number of Particles: The number of particles emitted each time an emission takes place. That is, if you set 'Delay' to 15, the emitter will emit particles every 15 ms, and if you set Number of Particles to 5, it will emit 5 particles every 15 ms.
Color: The emitter has two colors: start color and end color. The start color is the color of the particles when they're first emitted, and the end color is the color of the particle at the end of its life. If you set start color to red and the end to blue, it will slowly fade from red to blue throughout the particle's life.
Alpha: The alpha works the same way as the color, you set a start alpha and an end alpha and each particle fades from the start alpha to the end alpha.
Scale: The scale also works the same way as the color, you specify a start scale and an end scale, and the particle will slowly grow/shrink.
Spin: The amount that each particle is rotated each frame. This works the same way as the scale, you set a start spin and an end spin, and the spinning will speed up/slow down during the particle's life.
Emitter Angle: The emitter's angle has two properties: minimum angle, and maximum angle. The angle decides the angle at which particles are emitted. If you set the start angle to 90.0 and the end angle to 180.0, then each particle will be emitted at a random angle between 90 and 180. You could set both the start and end angles to the same value to have every particle emitted at the same angle.

I will post the functions with explanations if anybody requests it, but for now you can take a look at the '2D Particles' example below:

[Demo]

which includes the example source. Basically you call InitParticles() before any other particle functions. Then you call UpdateParticles() in your main loop, however you'll notice it takes a function 'TimeStep'. This is the amount of time your main loop takes. You can do this by either timing the main loop, like you do for timer-based movement, or you can lock your framerate at 60 and set it to 15. To make a particle emitter you use MakeParticleEmitter(), the parameters are explained in the code, and to set other properties you can use the other various functions, most of which should also be explained in the code.

If you have any suggestions or questions don't hesitate to ask.

GIDustin
15
Years of Service
User Offline
Joined: 30th May 2008
Location:
Posted: 29th Mar 2009 00:51
Nice job! The demo looks really good, and the code looks easy to replicate. I will keep this around for a future RPG I plan to make.
AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 29th Mar 2009 00:55
Thanks! Ya, I tried to keep the code pretty simple. Hopefully I'll be able to get a particle system editor started soon, making it even easier to add this to you projects.

Sixty Squares
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 29th Mar 2009 19:16
Wow great job! This looks pretty nice . I'll have to consider it if I make a 2D game at any point.

<---Spell casting battle game!
AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 29th Mar 2009 23:35

Login to post a reply

Server time is: 2024-05-18 18:12:58
Your offset time is: 2024-05-18 18:12:58