Groups | Blog | Home
all groups > flash actionscript > march 2007 >

flash actionscript : Exponential Particle Decay


xaero
3/8/2007 9:43:31 PM
I'm working on an animation that illustrates the concept of half-life (not the
video game). I already have the equation that correctly simulates exponential
decay. The idea is to start out with 1,024 particles spread out randomly in a
square area (about 320 x 320 pixels) and have them disappear at the correct
rate, following this equation:

http://www.petersenart.com/exponentialDecay.jpg

Ideally, the particles would be generated at random at the beginning and stay
in the same spot throughout the animation.

Does anyone know how to translate this into ActionScript? I'm a complete noob
when it comes to scripting. Thank you very much...
kglad
3/8/2007 10:22:01 PM
1. create a particle and give it a linkage id
2. excute a 1024 iteration for-loop that attached your particle at random
locations within your region and store particle references in an array.
2.3. initialize the arrays index and current number
3. start a loop (onEnterFrame or setInterval() ) that iterates each time t
and calculates the number of paritcles to remove (the difference between
previous and current) and use a for-loop to remove those movieclips.
4. increment the start array index
5. update the current number
6. repeat
Rothrock
3/8/2007 11:11:49 PM
How do you deal with fractional particles? The equation gives fractional answers, but, to the best of my knowledge, .4378 of a particle cannot have decayed.

kglad
3/8/2007 11:14:33 PM
Rothrock
3/8/2007 11:16:49 PM
kglad
3/8/2007 11:18:51 PM
xaero
3/9/2007 4:08:31 AM
Rothrock
3/9/2007 4:37:50 AM
When you create a MovieClip symbol, you will see a box (you might need to click
the Advanced button) to Export for Actionscript. Then you can give the exported
symbol a name. Don't include spaces or special characters in the name.

If you already have a clip created and didn't give it a name, you can right
click on its icon in the library panel and select linkage from the contextual
menu.

Remember that even if you don't use the clip in your code it will be included
in your swf ? making the size and download time larger. So if you later decide
not to use a certain clip you should go in and uncheck that box.

Also, I don't think and array is really needed here. You should learn about
them, but I don't think for this you need to do it.

I've attached my solution. I made a movieclip with a little round
semi-transparent circle on frame one and a stop() on the frame actionscript.
Then starting on frame 2 I made a tween of a smaller black dot racing away and
fading out. On the final frame of the movieclip I put a stop().

Remember the above is its own movieclip, not on the main timeline. Check its
linkage and name it atom.

Then put the code I've attached on Frame 1 of your main timeline.

Try it out and then pick it apart. Let me know what you think.


stop();
var frame:Number = 0;
var startPop:Number = 1024;
var lastPop:Number = startPop;
var lambda:Number = 0.069;
var E:Number = Math.E;
var duration:Number=100;
for (var i = 0; i<startPop; i++) {
this.attachMovie("atom", "atom"+i, 1000+i,
{_x:Math.floor(Math.random()*Stage.width),
_y:Math.floor(Math.random()*Stage.height),
_rotation:Math.floor(Math.random()*360)});
}
this.createTextField("generation",10000,10,10,200,20);
generation.text="Year: 0";
decayInterval = setInterval(this, "decay", duration);
function decay() {
frame++;
generation.text="Year: "+frame;
var curPop = Math.round(startPop*Math.pow(E, -lambda*frame));
if (curPop == 0) {
clearInterval(decayInterval);
}
for (var i = lastPop-1; i>=curPop; i--) {
this["atom"+i].gotoAndPlay(2);
}
lastPop = curPop;
}
DMennenoh **AdobeCommunityExpert**
3/9/2007 10:34:39 AM
[quoted text, click to view]

Why not use alpha, or a color to show decay... that way you can visualize
the decay better anyway.

--
Dave -
Head Developer
www.blurredistinction.com
Adobe Community Expert
http://www.adobe.com/communities/experts/

xaero
3/9/2007 5:02:56 PM
Rothrock, you are a god. Thank you so much! I've gotten it to work so far,
but now I just need to constrain the particles between discrete x and y
coordinates, since I want two sets of different particles decaying
side-by-side. I'll let you know how it works.

Also, is there a way to reset this?
Rothrock
3/9/2007 10:58:26 PM
Carefully check out the attachMovie method and its arguments. That is where you
will see how to constrain the particles.

There is always a way to reset things, but not as it is written ? that will be
left as an exercise for the reader. :) If you have problems, post back with
what you try.
AddThis Social Bookmark Button