In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Particle と Attractor の練習

[パーティクルとアトラクタの練習]
* しばらく画面を眺めていると描画されます。
* もう少し処理が軽くならないか試行錯誤中です。
* クリックでリセット。 
* 
* 参考:
* http://www.tom-carden.co.uk/p5/additive_particles4/applet/index.html
Get Adobe Flash player
by enoeno 08 Mar 2012
/*
* [パーティクルとアトラクタの練習]
* しばらく画面を眺めていると描画されます。
* もう少し処理が軽くならないか試行錯誤中です。
* クリックでリセット。 
* 
* 参考:
* http://www.tom-carden.co.uk/p5/additive_particles4/applet/index.html
*/

package {
    
    import flash.display.*;
    import flash.geom.*;
    import flash.events.*;
    
    import net.hires.debug.Stats;
    
    public class Main extends MovieClip {
        
        private var WIDTH:Number;
        private var HEIGHT:Number;
                
        private var _particle:Vector.<Particle>;
        private var _attractor:Vector.<Attractor>;
        private var _posX:Vector.<Number>;
        private var _posY:Vector.<Number>;
        
        private const NUM_PARTICLE:uint = 3500;
        private const NUM_ATTRACTOR:uint = 8;
        
        public var damp:Number = 0.00002;
        public var accel:Number = 10000;
        
        private var _screen:BitmapData;
        private var _canvas:Sprite;
        private var _parts:Shape;
        private var _rect:Rectangle;
        
        private var rad:Number;
        private var dRad:Number;
        private var colR:Number, colG:Number, colB:Number;
        private var rnd1:int, rnd2:int, rnd3:int;
        
        public function Main():void
        {
            init();
        }
        
        private function init():void
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            
            WIDTH = stage.stageWidth;
            HEIGHT = stage.stageHeight;
            
            _screen = new BitmapData( WIDTH, HEIGHT, false, 0x0 );
            addChild( new Bitmap(_screen) ) as Bitmap;
            
            
            _parts = new Shape();
            _parts.graphics.beginFill( 0xFFFFFF, 1 );
            _parts.graphics.drawCircle( 0, 0, 0.2 );
            
            _rect = new Rectangle(0, 0, 1, 1);
        
            stage.addEventListener( Event.ENTER_FRAME, update );
            stage.addEventListener( MouseEvent.CLICK, reset );
            stage.addEventListener( Event.RESIZE, resize );
            //addChild(new Stats());
            Wonderfl.capture_delay(180);
            reset();
        }
        
        private function reset( e:Event = null ):void
        {
            //_particle = [];
            //_attractor = [];
            _particle = new Vector.<Particle>(NUM_PARTICLE, true);
            _attractor = new Vector.<Attractor>(NUM_ATTRACTOR, true);
            _posX = new Vector.<Number>(NUM_PARTICLE, true);
            _posY = new Vector.<Number>(NUM_PARTICLE, true);
            
            rad = 0;
            dRad = Math.random()*0.02 + 0.03;
            
            rnd1 = Math.random()+0.5;
            rnd2 = Math.random()+0.5;
            rnd3 = Math.random()+0.5;
            
            colB = Math.floor( Math.random()*128 + 128 );
            colG = Math.floor( Math.random()*( colB * 0.5 ) + colB * 0.105 );
            colR = Math.floor( Math.random()*( colG * 0.5 ) + colG * 0.105 );
            
            var color = "0x"+ colR.toString(16) + colG.toString(16) + colB.toString(16);
            
            _screen.fillRect( _screen.rect, 0x0 );
            
            var i:uint;
            
            // init Attractors
            for ( i = 0; i < NUM_ATTRACTOR; i++) {
                
                var a:Attractor = new Attractor();
                a.x = Math.random()*WIDTH;
                a.y = Math.random()*HEIGHT;
                
                _attractor[i] = a;
            }
            
            //init Particles
            for (i = 0; i < NUM_PARTICLE; i++) {
                var p:Particle = new Particle();
                
                p.x = Math.random()*WIDTH;
                p.y = Math.random()*HEIGHT;
                
                p.vx = -accel/2 + Math.random()*accel;
                p.vy = -accel/2 + Math.random()*accel;
                
                _particle[i] = p;
            }
        }
        
        private function resize( e:Event = null )
        {
            stage.removeEventListener( Event.ENTER_FRAME, update );
            stage.removeEventListener( MouseEvent.CLICK, reset );
            stage.removeEventListener( Event.RESIZE, resize );
            init();
        }
        
        
        private function update( e:Event=null ):void
        {
            updatePos();
            
        }
        
        private function updatePos():void
        {
            var p:Particle;
            var a:Attractor;
            var d:Number;
            
            for( var i:uint = 0; i < NUM_PARTICLE; i++ ){
                
                p = _particle[i];
                
                for( var k:uint = 0; k < NUM_ATTRACTOR; k++ ){
                  
                  a = _attractor[k];
                  d = Math.pow(a.x-p.x, 2) + Math.pow(a.y-p.y, 2);
                  
                  if (d > 0.1) {
                    p.vx += accel * (a.x-p.x) / d;
                    p.vy += accel * (a.y-p.y) / d;
                  }
                }
                
                p.x += p.vx;
                p.y += p.vy;
                p.vx *= damp;
                p.vy *= damp;
                
                _posX[i] = p.x;
                _posY[i] = p.y;
            }
            draw();
        }
        
        private function draw():void
        {
            var m:Matrix;
            var color:ColorTransform = getColor();
            
            _screen.lock();
            for( var i:uint = 0; i < NUM_PARTICLE; i++ )
            {
                m = new Matrix();
                m.translate( _posX[i], _posY[i] );
                
                _screen.draw( _parts, m, color, BlendMode.ADD, null, false );
            }
            _screen.unlock();
        }
        
        private function getColor():ColorTransform
        {
            var r:Number = Math.floor( colB + Math.cos(rad*rnd1*3)*40 );
            var g:Number = Math.floor( colG + Math.cos(rad*rnd2*3)*40 );
            var b:Number = Math.floor( colR + Math.cos(rad*rnd3*3)*40 );
            
            return new ColorTransform(0, 0, 0, 0.4, r,  g, b, 0);
        }
        
    }
}


class Particle {
    public var x:Number;
    public var y:Number;
    public var vx:Number;
    public var vy:Number;
    
    public function Particle()
    {
        x = 0;
        y = 0;
        vx = 0;
        vy = 0;
    }
}

class Attractor
{
    public var x:Number;
    public var y:Number;
    
    public function Attractor()
    {
        x = 0;y = 0;
    }
}