• Create Account

Banner advertising on our site currently available from just \$5!

# Sparks Animation

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

12 replies to this topic

### #1Medo3337  Members   -  Reputation: 683

Like
0Likes
Like

Posted 16 May 2013 - 05:21 PM

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.

### #2AllEightUp  Moderators   -  Reputation: 4643

Like
1Likes
Like

Posted 16 May 2013 - 06:11 PM

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

### #3Medo3337  Members   -  Reputation: 683

Like
0Likes
Like

Posted 16 May 2013 - 06:23 PM

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


### #4AllEightUp  Moderators   -  Reputation: 4643

Like
0Likes
Like

Posted 16 May 2013 - 07:24 PM

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.

### #5Medo3337  Members   -  Reputation: 683

Like
0Likes
Like

Posted 16 May 2013 - 08:14 PM

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?

### #6AllEightUp  Moderators   -  Reputation: 4643

Like
0Likes
Like

Posted 16 May 2013 - 08:27 PM

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.

Edited by AllEightUp, 16 May 2013 - 08:42 PM.

### #7Medo3337  Members   -  Reputation: 683

Like
1Likes
Like

Posted 17 May 2013 - 02:41 AM

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

Edited by Medo3337, 17 May 2013 - 03:34 AM.

### #8Medo3337  Members   -  Reputation: 683

Like
0Likes
Like

Posted 17 May 2013 - 07:14 AM

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?

### #9belfegor  Crossbones+   -  Reputation: 2788

Like
1Likes
Like

Posted 17 May 2013 - 07:34 AM

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.

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

### #10Medo3337  Members   -  Reputation: 683

Like
0Likes
Like

Posted 17 May 2013 - 10:37 AM

@belfegor: Well, your help is one of the main reasons that made me able to accomplish that 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)

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


### #11Medo3337  Members   -  Reputation: 683

Like
0Likes
Like

Posted 17 May 2013 - 04:13 PM

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.

return vVector;
}

### #12Medo3337  Members   -  Reputation: 683

Like
0Likes
Like

Posted 18 May 2013 - 05:21 AM

Up.

### #13AllEightUp  Moderators   -  Reputation: 4643

Like
0Likes
Like

Posted 18 May 2013 - 10:18 AM

Sorry, went out of town.  Will reply in the other thread shortly...

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS