BrownianGravitation3D

by bradsedito forked from gravitation (diff: 135)
BradSedito
2011
♥0 | Line 209 | Modified 2011-09-20 03:59:59 | MIT License
play

ActionScript3 source code

/**
 * Copyright bradsedito ( http://wonderfl.net/user/bradsedito )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/1iLt
 */

// BradSedito
// 2011

package 
{
    
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.filters.BlurFilter;
    import com.greensock.*;
    import com.greensock.easing.*;
    import net.hires.debug.Stats;
    
    
    [SWF(width="465", height="465", frameRate="60",backgroundColor=0x000000)]
    
    
    public class BrownianParticles extends Sprite
    {
        
        public const SW:Number = new Number( stage.stageWidth  );
        public const SH:Number = new Number( stage.stageHeight ); 
        public const DEPTH:Number = new Number( 500 );
        public const sunColor  : uint = new uint( 0xFFFFFF );
        public const lineColor : uint = new uint( 0xFFFFFF );
        public   var particleColor:uint;
        public   var particles:Array;
        public   var num:uint= 50;
        public   var fl:Number = 0.8;
        public   var f2:Number = 0.8;
        public   var gravity:Number = 0.1;
        public   var bounce:Number = -0.8;
        public   var sun:particle;
        public   var sun2:particle;
        public   var sun3:particle;
        public   var sun4:particle;
        public   var randomColor:uint;
        public   var whiteColor:uint = new uint( 0xFFFFFF );
        public   var blurr:BlurFilter = new BlurFilter( 6,6,2 );
        public   var _stats:Stats = new Stats();
        
        
        public function BrownianParticles() 
        {
                    
            sun  = new particle( 0x000000,false,010 );
            sun2 = new particle( sunColor,false,020 );
            sun3 = new particle( sunColor,false,050 );
            sun4 = new particle( sunColor,false,300 );
            
            sun.x  = stage.stageWidth/2;
            sun.y  = stage.stageHeight/2;
            sun.z  = 0;
            sun2.x = stage.stageWidth/2;
            sun2.y = stage.stageHeight/2;
            sun2.z = 0;            
            sun3.x = stage.stageWidth/2;
            sun3.y = stage.stageHeight/2;
            sun3.z = 0;
            sun4.x = stage.stageWidth/2;
            sun4.y = stage.stageHeight/2;
            sun4.z = 0;
            
            sun.vx = 0;
            sun.vy = 0;
            sun.vz = 0;
            sun2.vx = 0;
            sun2.vy = 0;
            sun2.vz = 0;
            sun3.vx = 0;
            sun3.vy = 0;
            sun3.vz = 0;
            sun4.vx = 0;
            sun4.vy = 0;
            sun4.vz = 0;
            
            sun .mass = (10000);   // (Math.PI*03.333)                 );
            sun2.mass = 0;         // (5000 / 2); // (Math.PI*06.666)  );
            sun3.mass = 0;         // (5000 / 2); // (Math.PI*09.999)  );
            sun4.mass = 0;         // (5000 / 2); // (Math.PI*10.000)  );
            
            sun .alpha = 0.5;           
            sun2.alpha = 0.4;
            sun3.alpha = 0.3;
            sun4.alpha = 0.2;
           
          //sun .blendMode = "add";             
          //sun2.blendMode = "add";             
          //sun3.blendMode = "add";             
          //sun4.blendMode = "add";             
            
            addChild(  sun );
            addChild( sun2 );
            addChild( sun3 );
            addChild( sun4 );
            
            TweenMax.to(sun , 0, {blurFilter:{blurX:10, blurY:10, quality:1}}); 
            TweenMax.to(sun2, 0, {blurFilter:{blurX:10, blurY:10, quality:1}}); 
            TweenMax.to(sun3, 0, {blurFilter:{blurX:10, blurY:10, quality:1}}); 
            TweenMax.to(sun4, 0, {blurFilter:{blurX:10, blurY:10, quality:1}}); 
            
            _stats.y  = _stats.y + 10; 
            _stats.x  = _stats.x + 10; 
            _stats.alpha = .8;
            addChild(_stats); 
            
            particles = new Array();
            
            for(var i:uint = 0; i < num; i++)
            {
                var obj:particle = new particle( particleColor,false, 6 );
                obj.x  = Math.random() * stage.stageWidth; 
                obj.y  = Math.random() * stage.stageHeight;
                obj.z  = Math.random() * DEPTH;
                obj.vx = Math.random() * 0.2 - 0.1
                obj.vy = Math.random() * 0.2 - 0.1;
                obj.vz = Math.random() * 0.2 - 0.1;
                obj.mass = 1;
            
                TweenMax.to( obj, 0, {blurFilter:{blurX:10, blurY:10, quality:1} });     
                    
                addChild( obj );
                particles.push( obj );

            }
            
            addEventListener(Event.ENTER_FRAME, onEF);
        }
        private function onEF(event:Event):void
        {
            graphics.clear();
            sun.x = stage.stageWidth/2;
            sun.y = stage.stageHeight/2;
            sun.z = 0; 
            screenCheck(sun);
            
            for(var i:uint = 0; i < num; i++)
            {
                move(particles[i]);
                                
                graphics.lineStyle(1, 0x000000, .5);
                graphics.moveTo(sun.x, sun.y);
                graphics.lineTo(particles[i].x, particles[i].y);
                gravitate(particles[i], sun);
                screenCheck(particles[i])
              //_line.filters = [ blurr ];
            }
        }
        private function move(me:particle):void
        {
            me.x += me.vx;
            me.y += me.vy;
            me.z += me.vz;
            me.vy += gravity;
            me.vx *= fl;
            me.vz *= f2;
        }
        private function gravitate(partA:particle, partB:particle):void
        {
            var dx:Number = partB.x - partA.x;
            var dy:Number = partB.y - partA.y;
            var dz:Number = partB.z - partA.z;
            var distSQ:Number = dx*dx + dy*dy;
            var dist:Number = Math.sqrt(distSQ);
            if(dist > partB.r + partA.r){
                var force:Number = partA.mass * partB.mass / distSQ;
                var ax:Number = force * dx / dist;
                var ay:Number = force * dy / dist;
                var az:Number = force * dz / dist;
                partA.vx += ax / partA.mass;
                partA.vy += ay / partA.mass;
                partA.vz += az / partA.mass;
                
            }else{
                partA.vx = Math.random() * 100 - 50;
                partA.vy = Math.random() * 100 - 50;
                partA.vz = Math.random() * 100 - 50;                
            }

        }
        private function screenCheck(me:particle):void
        {
            var rad:Number =  me.r;
            if(me.x + rad > stage.stageWidth)
            {
                me.x = stage.stageWidth - rad;
                me.vx *= bounce;
            }
            else if(me.x - rad < 0)
            {
                me.x = rad;
                me.vx *= bounce;
            }
            if(me.y + rad > stage.stageHeight)
            {
                me.y = stage.stageHeight - rad;
                me.vy *= bounce;
            }
            else if(me.y - rad < 0)
                {
                    me.y = rad;
                    me.vy *= bounce;
                }
            if(me.z + rad > DEPTH)
                {
                    me.z = DEPTH - rad;
                    me.vz *= bounce;
                }
            else if(me.z - rad < 0)
            {
                me.z = rad;
                me.vz *= bounce;
            }
            
        }
    }
}

import flash.display.Sprite;
import flash.display.Graphics;
import flash.events.Event;

class particle extends Sprite
{
    public var color:uint;
    public var r:Number = 1;
    public var vx:Number = 0;
    public var vy:Number = 0;
    public var vz:Number = 0;
    public var mass:Number = 1;
    public var whiteColor:uint;
    public var toggleBlendMode:Boolean;

    public function particle( color:uint,toggleBlendMode:Boolean, r:Number=6 )
    {
        this.color = new uint();
        this.r = r;
        
        if( toggleBlendMode=true )
            {
              //  this.blendMode = "overlay";
            }
        if( toggleBlendMode=false )
            {
               // this.blendMode = "overlay";   
            }
        
        
        graphics.beginFill( color );
        graphics.drawCircle(0, 0, r);
        graphics.endFill();
        
    }
}