Sparks Animation

Started by
11 comments, last by GameDev.net 10 years, 11 months ago

I have bunches of lines that I want to animate for sparks effect.

In my particle engine, I have gravity which I set to 0.0f, 10.0f, 0.0f. (sparks will move up)

The sparks will be generated in random initial position.

In the particles Update() code:


currentParticle.position += gravity / elapsedTime;

I use the following to draw each spark line:


void RenderSingleLine(D3DXVECTOR3& lineFrom, D3DXVECTOR3& lineTo)
{
     // Code to draw spark line according to lineFrom and lineTo here...
}

How do I set lineFrom and lineTo according to the current spark position and gravity?

I want the lines to always point at the direction it's moving toward.

Advertisement

The question is what are you trying to accomplish. Something like the sparks from

">
? I was experimenting and adding features to the Irlicht renderer and added the particle rotation/stretch etc code to make the little sparks there. While those are actually stretched quads the same basic method of calculation works for lines. What you need is the particle velocity in order to figure out the length and the direction. While you can derive velocity from the information you post above, it will likely be unstable due to float rounding, so it is suggested to use real velocity stored with the particle. Anyway, here is the basic way to do it:

struct Particle
{
  Vector3f    position;
  Vector3f    velocity;
};
 
void Update()
{
   velocity += gravity * elapsedTime;
   position += velocity * elapsedTime;
}
 
RenderSingleLine(
  particle.position,  // Start of the particle is current location.
  particle.position - particle.velocity * length  // Trail end is at "length" based on velocity.
);

Now that gives you a fair approximation of a spark. Though, the tail sometimes ends up a bit odd. What you may be really desiring is a trail and not a tail, in which case you basically just record a list of past positions, drop intermediates which are within a given tolerance and draw a line list instead of a simple velocity tail.

If you expand on your desires, I'm sure folks can help out more..

When I set the gravity to 0.0f, 10.0f, 0.0f, the sparks will go up in a straight direction, how do I make the sparks spread when they go up?

Here is how I want the sparks to move:


 End point
\         /
 \       /
  \     /
   \   /
    \ /
Start point

Oh... You sound like you need to get your concepts worked out a bit better. There are three parts to this:

Emission point: Needs to be a point and a full coordinate system, i.e, three normalized vectors at right angles to each other. We'll say the +Z axis is the direction you want to emit the particles.

Emission velocity: This is a scalar value of how fast the particle moves. We'll say it is 20.0.

Emission randomness: Another scalar value, we'll just use this to narrow the emission cone. We'll say .5 which is basically saying 45 degree emission angle max.

Gravity: Given the above, we'll say gravity is {0, -10, 0}.

Put this all together using crappy C rand:

particle.position = emission.point;

Vector3f offset = Normalize( emission.x * (rand()/float( RAND_MAX )*2.0f-1.0f) + emission.y * (rand()/float( RAND_MAX )*2.0f-1.0f) );

particle.velocity = Normalize( emission.z+offset*emission.randomness )*emission.velocity;

Now, using velocity as I did in the prior reply during you update and the mentioned negative gravity, most of your particles should emit in random directions up to 45 degree's around the emission vector and then curve down with gravity over time.

I'm trying to convert your code to D3DX*

But having some problems

The following is a float value? How could I put it in D3DXVECTOR3?


emission.x * (rand()/float( RAND_MAX )*2.0f-1.0f) + emission.y * (rand()/float( RAND_MAX )*2.0f-1.0f)

Same apply here:


particle.velocity = Normalize( emission.z+offset*emission.randomness )*emission.velocity;

"emission.z+offset*emission.randomness" is a float value as well, how do you normalize it and make it D3DXVECTOR3?

Been a while since I've done D3DX stuff, sorry. Let me see if I can explain it well enough so you can convert it:

emission.x would be the "X" value of the coordinate system around your emission point. It is a Vector3.

You scale, or set the length/magnitude, of the x vector to the random value between -1 and +1.

So, the result should be a vector3 again.

Do the same to the y coordinate axis and add that to the result of the x scaling.

You end up with a randomized vector3 in the x/y plane.

The final item is normalize the emission direction (+z) plus the offset vector which is scaled by the randomness value. Finally you scale the result by the desired velocity.

NOTE: Perhaps this will help, nothing other than scale, add or normalize are in use within this.

I'm confused.

emission is a Vector3, emission.z is a float.

If I do the following, the program will not compile:

D3DXVec3Normalize(emission.z+offset*emission.randomness);

However, I tried doing it using the way I have in my engine, it could be the same way as your way:


void generateNewParticle()
{
    // Code to generate a new particle here...
    D3DXVECTOR3 randVec = GetRandomVec();
    particle.velocity += randVec * 4.0f;
}


D3DXVECTOR3 GetRandomVec()
{
    D3DXVECTOR3 vVector;
    vVector.z = getRandMinMax( -1.0f, 1.0f );
    float radius = (float)sqrt(1 - vVector.z * vVector.z);
    float t = getRandMinMax( -D3DX_PI, D3DX_PI );
    vVector.x = (float)cosf(t) * radius;
    vVector.y = (float)sinf(t) * radius;
    return vVector;
}

Althought it works, I have slight problems at the particles generation point, the particles are not looking at the moving direction when generated:

[attachment=15766:problem.png]

Up. Any idea how can I make the sparks look at the moving direction when generated so I get rid of the parts around the circles?

Sorry but i don't know the answer to your question. Although you could cover that up with smoke maybe? It doesn't look that bad.

I just wanted to say that i really like how 3rd image looks like. Congrats. biggrin.png

Could you possibly make a video so i can see it in action and moving?

@belfegor: Well, your help is one of the main reasons that made me able to accomplish that smile.png I'm still doing some improvement so I could upload a video when I finish.

Everyone,

In this screenshot, the sparks looks messed up, because it's getting generated pointing at random direction, then when it move it point at the UP direction (correct direction), this problem is much more noticeable when the particles are moving:

(I think the problem is be more clear in this screenshot)

[attachment=15782:5644.png]

I'm looking for a way to modify the code to make it look at the moving direction when generated.

So my question is: How do I make the particles generate while looking at the gravity direction?

Here is how I generate the particles (I need to update the following code to fix this problem):


D3DXVECTOR3 randVec = getRandVector();
particle.velocity += randVec * 4.0f; // 4.0f = How much spread

Here is how I render the sparks lines:


               // D3DXVECTOR3 lineFrom      D3DXVECTOR3 lineTo
RenderSingleLine( currentParticle.position, currentParticle.position - currentParticle.velocity * lineLength );

This topic is closed to new replies.

Advertisement