Archived

This topic is now archived and is closed to further replies.

JIMbond21

Particles System Question???

Recommended Posts

JIMbond21    122
I''m making a particle system for use in 2d games, is it better to implement a linked list when making a particle system or an array? Same question for a 3d particle system also?

Share this post


Link to post
Share on other sites
leiavoia    960
not necessarilly the best way, but the way i did it:

I have a Particle class, and a ParticleSource class (aka "explosion"). Both Particle and ParticleSource inherit from PollingObject which has a virtual function:

bool Poll( int time );

I then have a PollingEngine that keeps all polling objects in order (in an STL std::list). Every trip through the game loop, it calls Poll(time) on every object, and every object updates itself according to it''s own rules.

The engine Poll()s the ParticleSources, in turn each ParticleSource Poll()s each particle it is personally keeping track of.

The Polling Engine is the key concept here. It can handle much more than just particles though, also absolutely anything that needs regular updates.

Share this post


Link to post
Share on other sites
ZeroBit    122
Both Array and linked list can do, i think. Just make sure you pre-cache the particles instead of dynamically allocating it everytime it spawns / die.

Share this post


Link to post
Share on other sites
lonesock    807
The opinions expressed in this post are not necessarily those of the author.

Well, if you want a fixed number (capped, I should say) of particles, then I would recommend an array, as that would be much easier.

If you want a variable number, then a linked list is a good idea, but I would have each node in the list hold more that 1 particle. So maybe each extra node would have 1000 particles? Reduces the time spent traversing the list, and the overhead of adding / deleting nodes.

lonesock

Piranha are people too.

Share this post


Link to post
Share on other sites
leiavoia    960
for speed, yes: allocate everything first, then recycle them. For my particular system though, that doesn''t work because i treat all of my particles differently. They all have different colors, sizes, speeds, fade rates, trail lengths, etc. I create them on the fly with random values and let them just get popped off the stack when they die. I also have different particles involved for different things. I have about 10 different particle explosions now. Each one takes a random number particles, creates them all, then animates each one until it dies after which the explosion itself dies. However, i do not specifically use NEW or DELETE.

Share this post


Link to post
Share on other sites
Structural    328
quote:
Original post by leiavoia
for speed, yes: allocate everything first, then recycle them. For my particular system though, that doesn''t work because i treat all of my particles differently. They all have different colors, sizes, speeds, fade rates, trail lengths, etc. I create them on the fly with random values and let them just get popped off the stack when they die. I also have different particles involved for different things. I have about 10 different particle explosions now. Each one takes a random number particles, creates them all, then animates each one until it dies after which the explosion itself dies. However, i do not specifically use NEW or DELETE.


Why wouldn''t a static array work for dynamic particles?
Have an array with a realistic (or an absurd) number or maximum particles. Every loop, update the position of the particle or flag is as "dead". When you need a new particle, loop through your array and revive the first dead particle you come across.

Alternatively, what i haven''t tried, you could store the particles vertices in a seperate array, and draw them with one call of glDrawArrays (or the DX equivalent). Dead particles would be made invisible by having an alpha of 0 or three points at 0,0,0. I''m not sure if you use a 3D library for your 2D games, so this might not apply to you.

Share this post


Link to post
Share on other sites
oliii    2196
I''d personnally use an array, it''s less memory and less hassle. When deleting a particle, copy the last particle of the list in its place, and that ought to do it.


int AddParticle(const CParticle &xParticle)
{
m_axParticle[m_iNumParticles] = xParticle;
return m_iNumParticles++;
}

void DeleteParticle(int i)
{
m_axParticle[i] = m_axParticle[--m_iNumParticles];
}


as long as you don''t reference particles somewhere else, which you shouldn''t be necessary anyway.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Why not have an array and then when a particle dies you move the last "living" particle to the position of the one that died and decrease the "particle counter" by one?

Share this post


Link to post
Share on other sites