/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/rgJv
*/
package {
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
stage.quality = "LOW";
vec = new Vector.<xBlob>(0,false);
var a:xBlob; var i:int;
for (i=0;i<64;i++)
{
a = new xBlob();
a.cx = Math.random()*465;
a.cy = Math.random()*465;
//debug -- should work if two blobs in the exact same coords
//a.cx = 235; a.cy = 235;
vec.push(a);
}//nexti
myBlob = new xBlob();
myBlob.mass = 40;
stage.addEventListener(MouseEvent.CLICK, onClick);
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public function onClick(e:MouseEvent):void
{
var i:int; var num:int; var a:xBlob; var b:xBlob;
num = vec.length;
for (i =0;i<num;i++)
{
a = vec[i];
a.vx = Math.random()*32-16;
a.vy = Math.random()*32-16;
}
}//onclick
public var vec:Vector.<xBlob>;
public var myBlob:xBlob;
public function onEnter(e:Event):void
{
myBlob.cx = 230;
myBlob.cy = 230;
var ax:Number;
var ay:Number;
ax =230; ay= 230;
ax = stage.mouseX;
ay = stage.mouseY;
graphics.clear();
graphics.lineStyle(2,0);
var k:int;
var i:int; var num:int; var a:xBlob; var b:xBlob;
num = vec.length;
for (i =0;i<num;i++)
{
a = vec[i]; a.pw = a.w; a.w =0;
}
graphics.lineStyle(2,0xFF0000,1);
for (i =0;i<num;i++)
{
a = vec[i];
var ang:Number;
ang = Math.atan2(ay-a.cy, ax-a.cx);
a.vx += Math.cos(ang)*0.1;
a.vy += Math.sin(ang)*0.1;
// spring(a, myBlob, 30);
// myBlob.cx = 230; myBlob.cy = 230;
for (k = i+1; k < num; k++)
{
b = vec[k];
if (getMag(a.cx-b.cx,a.cy-b.cy)<50)
{
graphics.moveTo(a.cx,a.cy); graphics.lineTo(b.cx,b.cy);
spring(a,b, 20);
a.w +=1; b.w +=1;
}
}//nextk
}//nexti
graphics.lineStyle();
for (i =0;i<num;i++)
{
a =vec[i];
a.vy+=0.15 - a.w*0.01;
if (a.vx >0 && a.cx >465) {a.vx*=-0.5;}
else if (a.vx <0 && a.cx < 0) {a.vx*=-0.5;}
if (a.vy >0 && a.cy >465) { if(a.vy<0.5){a.vy=0;} else{ a.vy*=-0.5; } }
else if (a.vy <0 && a.cy < 0) {a.vy*=-0.5;}
a.cx += a.vx; a.cy+=a.vy;
graphics.beginFill(0,0.85);
graphics.drawCircle(a.cx,a.cy,16+a.w);
graphics.endFill();
a.mass = 5 + a.w;
}//nexti
}//onenter
public function getMag(ax:Number,ay:Number):Number
{ return Math.sqrt(ax*ax+ay*ay); }
public function spring(a:xBlob, b:xBlob, rest:Number):void
{
var t:Number;
t =0.5;
t=0.1;
var ax:Number; var ay:Number; var dx:Number; var dy:Number;
var nx:Number; var ny:Number; var dist:Number; var mag:Number;
var reld:Number; var relvel:Number; var imp:Number; var rem:Number;
dx = a.cx - b.cx; dy = a.cy - b.cy;
mag = Math.sqrt(dx*dx + dy*dy);
//if (mag < rest/2) {return;}
if (mag == 0) {mag = Math.random(); a.cx += Math.random()-0.5; b.cy+=Math.random()-0.5;}
nx = dx / mag; ny = dy / mag;
ax = a.vx - b.vx; ay = a.vy - b.vy;
relvel = (ax*nx) +(ay*ny);
reld = mag - (rest+rest);
rem = relvel + reld;
imp = rem / (1/a.mass + 1/b.mass);
imp *= 0.5;
a.vx += (imp *(nx*-t )) / a.mass; a.vy += (imp *(ny*-t )) / a.mass;
b.vx += (imp*(nx*t )) / b.mass; b.vy += (imp * (ny * t )) / b.mass;
}//spring
}//classend
}
internal class xBlob
{
public var cx:Number = 0;
public var cy:Number = 0;
public var vx:Number =0;
public var vy:Number = 0;
public var mass:Number = 5;
public var w:int = 0;
public var pw:int = 0;
}//xblob