# Classes and particle engine

This topic is 3973 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So im trying to build a simple particle engine with a class for the emitter and a class for the particles. The way i want it to work is by declaring an emitter with x,y positions (2d game), lifetime, effect and amount of particles to use. Now the question i have is where to declare the particles, can i declare the particles within my particle emitter class and how do i go about that if i can. This is what im trying out right now, is this a proper way of declaring a dynamic array of objects within my emitter class?
class CParticleEmitter {
private:
float posX, posY;
int amount;
int effect, textureID;
bool active;
CParticle* part;
.
.
.


void CParticleEmitter::initializeEmitter(float x, float y, int amountPart, int time, int effect)
{
part = new CParticle[amountPart];
.
.
.



##### Share on other sites
Since you are using C++, I would highly recommend using the Standard Template Library (STL). It will make your job much easier when dealing with things like dynamic arrays and such, and it's "built in" so you won't have any messy libraries to download and configure.

In this case, using an std::vector would allow you to add and remove particles from your emitter with ease, instead of worrying about dynamic memory, freeing and recreating when you need to resize, etc.

In your ParticleEmitter class, I would put an std::vector<CParticle> particles member, instead of your dynamic array. Then, when you want to add a particle just do something like this:

CParticle newParticle;newParticle.X = 0;newParticle.Y = 10;...particles.push_back(newParticle);

##### Share on other sites
Great that will help alot and thanks for the quick answer.

##### Share on other sites
If you're going to be doing a lot of insertion / removal of particles from the vector, I'd suggest using the std::list instead, whose insertion and removal algorithms run in constant time. You're going to be iterating through all of the particles anyway, so random access isn't required.

Here's why I think the std::list is better suited for the particle engine...
Once the particle emitter expires, you don't want to remove all the particles right away - you usually want to have the particles continue one last cycle. Terminating the particle emitter will terminate all particles once their life-energy reaches zero. If you use a std::vector, you have two options: remove the particle on the spot, or use an if-condition in the render loop of the particle to check if its life-energy is zero (and if it is, don't render it). The first option is an expensive operation, and the second option forces you to check whether or not the particle has expired EVERY CYCLE (the time it takes for a simple if-condition to execute isn't great on its own, but it adds up once you have 1000+ particles).

With the std::list, removing the particle on the spot is done in constant time, so the second option isn't even worth considering in this case.

Just my two cents... Feel free to debate my methods - there is obviously more than one way to implement this.

##### Share on other sites
Quote:
 Original post by TheShadow344If you're going to be doing a lot of insertion / removal of particles from the vector, I'd suggest using the std::list instead, whose insertion and removal algorithms run in constant time. You're going to be iterating through all of the particles anyway, so random access isn't required.Here's why I think the std::list is better suited for the particle engine...Once the particle emitter expires, you don't want to remove all the particles right away - you usually want to have the particles continue one last cycle. Terminating the particle emitter will terminate all particles once their life-energy reaches zero. If you use a std::vector, you have two options: remove the particle on the spot, or use an if-condition in the render loop of the particle to check if its life-energy is zero (and if it is, don't render it). The first option is an expensive operation, and the second option forces you to check whether or not the particle has expired EVERY CYCLE (the time it takes for a simple if-condition to execute isn't great on its own, but it adds up once you have 1000+ particles).With the std::list, removing the particle on the spot is done in constant time, so the second option isn't even worth considering in this case.Just my two cents... Feel free to debate my methods - there is obviously more than one way to implement this.

Yeah it mentioned list in the link that ussnewjersey4 posted and about the if statement that is why i added the lifetime on the emitter instead of the particles so i only need to check if my emitter is active (since im not gonna do a whole lot of effects with the particle engine, mainly explosions which doesn't require alot of lifetime thus eliminating my need for individual particle lifetimes) and if it is continue to render the particles associated to that emitter, but i see your point.