forked from: particle
@author sph62
// forked from sph62's particle
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import net.hires.debug.*;
/**
*
* @author sph62
*/
[SWF(backgroundColor="0xFFFFFF",width="800",height="800",frameRate="200")]
public class Main extends Sprite {
private var particles:Array;
private var numParticles:uint = 50;
private var minDist:Number = 100;
private var springAmount:Number = .001;
public function Main() {
init();
}
private function init():void {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
particles = new Array();
for (var i:uint = 0; i < numParticles; i++) {
var size:Number = Math.random() * 10 + 2;
var particle:Ball = new Ball(size);
particle.x = Math.random() * stage.stageWidth;
particle.y = Math.random() * stage.stageHeight;
particle.vx = Math.random() * 2 - 1;
particle.vy = Math.random() * 2 - 1;
particle.mass = size;
addChild(particle);
particles.push(particle);
}
addChild(new Stats());
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void {
for (var i:uint = 0; i < numParticles; i++) {
var particle:Ball = particles[i];
particle.x += particle.vx;
particle.y += particle.vy;
border(particle);
}
// graphics.clear();
for (i = 0; i < numParticles - 1; i++ ) {
var partA:Ball = particles[i];
for (var j:uint = i + 1; j < numParticles; j++ ) {
var partB:Ball = particles[j];
spring(partA, partB);
}
}
}
private function spring(partA:Ball, partB:Ball):void {
var dx:Number = partB.x - partA.x;
var dy:Number = partB.y - partA.y;
var distSQ:Number = dx * dx + dy * dy;
var dist:Number = Math.sqrt(distSQ);
if (dist < minDist) {
/*
graphics.lineStyle(1,0x0066ff,1-dist/minDist);
graphics.moveTo(partA.x, partA.y);
graphics.lineTo(partB.x, partB.y);
*/
var ax:Number = dx * springAmount;
var ay:Number = dy * springAmount;
partA.vx += ax / partA.mass;
partA.vy += ay / partA.mass;
partB.vx -= ax / partB.mass;
partB.vy -= ay / partB.mass;
}
}
private function border(particle:Ball):void {
if (particle.x > stage.stageWidth) {
particle.x = 0;
}else if (particle.x < 0) {
particle.x = stage.stageWidth;
}
if (particle.y > stage.stageHeight) {
particle.y = 0;
}else if (particle.y < 0) {
particle.y = stage.stageHeight;
}
}
/*
private function gravitate(partA:Ball, partB:Ball):void {
var dx:Number = partB.x - partA.x;
var dy:Number = partB.y - partA.y;
var distSQ:Number = dx * dx + dy * dy;
var dist:Number = Math.sqrt(distSQ);
var force:Number = partA.mass * partB.mass / distSQ;
var ax:Number = force * dx / dist;
var ay:Number = force * dy / dist;
partA.vx += ax / partA.mass;
partA.vy += ay / partA.mass;
partB.vx -= ax / partB.mass;
partB.vy -= ay / partB.mass;
}
*/
}
}
import flash.display.Sprite;
class Ball extends Sprite {
public var _radius:Number;
public var _color:uint;
public var _alpha:Number;
public var mass:Number = 0;
public var vx:Number = 0;
public var vy:Number = 0;
public function Ball(radius:Number = 25, color:uint = 0x0066FF, alpha:Number = .3 ) {
this._radius = radius;
this._color = color;
this._alpha = alpha;
this.mass = radius * 5;
init();
}
private function init():void {
graphics.beginFill(_color, _alpha);
graphics.drawCircle(0, 0, _radius);
graphics.endFill();
graphics.beginFill(_color, _alpha);
graphics.drawCircle(0, 0, _radius*2);
graphics.endFill();
}
}