Jump to content

  • Log In with Google      Sign In   
  • Create Account


Particle Engine


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 JustinDaniel   Members   -  Reputation: 137

Like
0Likes
Like

Posted 04 March 2012 - 09:34 PM

Hey guys!
I have been wondering, which is the best method for creating a particle system.
Structs or vector containers?
Im just curious because i tried both of them and on a test, My particle engine using structs can render 100000 particles per second, But when i get down to Vectors and iterate over every single particles i could not go over 5000 particles..
I have an Core- i5 processor and using vectors is taking much of my CPU usage.
Anybody ever tried a vector container for particle engines?

struct
{
	 float x;
	 float y;
	 .....
}particles;

//Either using a for loop to access them or vector containter and iterate over them..

Thanks!

EDIT:: Sorry if i am at the wrong forum. Move it if so.

Sponsor:

#2 dpadam450   Members   -  Reputation: 885

Like
0Likes
Like

Posted 04 March 2012 - 11:03 PM

Structs or vector containers?

Your term usage of "Struct" does not make sense. Explain what you mean. Vectors hold structs.

#3 Hodgman   Moderators   -  Reputation: 29400

Like
0Likes
Like

Posted 04 March 2012 - 11:32 PM

A std::vector and a raw array should both be exactly as fast as each other, if used correctly.

#4 JustinDaniel   Members   -  Reputation: 137

Like
0Likes
Like

Posted 05 March 2012 - 12:35 AM

Structs or vector containers?

Your term usage of "Struct" does not make sense. Explain what you mean. Vectors hold structs.

My bad, I meant an array of structs or A vector container.

A std::vector and a raw array should both be exactly as fast as each other, if used correctly

Really? Then i guess im using it wrong because i see a large amount of performance drop.

#5 SuperVGA   Members   -  Reputation: 1118

Like
0Likes
Like

Posted 05 March 2012 - 01:21 AM


A std::vector and a raw array should both be exactly as fast as each other, if used correctly

Really? Then i guess im using it wrong because i see a large amount of performance drop.

.reserve as much space as you assume is needed by the particle system early on, -avoid pushing new particles onto it when it's capacity has been reached.
Naturally, never .insert into the vector, and never .remove elements either as it readdresses the vector contents.
Maintain another vector of addresses to empty/unused particle indices, so you can easily resurrect a particle when it's needed at the emission point again.

Ex. [0][1][2][3][4]
particle on index 3 times out/"dies".
push_back on dead-particle-references already with capacity()==particle.size(): [ ][ ][ ][ ][ ]
[3][ ][ ][ ][ ]

#6 dpadam450   Members   -  Reputation: 885

Like
0Likes
Like

Posted 05 March 2012 - 10:47 AM

I would post your code.

#7 JustinDaniel   Members   -  Reputation: 137

Like
0Likes
Like

Posted 05 March 2012 - 11:07 AM

class Particle
{
	float x;
	float y;
	float z;
	.....
};

Particle particles;
//Either using a vector like
std::vector<Particle> particle;
const int Total_Particles = 100000;
for( int i = 0; i < Total_Particles; i++ )
	particle.push_back(particles);

//And then use an
for( std::vector<Particle>::iterator  iter = particles.begin(); iter != particles.end(); iter+= 1)
{
	iter->x = ...
	iter->y = ...
}

//OR using an array of structs
Particle particles[Total_Particles];

//And then using a for loop
for( int i = 0; i < Total_Particles; i++ )
{
	particle[i].x = ...;
	particle[i].y = ...;
}

//I find vectors are a bit slow, But i guess i am using them wrong
Thanks

#8 dpadam450   Members   -  Reputation: 885

Like
0Likes
Like

Posted 05 March 2012 - 12:02 PM

So this looks like just the initialization and not rendering. I would hope that you are not doing this each frame:
for( int i = 0; i < Total_Particles; i++ )
particle
.push_back(particles);

Also before that loop I posted, you want to do, as suggested, particle.reserve(Total_Particles). A vector IS an array, but it allows growth. So when the array fills up, it creates a new, bigger array, copies the old smaller array into the new bigger one, and allows you to add more elements.

#9 phantom   Moderators   -  Reputation: 7058

Like
0Likes
Like

Posted 05 March 2012 - 01:52 PM

//And then use an
for( std::vector<Particle>::iterator  iter = particles.begin(); iter != particles.end(); iter+= 1)
{
	iter->x = ...
	iter->y = ...
}
Thanks


Two comments on this;

1) As you are only iterating pull the 'end' condition out of the loop, that will save the call to 'end' every iteration
2) use '++iter' not 'iter += 1'

The first is likely to gain you more than the second but the second is much better style.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS