Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

Sparks Animation

Started by Medo Mex 11 replies 2.2k views
Original Post
Medo Mex
Medo Mex

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.

Medo Mex
Medo Mex

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
Medo Mex
Medo Mex

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?

Medo Mex
Medo Mex

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]

Medo Mex
Medo Mex

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?

belfegor
belfegor

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?

Medo Mex
Medo Mex

@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 );
Medo Mex
Medo Mex

Okay, I figured one step towards solving this problem:

How do I modify the following code to make it generate a random vector looking at or close the direction of gravity?

Basically, I want the particle random vector to be limited to the half circle of the direction of the gravity

I think the line following line should be modified:

float t = getRandMinMax( -D3DX_PI, D3DX_PI ); // I want to limit this circle

D3DXVECTOR3 getRandVector(D3DXVECTOR3 gravity)
{
D3DXVECTOR3 vVector;
// Pick a random Z between -1.0f and 1.0f.
vVector.z = GetRandomMinMax( -1.0f, 1.0f );
// Get radius of this circle
float radius = (float)sqrt(1 - vVector.z * vVector.z);
// Pick a random point on a circle.
float t = getRandMinMax( -D3DX_PI, D3DX_PI );
// Compute matching X and Y for our Z.
vVector.x = (float)cosf(t) * radius;
vVector.y = (float)sinf(t) * radius;
return vVector;
}
[attachment=15792:gr.png]
Medo Mex
Medo Mex

Up.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.