How to do (2D) Particle Systems?

Moderator
Posts: 508
Joined: 2002.09
Post: #1
Hi,

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..."
jamie
Unregistered

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.

http://65.189.191.173/basicallyreal/html/examples.html

The link on that site is for "Particles Yum"

jamie
furballphat
Unregistered

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:

Code:
```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.
Moderator
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
iollmann
Unregistered

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.
Member
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
Unregistered

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
Code:
```while (particleSystem) {     RenderParticleSystem(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!
Member
Posts: 104
Joined: 2002.04
Post: #8
w_reade, this would make a good tutorial if you wanted to tackle it.