forked from: forked from: particle _ 星間飛行? 2
@author sph62
KonKiKi
改造させて頂きました。
[SWF(backgroundColor="0x000000",width="800",height="800",frameRate="30")]
// forked from KonKiKi's forked from: particle _ 星間飛行?
// forked from sph62's particle
package {
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.*;
import flash.geom.*;
/**
*
* @author sph62
*
* KonKiKi
* 改造させて頂きました。
**/
//[SWF(backgroundColor="0x000000",width="800",height="800",frameRate="30")]
public class Main extends Sprite {
private var particles:Array;
private var numParticles:uint = 30;
private var minDist:Number = 100;
private var springAmount:Number = 0.001;
private var damp:Number = 0.98;
private var renge:Number = 4;
//konKiKi
private var space:Sprite;
private var bmp:BitmapData;
private var cxform:ColorTransform;
public function Main() {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
var fx:Number = Math.random() * stage.stageWidth;
var fy:Number = Math.random() * stage.stageHeight;
init(fx,fy);
}
private function init(fx:Number,fy:Number):void {
//KonKiKi
bmp = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0);
cxform = new ColorTransform(1,1,1,0.99);
addChild( new Bitmap(bmp) );
space = new Sprite();
addChild(space);
particles = new Array();
for (var i:uint = 0; i < numParticles; i++) {
var size:Number = 1;
var mass:Number = Math.random() * 79 + 50;
var fr:Number = Math.random() * 360;
var fv:Number = Math.random() * 2 + 1;
var particle:Ball = new Ball(size,mass,fr,fv);
particle.x = fx;
particle.y = fy;
addChild(particle);
particles.push(particle);
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(MouseEvent.CLICK,reset,false,0,true);
}
private function onEnterFrame(event:Event):void {
space.graphics.clear();
for (var i:uint = 0; i < particles.length; i++ ) {
var partA:Ball = particles[i];
upDataePosition(partA);
border(partA);
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 - partA.scaleX));
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;
}
}
//KonKiKi
private function drawSpace():void{
bmp.lock();
bmp.colorTransform(bmp.rect,cxform);
bmp.draw(stage);
bmp.unlock();
}
private function upDataePosition(particle:Ball):void {
particle.vr = particle.vr * damp + ( Math.random() * (renge * 2) - renge );
particle.r += particle.vr;
var rad:Number = particle.r *( Math.PI / 180 );
particle.x += Math.cos(rad) * particle.vx;
particle.y += Math.sin(rad) * particle.vy;
particle.o += particle.vo;
if(particle.o > Math.PI * 2) particle.o -= Math.PI * 2;
var scale:Number = Math.sin(particle.o);
particle.scaleX = scale;
particle.scaleX = scale;
}
private function reset(eventObj:MouseEvent):void {
removeEventListener(Event.ENTER_FRAME,onEnterFrame);
bmp.dispose();
for(var i:uint = 0; i < particles.length; i++){
removeChild(particles[i]);
particles[i] = null;
}
init(stage.mouseX,stage.mouseY);
}
}
}
import flash.display.Sprite;
class Ball extends Sprite {
public var _radius:Number;
public var _color:uint;
public var mass:Number;
public var vx:Number;
public var vy:Number;
public var r:Number;
public var vr:Number;
public var o:Number;
public var vo:Number;
public function Ball(radius:Number,m:Number,fr:Number,fv:Number,color:uint = 0xFFFFFF) {
this._radius = radius;
this._color = color;
this.mass = m;
this.vx = fv;
this.vy = fv;
this.r = fr;
this.vr = 0;
this.o = 0;
this.vo = Math.random() * 0.05 - 0.005;
init();
}
private function init():void {
graphics.beginFill(_color,_radius);
graphics.drawCircle(0, 0, 1);
graphics.endFill();
}
}