How to do (2D) Particle Systems?

Posts: 509
Joined: 2002.09
Post: #1

I was wondering how to do write a Particle System engine for a game. I wanna keep it simple by making a 2D engine if it's possible because my 3D knowledge is very slim. Also my maths are good enough but they are certainly not that advanced.

Any help would be welcome..

"When you dream, there are no rules..."
Quote this message in a reply
Post: #2
Here is a link to a 2D particle system for TNT Basic. There are examples and the source code I think so you might be able to learn something from checking it out.

The link on that site is for "Particles Yum"

Quote this message in a reply
Post: #3
Basically, you want to have a big array of particles (which can be structures, objects, etc.) Every frame you loop through all these particles and do something to do them like move them or change their colour.

A particle system to create 360 particles flying out in a circle, fading from red to black:

structure particle
    intger x, y
    integer angle
    color colour
end structure

function init
    set every particle x to 0
    set every particle y to 0
    set every particle colour to red
    repeat 360 times with i
        set particle i angle to i
    end repeat
end init

function iterate
    set every particle x to cos(angle) * current frame
    set every particle y to sin(angle) * current frame
    set every particle colour to red - current frame
end iterate

That probably isn't very clear, but it should get across the basic idea of a particle system, make a very simple particle and then make lots of them.
Quote this message in a reply
Posts: 522
Joined: 2002.04
Post: #4
Check out how SpriteWorld does it. It works well. I am using a particle system based on theirs for a star field complete with parallax, and a separate one for misc effects like exhaust and debris dust. It is pretty quick, and with a little work you can get it extremely quick like I did. Or at least it will give you an idea on how to accomplish your own brew. Good luck!

-Jon Czeck
Quote this message in a reply
Post: #5
The main problem with particle systems is that they get dreadful cache behavior. This is because by design they skip all over memory. This is why a particle system with a few hundred particles can be more expensive than a sprite with a few thousand pixels. In general, you will find them to be cheaper if you keep the particles in a fairly small confined space.
Quote this message in a reply
Posts: 156
Joined: 2002.10
Post: #6
I don't see why the system should "skip all over memory" - can't you just allocate all the particle structs/objects in an array (not an array of pointers) and then they will all be stored contiguously in memory?

- Iain
Quote this message in a reply
Post: #7
I think I downloaded a particle system tutorial (from here? - if it was I can't find it) a little while ago, which explained that you could improve the situation by allocating a particle system (array of similar particles, with perhaps a bit of extra data) each time you need some particles, and then only deleting that system when all the particles in that system are finished with. It cuts down on time taken allocating/deallocating space, probably helps avoid memory fragmentation, and if you just have a linked list of particle systems you can surely do something like
while (particleSystem)
    particleSystem = particleSystem->next;
which means you only jump around once per particle system, rather than potentially once per particle. Which is good.

And, of course, if you make it so that all particles in a system are basically identical, you can store the data for what it looks like, and then speed through drawing all the particles in one system, just getting each one's position/orientation and possibly age if anything depends on that.

I wish I could remember where I found that tutorial!
Quote this message in a reply
Posts: 104
Joined: 2002.04
Post: #8
w_reade, this would make a good tutorial if you wanted to tackle it.
Quote this message in a reply
Post: #9
I've got a uDG entry to finish, but it's so flattering to be asked that I'll try to have a go Grin. Don't expect it for at least a month though!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  osg::Particle how to change de texture of each particle zinbawe 1 4,217 Jul 14, 2009 12:46 AM
Last Post: DoG
  Particle groups unknown 4 5,085 Nov 4, 2005 10:27 AM
Last Post: Zekaric
  Particle Engines Nick 18 11,056 Sep 8, 2005 12:24 PM
Last Post: TomorrowPlusX
  Quaternions, coordinate systems, etc... Jesse 4 6,915 Jul 14, 2003 08:31 AM
Last Post: DoG