Public Group

#### Archived

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

# Particle trail effect???

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

## 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 on other sites
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.

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

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

##### 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 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;

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 );

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

// 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

//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 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

##### 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]

1. 1
Rutin
44
2. 2
3. 3
4. 4
5. 5

• 9
• 9
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632983
• Total Posts
3009706
• ### Who's Online (See full list)

There are no registered users currently online

×