PDA

View Full Version : Q: Random Particle movement with deceleration


jbach
02-02-2002, 01:28 PM
Hi
My math skills are a bit rusty, to say the least.
I've been unsuccessful so far in trying to write a routine for random particle-like movement with both deceleration and collision detection (other particles and stage bounds)
The parameters I need to pass are:
1-initial speed
2-initial direction
3-final _x resting position
4-final _y resting position

Optionally it would be nice to control:
-elasticiity or easing out of the deceleration
and
-duration (although this is indirectly determined by initial speed

To centralize code I want to make this a function and call it from a onCe(eF) handler of each particle MC.
Anyone care to offer help and/or suggestions?

IFZen
02-13-2002, 01:03 PM
First step : You will have to generate the initial positon and velocity (ie. speed rate and direction) of your mcs. For instance like this :

function init(mc) {
clip._x = Math.random() * _root.stageWidth + _root.stageLeft;
clip._y = Math.random() * _root.stageHeight + _root.stageTop;;
clip._vx = Math.random() * _root.vxRange + _root.vxMin;
clip._vy = Math.random() * _root.vyRange + _root.vyMin;
}

Where all the _root vars used here will be defined as constants for your animation, boucing your postion and speed spaces.
This function will have to be called for instance on a load clipEvent for the mcs you will move on stage.

2nd step : Movement
I think a good way for it is to make the movement based on enterFrame clip events. On each frame, mesure the ellapsed time like this :

var t0 = getTimer();
dt = t1 - t0;
t1 = t0;
Then use the dt variable to make move your clips like this :


// linear mouvement
mc._x += mc._vx * dt;
mc._y += mc._vy * dt;

// bouncing
if (mc._x < _root.stageLeft)
mc._x = 2 * _root.stageLeft - mc._x
else if (mc._x > (_root.stageWidth + _root.stageLeft))
mc._x = 2 * (_root.stageWidth + _root.stageLeft) - mc._x;

if (mc._y < _root.stageTop)
mc._y = 2 * _root.stageTop- mc._y
else if (mc._y > (_root.stageHeight + _root.stageTop))
mc._y = 2 * (_root.stageHeight + _root.stageTop) - mc._y;


And if you want your clip to decelerate, reduce the _vx and _vy variables by a determinate factor (that you can define in the init() function with a value take between 0 and 1):


mc._vx *= mc._deceleration;
mc._vy *= mc._deceleration;


All this code can be placed into a function getting mc as argument. This function will then be called by all your mc's on the enterFrame clip event.

For collision, I won't go into details (this would be too long to write and you already have a lot to analyse) but the principle is to test collision with all your mc's instaces (!) and then to decide what to do with the velocities of collided mc's. In a physical way you have to choose randomly a symetric line between the mc's and to reflect both speeds regarding this line. Doc Cos and Ms. Sin are by the way.

Hope it helps, and...

BZen

jbach
02-14-2002, 12:21 PM
Much thanks

jbach
02-14-2002, 12:29 PM
The way I undderstand it i would inititialize the objects by first calling the 'init' function in a onCE(Load) handler and then call the remaining code (as a function) from an onCE(enterFrame) handler.
Question: what is the best way to use clipevents for calling functions?
I've head some people say to use separate empty MC's with just the CE code and others swear by the 2 frame looping method (Flash 4).
If I use blank MC's with onCE handlers do I need one blank MC with onCE handlers for every object I'm trying to move??

IFZen
02-14-2002, 12:52 PM
Well both methods are equivalent but playing with clip events tends to get some fun in AS coding. Here is a post of ananimation I made. It is nearly what you're doing except I coded acceleration between particules. I used the clip events method. Make what you want of it.

See you,
IFZen

part.fla (http://ifzen.levillage.org/AS/part.fla)