forked from: particle _ 星間飛行?
@author sph62
KonKiKi
改造させて頂きました。
[SWF(backgroundColor="0x000000",width="800",height="800",frameRate="30")]
// forked from sph62's particle
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
/**
*
* @author sph62
*/
/*KonKiKi
改造させて頂きました。
*/
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.*;
//[SWF(backgroundColor="0x000000",width="800",height="800",frameRate="30")]
public class Main extends Sprite {
private var particles:Array;
private var numParticles:uint = 50;
private var minDist:Number = 100;
private var springAmount:Number = .001;
//konKiKi
private var space:Sprite;
private var bmp:BitmapData;
private var cxform:ColorTransform;
public function Main() {
init();
}
private function init():void {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
//KonKiKi
bmp = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0);
cxform = new ColorTransform(1,1,1,0.98);
addChild( new Bitmap(bmp) );
space = new Sprite();
addChild(space);
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);
}
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);
}
space.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);
}
}
//KonKiKi
drawSpace();
}
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) {
space.graphics.lineStyle(1,0xFFFFFF,1-dist/(minDist*0.7));
space.graphics.moveTo(partA.x, partA.y);
space.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;
}
*/
//KonKiKi
private function drawSpace():void{
bmp.lock();
bmp.colorTransform(bmp.rect,cxform);
bmp.draw(stage);
bmp.unlock();
}
}
}
import flash.display.Sprite;
class Ball extends Sprite {
public var _radius:Number;
public var _color:uint;
public var mass:Number = 0;
public var vx:Number = 0;
public var vy:Number = 0;
public function Ball(radius:Number = 1, color:uint = 0xFFFFFF) {
this._radius = radius;
this._color = color;
this.mass = radius * 5;
init();
}
private function init():void {
graphics.beginFill(_color, 1);
graphics.drawCircle(0, 0, 1);
graphics.endFill();
}
}