/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/6saj
*/
package {
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest()
{
vec = new Vector.<xBlob>(0,false);
var a:xBlob; var i:int;
// a = new xBlob(); vec.push(a);
//a.cx=235; a.cy=235;
var ta:Number; var ms:Number;
ms=16;
num=10;
for (ms=16;ms>8;ms-=4)
for (i=0;i<num;i+=1)
{
a = new xBlob(); vec.push(a);
ta = i* (6.28/num);
a.cx = 235+Math.cos(ta) * ms;
a.cy= 235+Math.sin(ta) * ms;
a.ang = ta;
}//nexti
vspr = new Vector.<xSpring>(0,false);
var k:int; var num:int;
var b:xBlob; var m:xSpring;
num = vec.length;
for (i =0;i<num;i+=1)
{
a = vec[i];
for (k=i;k<num;k+=1)
{
b = vec[k];
if (getBlobMag(a,b) >24) { continue; }
addSpring(a,b, 0.1);
}//nextk
}//nexti
// addSpring(vec[0], vec[16], 0.2);
// addSpring(vec[4], vec[20], 0.2);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMdown);
stage.addEventListener(MouseEvent.MOUSE_UP, onMup);
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public function onMdown(e:MouseEvent):void { mbutton = 1;}
public function onMup(e:MouseEvent):void { mbutton = 0;}
public var mbutton:int = 0;
public function addSpring(a:xBlob, b:xBlob, t:Number=0.5):void
{
var m:xSpring;
m = new xSpring(); vspr.push(m);
m.a= a; m.b=b;
m.rest = getBlobMag(a, b);
m.t =t;
}//addSpring
public var vec:Vector.<xBlob>;
public var vspr:Vector.<xSpring>;
public function onEnter(e:Event):void
{
var m:xSpring;
var a:xBlob; var i:int; var num:int;
graphics.clear();
graphics.lineStyle(2,0);
num = vspr.length;
for (i=0;i<num;i+=1)
{
m = vspr[i];
spring(m.a, m.b, m.rest, m.t);
// graphics.moveTo(m.a.cx, m.a.cy);
// graphics.lineTo(m.b.cx, m.b.cy);
}//nexti
var mx:Number; var my:Number;
var ax:Number; var ay:Number;
ax=0;ay=0;
mx = stage.mouseX; my = stage.mouseY;
num = vec.length;
for (i =0;i<num;i+=1)
{
a = vec[i];
if (a.cx < mx-8 ) { a.vx+=0.5; }
else if(a.cx >mx+8){a.vx-=0.5;}
if(mbutton>0){a.vy-=1;}
a.vy += 0.2;
a.vx *= 0.99;
a.vy *= 0.99;
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; } a.vx *= 0.8; }
else if (a.vy <0 && a.cy < 0) {a.vy*=-0.5;}
a.cx += a.vx; a.cy+=a.vy;
ax+=a.cx; ay+=a.cy;
graphics.beginFill(0,0.85);
graphics.drawCircle(a.cx,a.cy, 4);
graphics.endFill();
// graphics.moveTo(a.cx, a.cy);
// graphics.lineTo(a.cx+Math.cos(a.ang+ang)*16, a.cy+Math.sin(a.ang+ang)*16);
}//nexta
var ta:Number;
ax/=num; ay/=num;
cx=ax; cy=ay;
a = vec[0];
ta = Math.atan2(ay-a.cy,ax-a.cx);
ang = ta;
graphics.drawCircle(cx,cy,8);
graphics.moveTo(cx,cy);
graphics.lineTo(cx+Math.cos(ang)*16, cy+Math.sin(ang)*16);
/*
for (i =0;i<num;i+=1)
{
a = vec[i];
//ax = cx + Math.cos(ang)*16;
//ay = cy+Math.sin(ang)*16;
ta = Math.atan2(ay-a.cy,ax-a.cx);
a.vx +=Math.cos(ta)*0.5;
a.vy+=Math.sin(ta) *0.5;
}//nexti
*/
}//onenter
public var cx:Number = 0;
public var cy:Number = 0;
public var ang:Number = 0;
public function getMag(ax:Number,ay:Number):Number
{ return Math.sqrt(ax*ax+ay*ay); }
public function getBlobMag(a:xBlob, b:xBlob):Number
{ return getMag(a.cx - b.cx, a.cy - b.cy); }
public function spring(a:xBlob, b:xBlob, rest:Number, t:Number=0.5):void
{
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 rem:Number;
dx = ((a.cx + a.vx) - (b.cx + b.vx));
dy = ((a.cy+a.vy) - (b.cy+b.vy));
mag = Math.sqrt(dx * dx + dy * dy);
if (mag == 0) {mag = Math.random(); a.cx += Math.random()-0.5; b.cy+=Math.random()-0.5;}
//if (mag == 0) { mag = 0.001; }
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;
if (rem > 130) { rem = 130; }
else if (rem < -130) { rem = -130; }
a.vx += (rem *(nx*-t )) ; a.vy += (rem *(ny*-t )) ;
b.vx += (rem* (nx*t )) ; b.vy += (rem * (ny * t )) ;
}//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 ang:Number = 0;
}//xblob
internal class xSpring
{
public var a:xBlob = null;
public var b:xBlob = null;
public var rest:Number = 30;
public var t:Number = 0.1;
}//xspring