FlowSimulation3D [ff]
package
{
import flash.display.*;
import flash.geom.*;
import flash.events.*;
import flash.utils.*;
import flash.geom.*;
import net.hires.debug.Stats;
public class FlowSimulation3D extends Sprite
{
private const NUM_PARTICLE:uint = 20;
private const SW:Number = stage.stageWidth;
private const SH:Number = stage.stageHeight;
private var world:Sprite = new Sprite();
private var bmpData:BitmapData = new BitmapData( 465, 465, false, 0xFFFFFF );
private var forceMap:BitmapData = new BitmapData( 233, 233, false, 0xFFFFFF );
private var randomSeed:uint = Math.floor( Math.random() * 0xFFFF );
private var rect:Rectangle = new Rectangle( 0, 0, 465, 465 );
private var seed:Number = Math.floor( Math.random() * 0xFFFF );
private var offset:Array = [new Point(), new Point()];
private var particleList:Vector.<Particle> = new Vector.<Particle>(NUM_PARTICLE, true);
private var colorTransform:ColorTransform = new ColorTransform( 0.5, 0.5, 0.5, 1.0 );
private var timer:Timer;
public function FlowSimulation3D()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.quality = StageQuality.BEST;
stage.frameRate = 90;
addChild(new Bitmap( bmpData )) as Bitmap;
//addChild(new Bitmap( forceMap )) as Bitmap;
resetFunc();
for (var i:uint = 0; i < NUM_PARTICLE; i++)
{
var px:Number = Math.random() * SW;
var py:Number = Math.random() * SH;
var pz:Number = Math.random() * SW * 2 - SW;
particleList[i] = new Particle(px, py, pz);
world.addChild(particleList[i]);
}
addEventListener( Event.ENTER_FRAME, loop );
var timer:Timer = new Timer(500)
timer.addEventListener(TimerEvent.TIMER, resetFunc);
timer.start();
addChild(new Stats);
}
private function loop( e:Event ):void
{
var len:uint = particleList.length;
var col:Number
for (var i:uint = 0; i < len; i++)
{
var dots:Particle = particleList[i];
col = forceMap.getPixel( dots.x >> 1, dots.y >> 1);
dots.ax += ( (col >> 8 & 0xff) - 128 ) * .0005;
dots.ay += ( (col >> 4 & 0xff) - 128 ) * .0005;
dots.az += ( (col >> 16 & 0xff) - 128 ) * .0005;
dots.vx += dots.ax;
dots.vy += dots.ay;
dots.vz += dots.az;
dots.x += dots.vx;
dots.y += dots.vy;
dots.z += dots.vz;
dots.rotationX += dots.vx;
dots.rotationY += dots.vy;
dots.rotationZ -= dots.vz;
var _posX:Number = dots.x;
var _posY:Number = dots.y;
var _posZ:Number = dots.z;
dots.ax *= .95;
dots.ay *= .95;
dots.az *= .95;
dots.vx *= .90;
dots.vy *= .90;
dots.vz *= .90;
( _posX > SW ) ? dots.x = 0 :
( _posX < 0 ) ? dots.x = SW : 0;
( _posY > SH ) ? dots.y = 0 :
( _posY < 0 ) ? dots.y = SH : 0;
( _posZ > SW ) ? dots.z = -SW :
( _posZ < -SW ) ? dots.z = SW : 0;
}
bmpData.colorTransform(rect, colorTransform);
bmpData.draw(world);
}
private function resetFunc(e:Event = null):void{
forceMap.perlinNoise(233, 233, 3, seed, false, false, 1|2|4|0, false, offset );
offset[0].x += 1.5;
offset[1].y += 1;
seed = Math.floor( Math.random() * 0xFFFFFF );
}
}
}
import flash.display.Sprite;
class Particle extends Sprite{
public var vx:Number = 0;
public var vy:Number = 0;
public var vz:Number = 0;
public var ax:Number = 0;
public var ay:Number = 0;
public var az:Number = 0;
function Particle( x:Number, y:Number, z:Number ) {
this.x = x;
this.y = y;
this.z = z;
graphics.beginFill(0xFFFFFF);
graphics.drawCircle(0,0,10);
graphics.endFill();
}
}