How to do (2D) Particle Systems?
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..
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..."
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
http://65.189.191.173/basicallyreal/html/examples.html
The link on that site is for "Particles Yum"
jamie
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:
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.
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 iterateThat 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.
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
-Jon Czeck
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.
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
- Iain
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
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!
Code:
while (particleSystem)
{
RenderParticleSystem(particleSystem);
particleSystem = particleSystem->next;
}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!
w_reade, this would make a good tutorial if you wanted to tackle it.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| osg::Particle how to change de texture of each particle | zinbawe | 1 | 2,611 |
Jul 14, 2009 12:46 AM Last Post: DoG |
|
| Quaternions, coordinate systems, etc... | Jesse | 4 | 3,962 |
Jul 14, 2003 08:31 AM Last Post: DoG |
|

. Don't expect it for at least a month though!
