Particles System Question???
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?
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.
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.
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.
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.
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.
So is it better ot have a fixed preallocated particle system or a dynamically allocated system?
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.
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.
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.
as long as you don''t reference particles somewhere else, which you shouldn''t be necessary anyway.
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.
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?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement