flash on 2012-7-19

by mutantleg
♥0 | Line 102 | Modified 2012-09-27 21:01:33 | MIT License
play

ActionScript3 source code

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

package {
    import flash.events.Event;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        
        public var vecGlob:Vector.<aGlob>;
        public var vecSpring:Vector.<aSpring>;
        
        public function FlashTest() {
            // write as3 code here..
            
            vecGlob = new Vector.<aGlob>;
            vecSpring = new Vector.<aSpring>;
            
            var g:aGlob;
            var s:aSpring;
            var i:int;
            var k:int;
            
            /*
            vecGlob.push( new aGlob(200, 100) );
            vecGlob.push( new aGlob(200, 120) );
            vecGlob.push( new aGlob(200, 140) );
            vecGlob.push( new aGlob(200, 160) );
            
            vecSpring.push( new aSpring(vecGlob[0], vecGlob[1]) );
            vecSpring.push( new aSpring(vecGlob[1], vecGlob[2]) );
            vecSpring.push( new aSpring(vecGlob[2], vecGlob[3]) );
            */
            for (i =0; i< 32; i++)
            {
                vecGlob.push(new aGlob(200,10+i*3) );
            }//nexti
            
            vecGlob[0].mass = 1000;
            vecGlob.push(new aGlob(200, 100) );
            //vecGlob[32].mass = 1000;
            
            for ( i=1; i < 32; i++)
            {
                vecSpring.push(new aSpring(vecGlob[i-1],vecGlob[i]) );
            }//nexti
            vecSpring.push(new aSpring(vecGlob[31], vecGlob[32]) );
            
            
            stage.addEventListener(Event.ENTER_FRAME, onEnter);
        }//ctor
        
        public function onEnter(e:Event):void
        {
            graphics.clear();
            graphics.lineStyle(1, 0);
            
            var i:int;
            var num:int;
            var g:aGlob;
            var s:aSpring;
            var k:Number;
            var it:Number;
            var d:Number;
            it = 100;
            d = 1 / (it/2);
            
            
           
            for (k = 0; k < it; k++)
            {
                 g = vecGlob[0];
            g.cx = mouseX;
            g.cy = mouseY;
            g.vx = 0;
            g.vy = 0;
            
                num = vecGlob.length;
                for (i = 1; i < num; i++)
                {
                    g = vecGlob[i];
                    //g.update(d);
                    g.grav(d);
                    
                    //graphics.drawCircle(g.cx, g.cy, 8);
                }//nexti
                
                num = vecSpring.length;
                for (i = 0; i < num; i++)
                {
                   s = vecSpring[i];
                   s.update(d);
                   
                   //graphics.moveTo(s.a.cx, s.a.cy);
                   //graphics.lineTo(s.b.cx, s.b.cy);
                }//nexti
                
                num = vecGlob.length;
                for (i = 1; i < num; i++)
                {
                    g = vecGlob[i];
                    g.update(d);
                 }
                
            }
            
            num = vecGlob.length;
            for (i = 0; i < num; i++)
            {
                g = vecGlob[i];
               // graphics.drawCircle(g.cx, g.cy, 8);
            }//nexti            
            
            num = vecSpring.length;
            for (i = 0; i <num;i++)
            {
                s   = vecSpring[i];
                 graphics.moveTo(s.a.cx, s.a.cy);
                   graphics.lineTo(s.b.cx, s.b.cy);
            }
            
            
        }//onenter
        
    }
}

internal class aGlob
{
    public var cx:Number = 0;
    public var cy:Number = 0;
    public var ox:Number = 0;
    public var oy:Number = 0;
    public var vx:Number = 0;
    public var vy:Number = 0;
    public var mass:Number = 5.0;
    
    public function aGlob(x:Number=0,y:Number=0):void
    {
     cx = x;
     cy = y;   
    }//ctor
    
    public function grav(d:Number=1.0):void
    {
        vy += 1.225*d;
        
    }
    
    public function update(d:Number=1.0):void
    {
        //vy += 1.225;//*d;
       // if (vy > 4) { vy = 4; }
        
        vx *= 0.99;
        vy *= 0.99;
        
        ox = cx;
        oy = cy;
        cx += vx*d;
        cy += vy*d;
        
        
        if (vy > 0 && cy >= 300) { cy= 300; vy = 0;}
        
    }//update
    
 };//glob
 
 internal class aSpring
 {
     public var a:aGlob;
     public var b:aGlob;
     
     public function aSpring(a_:aGlob, b_:aGlob):void
     {
         a = a_;
         b = b_;
     }//ctor
     
     public function update(d:Number=1.0):void
     {
         if (a == null || b == null) { return; }
         
         var mag:Number;
         var dx:Number;
         var dy:Number;
         var nx:Number;
         var ny:Number;
         var dist:Number;
         dist = 6;
         
         dx = a.cx - b.cx;
         dy = a.cy - b.cy;
         mag = Math.sqrt(dx*dx + dy*dy);
         if (mag < dist) { return; }
         //if (mag > 20) { mag = 20;}
         
         nx = dx / mag;
         ny = dy / mag;
         
         var ax:Number;
         var ay:Number;
         ax = a.vx - b.vx;
         ay = a.vy - b.vy;
         var relvel:Number;
         relvel = (ax*nx) +(ay*ny);
         
         
        var reld:Number;
                 reld = mag - 20;
                 var rem:Number;
                 rem = relvel + (reld / d);
                 rem = relvel + reld;
                 var imp:Number;
                 imp = rem / (1/a.mass + 1/b.mass);
                 //imp *= 0.99;
                 imp *= 0.5;
                 //imp *= d;

         
         //nx *= 0.7;
         //ny *= 0.7;
         
         a.vx += (imp *(nx*-0.5  )) / a.mass; 
         a.vy += (imp *(ny*-0.5  )) / a.mass;
         b.vx += (imp*(nx*0.5  )) / b.mass;
         b.vy += (imp*(ny*0.5 )) / b.mass;
         /*
         a.cx += nx * -0.5 * d;
         a.cy += ny * -0.5 * d;
         
         b.cx += nx * 0.5 * d;
         b.cy += ny * 0.5 * d;
         */
     }//update
     
 };//spring