Jump to content
  • Advertisement
Sign in to follow this  
grayrobertos

Particle problems

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I using the bellow procedures to try and create an explosion effect on collision with another object. The actual particle code works fine, and works with the first object however the following objects simply show an already exploded explosion if you are with me. I am pretty sure the problem is with this bit of code simply not being reset, and have tried a number of solutions but none seem to work and just produce even more random effects.
g_particle[n].life-=delay*0.0060;	
		if (g_particle[n].life>0){
So can anyone think of a better way to create an explosion similar to the following code, ideally avoiding classes.
void init_particals(){
for (i=0 ; i<MAX_PARTICLES ; i++) {
		g_particle.active = true;
		g_particle.x=0;
		g_particle.y=0;
		g_particle.xi = float((rand()%800)-(rand()%800));
		g_particle.yi = float((rand()%600)-(rand()%600));	
		g_particle.life= (rand()%10);
		
	}
}

void particals(){
	for (n=0 ; n<MAX_PARTICLES ; n++) {						
		
		if (g_particle[n].active) {							
		glPushMatrix();
			glTranslated(g_particle[n].x,g_particle[n].y,0);
			DrawObject(4,4,1);
		glPopMatrix();
       		g_particle[n].life-=delay*0.0060;	
		if (g_particle[n].life>0){
			g_particle[n].x +=g_particle[n].xi/100;			
			g_particle[n].y +=g_particle[n].yi/100;		
		}
	
		}
	}

}

void pedestrians(){

	if (nocollision){
		movepedestrians();
		}
		else
		{
		glPushMatrix();		
			glTranslated(pedestrian.x,pedestrian.y,0);
			particals();
		glPopMatrix();
		}
}

Any help at all apreciated.

Share this post


Link to post
Share on other sites
Advertisement
There are a lot of issues with this code.

First, you only update the particles if there's been a collision that frame. When the collision's over, the particles will stop updating.

Second, you only initialize the particles once, so if there's a new collision, the particles aren't going to reset. You need to reinitialize the array every time there's a new collision.

Third, there's only one array of particles, so every explosion will look exactly the same. That's fine if there'll never be more than one simultaneous explosion, but otherwise, you need to add new particles for a new explosion.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ozymandias42
There are a lot of issues with this code.

First, you only update the particles if there's been a collision that frame. When the collision's over, the particles will stop updating.

Second, you only initialize the particles once, so if there's a new collision, the particles aren't going to reset. You need to reinitialize the array every time there's a new collision.

Third, there's only one array of particles, so every explosion will look exactly the same. That's fine if there'll never be more than one simultaneous explosion, but otherwise, you need to add new particles for a new explosion.


agreed on the lot of issues front, I just can't work out how to re-initialise it every time.

Share this post


Link to post
Share on other sites
Lots of issues indeed! I think help isn't what's needed here, it's a start-again job, at least as far as the code goes. And the idea is pretty much okay, so code is what needs doing.

I'm not an expert on particle systems, but I'd do something like (pseudo):
Vector<Particle> particles;

void game_loop(){
while(true){
// Must update the particles every frame!
foreach(Particle p in particles) p.update();
pedestrians(); // probably better as an OO update-each-object
render();
... etc
}
}

pedestrians(){
if(collision){
for(int i = 0; i < NUM_PARTICLES; i++){
particles.add(new Particle(particles, pedestrian.Location));
}
} else movepedestrians();
}

class Particle {
Particle(Vector<Particles> owner, Vector2D vec){
this.origin = vec;
this.owner = owner;
this.velocity = random_vector(-800, 800, -600, 600);
this.life = random(10);
}

update(){
life -= delay*0.006; // whatever that means
if(life < 0) owner.remove(this);
else location += timestep * velocity;
}
}


The essential things are: make sure you update the particles each frame; you need to create new particles when a collision happens; you need to destroy 'dead' particles. (Though the last two can be made a bit more efficient with a particle pool and reuse of objects, that's premature optimisation at this stage imho.)

Share this post


Link to post
Share on other sites
Quote:
Original post by grayrobertos
So can anyone think of a better way to create an explosion similar to the following code, ideally avoiding classes.


Ironically, most of the (many) things wrong with this code are things that object oriented programming makes easier. I'd suggest taking the time to become familiar with OOP.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!