# Particles Animation

## Recommended Posts

I have two questions regarding particles animation:

1. What is the best way for animating particles? Should I use timeline and multiple key frames to change the values (alpha/size/velocity/etc...)
Is there is any other good suggestions for animating particles?

2. How do I make the particles totally frame independent?
I'm multiplying the velocity with the elapsed time, but I notice that if the frame delay, the particles generation DELAY, which is a real problem.

##### Share on other sites

Rather than updating velocity/etc per-frame and based on elapsed frametime, it's easier if you just store the original position/velocity/etc that the particle was first generated with, then calculate a new position as required every frame based on these factors and the amount of time that's elapsed since the particle was generated.  The equation is simple enough, can run on the CPU or in a vertex shader, and looks something like this:

NewPosition = OriginalPosition + (Velocity + (Acceleration + Gravity) * ElapsedTime) * ElapsedTime;


That will give you framerate-independent particle movement.  Of course, if you're doing anything other than basic physics with them, it may not be suitable.

Edited by mhagain

##### Share on other sites

@mhagain: The problem is that if the frame delayed, the particles generation get delayed, so when it generate again after the delay, it appears like the smoke stopped and started again.

##### Share on other sites

2. How do I make the particles totally frame independent?

I'm multiplying the velocity with the elapsed time, but I notice that if the frame delay, the particles generation DELAY, which is a real problem.

Specify the emission rate in particles per second and pass your time step to the emitter; it is then the emitters job to spawn the correct number of particles and initialize them with proper starting points.  Something like this:

void SpawnParticles(float          a_fPPS,        // Particles to emit per second
const Vector3& a_vPosition,   // Position to emit from
const Vector3& a_vVelocity,   // Direction and speed of emission
float          a_fDeltaTime)  // Time span to emit particles
{
float32 fNumParticles = a_fPPS * a_fDeltaTime + s_fRemParticles;
int32   nNumParticles = (int32)(fNumParticles + 0.5f);

for(int32 i = 0; i < nNumParticles; i++)
{
float32 fTimeOffset = a_fDeltaTime * (float)i / (float)nNumParticles;
Vector3 vPosition   = a_vPosition + a_vVelocity * fTimeOffset;

ParticleSystem::SpawnParticle(vPosition, a_vVelocity);
}

return;
}


##### Share on other sites

@nonoptimalrobot: Even I tried that, but the problem is:

Let say the particles regularly spawn 3 particles each 100 milliseconds, If the frame delayed for sometime it will spawn for example 60 particles (due to frame delay)

Now, the frame delay caused the old particles to move and leave space, when the particles spawn again you will see noticeable space between the old particles and the new spawned particles.

Another question that I didn't get it's answer:

What is the best way to animate particles? should I use time/keyframes? Any other good methods?

Edited by Medo3337

##### Share on other sites

Now, the frame delay caused the old particles to move and leave space, when the particles spawn again you will see noticeable space between the old particles and the new spawned particles.

You don't need to spawn all particles at the same location. It's easy to figure out the new position of a particle spawned last frame, and then interpolate the spawn positions of your new particles this frame from the actual new position to the last frame particle position, giving you a smooth transition.

(so if you spawned particle A last frame, which has now moved, and this frame you are spawning particles B,C and D, you can make B at the original spawn position, C a bit closer to A than B, and D even closer to A.)

##### Share on other sites

@nonoptimalrobot: Even I tried that, but the problem is:

Let say the particles regularly spawn 3 particles each 100 milliseconds, If the frame delayed for sometime it will spawn for example 60 particles (due to frame delay)

Now, the frame delay caused the old particles to move and leave space, when the particles spawn again you will see noticeable space between the old particles and the new spawned particles.

That code takes care of that by adjust the particles spawn position with this bit:

float32 fTimeOffset = a_fDeltaTime * (float)i / (float)nNumParticles;
Vector3 vPosition   = a_vPosition + a_vVelocity * fTimeOffset;


vPosition is the particle position had it been integrated forward in time across a partial frame.  You'll have to replace the second line with your actual particle update code.  If you are having problems because the emitter is moving too then adjust the spawn position to account for the emitter's position between frames as well:

void SpawnParticles(float          a_fPPS,        // Particles to emit per second
const Vector3& a_vPosition,   // Position to emit from
const Vector3& a_vEmitterVel, // Velocity of the emitter
const Vector3& a_vVelocity,   // Direction and speed of emission
float          a_fDeltaTime)  // Time span to emit particles
{
float32 fNumParticles = a_fPPS * a_fDeltaTime + s_fRemParticles;
int32   nNumParticles = (int32)(fNumParticles + 0.5f);

for(int32 i = 0; i < nNumParticles; i++)
{
float32 fTimeOffset = a_fDeltaTime * (float)i / (float)nNumParticles;

// Replace this with particle time integration code
Vector3 vPosition   = a_vPosition + a_vVelocity * fTimeOffset;/

// Replace this with emitter position time integration code
vPosition += a_vEmitterVel * fTimeOffset;

ParticleSystem::SpawnParticle(vPosition, a_vVelocity);
}
return;
}

Edited by nonoptimalrobot

##### Share on other sites

I'll checkout this solution.

I'm also looking for the best way to animate particles, should I use timeline/keyframes? Any suggestions?

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Forum Statistics

• Total Topics
628379
• Total Posts
2982344

• 10
• 9
• 15
• 24
• 11