Brownian (Random) Motion

First some history. One day, a botanist named Robert Brown was trying to look at some grains of pollen in a drop of water and found that they were randomly moving around. Even though there was no current or motion in the water, those little grains just never settled down. He found the same thing happened with dust particles, so it wasnt like the pollen was swimming. Even though he hadnt the slightest idea why they did this, and neither he or anyone else really offered an explanation for several decades, he somehow got the phenomenon named after himjust for noticing it!

The way Brownian motion is explained these days is that the zillions of water molecules in a drop of water are in constant motion, even if the water appears to be still. These molecules collide with the pollen or dust particle, and in doing so, transfer some of their momentum to it. Since even a speck of dust is a million times heavier than a single water molecule , each collision doesnt do much. But when you have so many millions of collisions per second, it starts to add up.

Now some of the molecules might be hitting on one side, and some on the other. Overall, they are going to generally average out. But over time, you are going to see fluctuations, where more are hitting, say, on the left side, and its enough to start the particle moving a bit to the right. Then more might hit on the bottom, and the particle starts to move upwards. Again, these eventually average out, so it doesnt usually result in much momentum in any one direction. You get this random floating-around action.

You can easily simulate this in Flash. On each frame, you just calculate random numbers to add to the x and y velocity of a moving object. The random numbers should be calculated to be either positive or negative, and usually quite small, say in a range from ˆ 0.1 to +0.1. You can do that like so:

 vx += Math.random() * 0.2  0.1; vy += Math.random() * 0.2  0.1; 

Multiplying the random decimal by 0.2 gives you a number from 0.0 to 0.2. Subtracting 0.1 makes it ˆ 0.1 to 0.1. Its important to add some friction into this, otherwise the velocities tend to build up, and things start zipping around unnaturally. In ch19_01.fla , Ive created 50 particles and have them floating around with Brownian motion. The particle is a movie clip with a small black circle in it named dot and exported with that name . Here is the code:

 var numDots:Number = 50; var friction:Number = 0.95; init(); function init():Void {       for(var i:Number = 0;i<numDots;i++)       {             var dot:MovieClip = attachMovie("dot", "dot" + i, i);             dot._x = Math.random() * Stage.width;             dot._y = Math.random() * Stage.height;             dot.vx = 0;             dot.vy = 0;       } } function onEnterFrame():Void {       for(var i:Number = 0;i<numDots;i++)       {             var dot:MovieClip = this["dot" + i];  dot.vx += Math.random() * 0.2 - 0.1;   dot.vy += Math.random() * 0.2 - 0.1;  dot._x += dot.vx;             dot._y += dot.vy;             dot.vx *= friction;             dot.vy *= friction;             if(dot._x > Stage.width)             {                   dot._x = 0;             }             else if(dot._x < 0)             {                   dot._x = Stage.width;             }             if(dot._y > Stage.height)             {                   dot._y = 0;             }             else if(dot._y < 0)             {                   dot._y = Stage.height;              }       } } 

Most of this is old news to you, so Ive put the relevant bits in bold.

Figure 19-1 shows how running this code appears on screen.

image from book
Figure 19-1: Brownian motion

In ch19_02.fla , I reduced the number of dots to 20. I then added the line

 lineStyle(1, 0, 50); 

to the init function, and changed the first part of onEnterFrame to include some drawing code.

 function onEnterFrame():Void {       for(var i:Number = 0;i<numDots;i++)       {             var dot:MovieClip = this["dot" + i];  moveTo(dot._x, dot._y);  dot.vx += Math.random() * 0.2 - 0.1;             dot.vy += Math.random() * 0.2 - 0.1;             dot._x += dot.vx;             dot._y += dot.vy;  lineTo(dot._x, dot._y);  dot.vx *= friction;             dot.vy *= friction; 

This draws a line from where each dot is before it moves, to where it is after it moves. So it draws its own path , as shown in Figure 19-2. Youll often see these kinds of diagrams if you look up the term Brownian motion.

image from book
Figure 19-2: Brownian motion with trails

Brownian motion is useful any time you want something to be moving around as if it was floating with no volition of its own, with no forces really acting on it. You can also add it to a movie clip that has some other motion applied to it, to give it a sense of randomness. An example would be a fly or bee thats buzzing around. You might have it moving along in some path, but adding in some random motion could make it look much more lifelike.



Foundation ActionScript. Animation. Making Things Move
Foundation Actionscript 3.0 Animation: Making Things Move!
ISBN: 1590597915
EAN: 2147483647
Year: 2005
Pages: 137
Authors: Keith Peters

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net