package{
import flash.display.Sprite;
import flash.events.Event;
[SWF(width="465",height="465",backgroundColor="0xffffff",frameRate="60")]
public class Gravity extends Sprite{
private var stars:Vector.<Star>;
private const G:Number=0.000001;
public function Gravity(){
graphics.beginFill(0x000000);
graphics.drawRect(0,0,465,465);
stars=new Vector.<Star>();
for(var i:int=0;i<100;i++){
var star:Star=new Star(Math.random()*465,Math.random()*465,Math.random()*5+5,Math.random()*500+500,(Math.random()*0xffffff>>0)&0xff88ff);
addChild(star);
stars.push(star);
}
stars.push(addChild(new Star(465/2,465/2,20,10000)));
addEventListener(Event.ENTER_FRAME,onEnterFrame);
}
private function onEnterFrame(e:Event):void{
var l:int=stars.length;
for(var i:int=0;i<l;i++){
var s:Star=stars[i];
if(s.density<10000){
var sx:Number=s.x;
var sy:Number=s.y;
var sr:Number=s.radius;
for(var j:int=0;j<l;j++){
if(i!=j){
var t:Star=stars[j];
var stx:Number=t.x-sx;
var sty:Number=t.y-sy;
var r2:Number=stx*stx+sty*sty;
if(r2>Math.pow(sr+t.radius,2)){
var a:Number=G*t.mass/r2;
var angle:Number=Math.atan2(sty,stx);
s.vx+=Math.cos(angle)*a;
s.vy+=Math.sin(angle)*a;
}
}
}
s.x+=s.vx;
s.y+=s.vy;
}
}
}
}
}
import flash.display.Sprite;
import flash.display.Graphics;
class Star extends Sprite{
public var radius:Number;
public var r2:Number;
public var density:Number;
public var mass:Number;
public var vx:Number;
public var vy:Number;
public function Star(x:Number,y:Number,radius:Number,density:Number,color:uint=0xffffff){
this.x=x;
this.y=y;
this.radius=radius;
r2=radius*radius;
this.density=density;
this.mass=4/3*Math.PI*Math.pow(radius,3)*density;
var r:Number=Math.random()*2*Math.PI;
vx=Math.cos(r);
vy=Math.sin(r);
cacheAsBitmap=true;
blendMode="add";
var g:Graphics=graphics;
g.lineStyle(1,0x000000);
g.beginFill(color,0.5);
g.drawCircle(0,0,radius);
g.drawCircle(0,0,radius*0.1);
}
}