PDA

View Full Version : How to do (2D) Particle Systems?


Taxxodium
2002.10.07, 04:15 PM
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..

jamie
2002.10.07, 05:23 PM
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
2002.10.08, 05:31 PM
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.

aarku
2002.10.08, 09:16 PM
Check out how SpriteWorld (http://www.spriteworld.org) 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
2002.10.19, 02:22 PM
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.

IBethune
2002.10.19, 03:16 PM
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

w_reade
2002.10.20, 09:43 AM
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)
{
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!

GoodDoug
2002.10.21, 03:59 PM
w_reade, this would make a good tutorial if you wanted to tackle it.

w_reade
2002.10.21, 07:16 PM
I've got a uDG entry to finish, but it's so flattering to be asked that I'll try to have a go :D. Don't expect it for at least a month though!