Jump to content
  • Advertisement

Archived

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

JIMbond21

Particle trail effect???

This topic is 5206 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

Anyone know how to go about making a particle have a fading tail or trail like in a real fire work or for like for sparks? I know a few ways I could do this but the ones im thinking of are very costly, does anyone know of an efficient way to do this?

Share this post


Link to post
Share on other sites
Advertisement
Simply set the alpha of the particle as a function of the particles lifetime. i.e. particle_alpha = max_alpha - (particle_lifetime * factor). If your alpha values range from 0 to 1 and particle_lifetime was in seconds and you want a particle to fade out over 5 seconds factor would be 1/5 and max_alpha would be 1.

[edit]Just realised I read your question wrong , Oluseyi provides the solution[/edit]

[edited by - Monder on April 14, 2004 1:31:11 PM]

Share this post


Link to post
Share on other sites
The trail of a particle is a given number of its previous positions. So simply store a variable number of previous positions.

You can have a fixed-length trail, but that tends to look rather generic though it''s the easiest to implement:

// assume that next_position(p) returns a clone of the particle in its next position,

// with all state data updated


list<Particle> trail;
...

while( !trail.empty() )
{
if( !<terminal condition> )
trail.push_front( next_position(trail.front()) );
trail.pop_back();
}

This will cause the particle trail to evaporate when <terminal condition> is set.

The alternative method is to eliminate trailing particles at different intervals, indicated by the method Particle::done(). The effect is more non-deterministic trails, which look nicer, and it doesn''t take that much more work.


// assume that next_position(p) returns a clone of the particle in its next position,

// with all state data updated


list<Particle> trail;Particle * p;
...

while( !trail.empty() )
{
if( !<terminal condition> )
trail.push_front( next_position(trail.front()) );
p = &(trail.back());
if( p->done() )
trail.pop_back();
}

Share this post


Link to post
Share on other sites
like this?



What i do in this older screenshot is i have a particle with a trail of some number of particles. If i hade, say, 10 pseudo-particles in the trail, i

1) get the color of the underlying pixel

2) get the color of the actual particle i'm drawing

3) mathmatically merge the two and draw the pixel

each particle in the trail gets it position by offsetting the actual particle's time a wee little bit (how much suject to testing). I loop through 0-10 and place each pseudoparticle in the trail a little bit before the last one, and i use the position in the number of particles in the trail to effect the alpha. For instance, the first particle at the head would have full opacity. The last one would have full transparacy and the one in the middle 50%.

here's my cut and paste code for drawing one of my trailing particles:


bool Particle::Poll( int time ) {

//LockSurface(screen);

gamespace->LockGameSurface();

// foreach of the last X pixels in the flow of time

for (int n=0; n < fade; n++) {

// get time and number of pixels to add since the last time

float time_from_birth = time - birth_tick - (interval*n);
int pixels = int( (int)speed * (int)time_from_birth ) / 1000;

// am i dead?

if ( time_from_birth > ls ) {
if ( n == fade-1 ) { return false; } else { continue; }
}


//std::cout << "Step " << n << ": time from birth = " << time_from_birth << ", pixels = " << pixels << "\n";


if (pixels < 0) { break; }

// basic right-angle trig

//int tempangle = angle % 90;

int xoff = int( cos( DegToRad(angle) ) * pixels );
int yoff = int( sin( DegToRad(angle) ) * pixels );

// adjust for "gravity"

yoff += int ( pow( (float)weight, time_from_birth/1000.0 ) );
int x = startx + xoff;
int y = starty + yoff;

// OOB

if ( xpos < 1 || ypos < 1 || xpos > gamespace->SpaceWidth()-2 || ypos > gamespace->SpaceHeight()-2 ) {
// if the last psuedo-particle on the tail is OOB, stop monitering the particle:

//if ( n == fade-1 ) { return false; } else { continue; }

continue;
}

// int xo[] = {-1,0,0,0,1};

// int yo[] = {0,-1,0,1,0};

// for (int i=0; i < 5; i++) {

// // draw

// float ratio = ( ((float)fade-(float)n) / (float)fade );

// SDL_Color c = GetPixel(screen, x+xo[i], y+yo[i]);

// int R = c.r + int( (color.r - c.r) * ratio );

// int G = c.g + int( (color.g - c.g) * ratio );

// int B = c.b + int( (color.b - c.b) * ratio );

// DrawPixel( screen, x+xo[i], y+yo[i], R, G, B);

// }


// draw

float ratio = ( ((float)fade-(float)n) / (float)fade );
//SDL_Color c = GetPixel(screen, x, y);

SDL_Color c = gamespace->GetPixelFromSpace(x, y);
int R = c.r + int( (color.r - c.r) * ratio );
int G = c.g + int( (color.g - c.g) * ratio );
int B = c.b + int( (color.b - c.b) * ratio );
//DrawPixel( screen, x, y, R, G, B);

gamespace->DrawPixelToSpace(x, y, R, G, B);

}

//UnlockSurface(screen);

gamespace->UnlockGameSurface();

return true;
}



and if you want a fun demo, try this:

particle demo



[edited by - leiavoia on April 15, 2004 11:52:55 AM]

Share this post


Link to post
Share on other sites
When I need particle trails, I simply use a moving emitter that spawns stationary particles. As the emitter moves, it drops behind it a trail of particles like breadcrumbs. The particles are given a random life-time value within a range, so they disappear in non-uniform pattern but with regularity. By varying the average life length I can shorten or lengthen the trail. For things such as smoke, I might give the particles just the slightest hint of an upward velocity, perhaps with very small divergent velocities and a varying alpha component, to model the effect of a smoketrail rising and slowly dissipating.


Golem
Blender--The Gimp--Python--Lua--SDL
Nethack--Crawl--ADOM--Angband--Dungeondweller

Share this post


Link to post
Share on other sites
leaving behind trails only works with lower speed particles however. That's how i used to do it. It also requires that much more memory and management of the new particles.

For some of my fireworks, they really fly across the screen at about 1000-3000 pixels per second. They would not accurately drop trailer particles and it would look more like a rock skipping across a pond than a high speed spark. Having the particle itself be responsible for drawing it's own tail reduces the need to create more particles or storing memory for positions of those particles.

[edited by - leiavoia on April 15, 2004 9:02:52 PM]

Share this post


Link to post
Share on other sites

  • 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!