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

flash on 2016-5-16

Get Adobe Flash player
by mutantleg 16 May 2016
    Embed
/**
 * Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/Sbij
 */

package {
    import flash.ui.Keyboard;
    import flash.events.KeyboardEvent;
    import flash.events.Event;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            myDraw = new xVertDraw();          
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKdown);
            stage.addEventListener(KeyboardEvent.KEY_UP, onKup);  
            stage.addEventListener(Event.ENTER_FRAME, onEnter);
        }//ctor

        public function onKdown(e:KeyboardEvent):void { vecKey[e.keyCode]=true; }
        public function onKup(e:KeyboardEvent):void { vecKey[e.keyCode]=false; }
        public function isKeyDown(k:int):Boolean { return vecKey[k]; }
        
        public var vecKey:Vector.<Boolean> = new Vector.<Boolean>(512,false); 

        public var cx:Number = 0;        public var cy:Number = 0;        public var cz:Number = 200;
        public var ang:Number = 0;
        
        public var gt:int = 0;
        
        public function onEnter(e:Event):void
        {

            mx = stage.mouseX; my = stage.mouseY;

            graphics.clear();
            graphics.lineStyle(2, 0);
            
             xVertDraw.setProjMat(projMat, 90, 1, 1, 6000);
             xVertDraw.setViewMat(viewMat, camx, camy, camz,  cori.x, cori.y, cori.z, cori.w);
             xVertDraw.multMatrix(projMat, viewMat, myDraw.projMat);

             graphics.drawRect(0,0,465,465);
             
              myDraw.drawBox3(graphics, 0,0,0, 64, 64,64);
              myDraw.drawBox3(graphics, 0,0,0, 264, 264,264);
              
              
              myDraw.drawVert(graphics, 0,0,0, 64);

             btnOver = -1;            
             btnw = -1;
              drawBtn(10, 128,64,0, 32);
              drawBtn(20, 128+64,64,0, 32);
              drawBtn(30, 128+64+64,64,0, 40);
              drawBtn(40, 128,0,0, 32);
              drawBtn(50, 128+64,0,0, 32);
              drawBtn(60, 128+64+64,0,0, 32);
              drawBtn(70, 128,-64,0, 32);
              drawBtn(80, 128+64,-64,0, 32);
              drawBtn(90, 128+64+64,-64,0, 32);


             var ax:Number; var az:Number;
             var ms:Number; ms =8; ax=0;az=0;
             
             cori.reset();
             cori.rotAxis(ang, 1);
             ax = cori.getFrontVx()*ms;
             az = cori.getFrontVz()*ms;
             
             
              if (isKeyDown(Keyboard.UP))    {  cx-=ax; cz-=az; }
              if (isKeyDown(Keyboard.DOWN))  {  cx+=ax; cz+=az; }
              if (isKeyDown(Keyboard.LEFT))  {  ang+=0.04; }
              if (isKeyDown(Keyboard.RIGHT)) {  ang-=0.04; } 
              
              camx=cx; camy=cy;camz=cz;
              

            gt +=1;
        }//onenter
        
        public var ident:Vector.<Number> = Vector.<Number>([1, 0, 0, 0, 0, 1, 0, 0,  0, 0, 1, 0, 0, 0, 0, 1]); 
        public var temp:Vector.<Number> = Vector.<Number>([1, 0, 0, 0, 0, 1, 0, 0,  0, 0, 1, 0, 0, 0, 0, 1]); 
        public var projMat:Vector.<Number> = Vector.<Number>([1, 0, 0, 0, 0, 1, 0, 0,  0, 0, 1, 0, 0, 0, 0, 1]); 
        public var viewMat:Vector.<Number> = Vector.<Number>([1, 0, 0, 0, 0, 1, 0, 0,  0, 0, 1, 0, 0, 0, 0, 1]); 
        
        public var mx:Number = 0; public var my:Number= 0;        
        public var btnOver:int = -1;
        public var btnw:Number =-1;
        public function drawBtn(id:int, ax:Number, ay:Number, az:Number, r:Number):void
        {
            graphics.lineStyle(2, 0);
            
          myDraw.drawVert(graphics, ax,ay,az, r);
          if (myDraw.btm < 0) { return; }
          
          var aw:Number; var ah:Number;
          aw = ah = myDraw.btm; 
          ax = myDraw.btx-aw;
          ay = myDraw.bty-ah;
          aw*=2; ah*=2;

          graphics.drawRect(ax, ay, aw, ah);          
            
          var over:Boolean;
          over = ((mx<ax)||(my<ay)||(mx>ax+aw)||(my>ay+ah)) == false;          
          if (btnOver > 0) { if (btnw > myDraw.btw) { return;}   }
          if (over) {
              btnOver = id; btnw = myDraw.btw;
          graphics.lineStyle(2, 0xFF0000);
             graphics.drawRect(ax, ay, aw, ah);          
               }//endif  
            
        }//checkpress

        
    }//classend
}

import flash.display.Graphics;

 var myDraw:xVertDraw;        
 var camx:Number = 0;  var camy:Number = 0;  var camz:Number = 1024;
 var cori:xQuat = new xQuat();
   



internal class xQuat
{
 public var x:Number = 0;        public var y:Number = 0;    
 public var z:Number = 0;        public var w:Number = 1;
 
  public function reset():void {    x = 0;  y = 0;  z = 0;  w = 1;   }

  public function normalise():void
  { var mag:Number;  mag = (x * x) + (y * y) + (z * z) + (w * w);
    if (mag == 1.0) return; //already normal
    if (mag == 0) { x = 0;  y = 0;  z = 0;  w = 1; return; }
    mag = 1.0 / Math.sqrt(mag);  x *= mag; y *= mag; z *= mag; w *= mag;
  }//norm
 
  public function rotAxis(ang:Number, xyz:int=0):void //0 x  1 y  2 z
  { var ax:Number; var ay:Number; var az:Number; var aw:Number;
    var tx:Number; var ty:Number; var tz:Number; var tw:Number;
    ang *= 0.5;
    ax = xyz == 0? Math.sin(ang):0; ay  = xyz == 1? Math.sin(ang):0;  
    az = xyz == 2? Math.sin(ang):0; aw = Math.cos(ang);
    tx = (w * ax + x * aw + y * az - z * ay);   ty = (w * ay + y * aw + z * ax - x * az);
    tz = (w * az + z * aw + x * ay - y * ax);   tw = (w * aw - x * ax - y * ay - z * az);
    x = tx;  y = ty;     z = tz;   w = tw;     
    normalise();        
  }//rotaxis
      
 public function setVec(vec:Vector.<Number>):void
  { var xx:Number= x * x;  var xy:Number= x * y; var xz:Number = x * z;  var xw:Number = x * w;
    var yy:Number= y * y;  var yz:Number= y * z; var yw:Number = y * w;
    var zz:Number= z * z;  var zw:Number= z * w;
    vec[3]  = vec[7] = vec[11] = vec[12] = vec[13] = vec[14] = 0.0; vec[15] = 1.0;
    vec[0]  = 1.0 - 2.0 * ( yy + zz ); vec[4]  =     2.0 * ( xy - zw );   vec[8]  =     2.0 * ( xz + yw );
    vec[1]  =  2.0 * ( xy + zw ) ;     vec[5]  = 1.0 - 2.0 * ( xx + zz ); vec[9]  =  2.0 * ( yz - xw ) ;
    vec[2]  =     2.0 * ( xz - yw );   vec[6]  =     2.0 * ( yz + xw );   vec[10] = 1.0 - 2.0 * ( xx + yy );       
 }//setvec
 
 
  public function lookAtXYZ(ax:Number, ay:Number, az:Number,  bx:Number, by:Number, bz:Number):void  
  { var yaw:Number;  var pitch:Number; var dx:Number;  var dz:Number; var am:Number;
    x = 0;  y = 0;  z = 0;  w = 1;
    dx = bx - ax;      dz = bz - az;
    yaw = Math.atan2(dz, dx);
    am = Math.cos(yaw) * dx + Math.sin(yaw) * dz;
    pitch = Math.atan2(by - ay, am);
    rotAxis( -yaw - 1.57, 1);
    rotAxis(pitch, 0);
  }//lookat  
      
      public function getSideVx():Number        { return  1.0 - 2.0 * ( y * y + z * z ); }        
      public function getSideVy():Number        { return  2.0 * ( x * y + z * w ); }    
      public function getSideVz():Number        { return  2.0 * ( x * z - y * w ); }
      public function getUpVx():Number          { return  2.0 * ( x*y - z*w ); }
      public function getUpVy():Number          { return  1.0 - 2.0 * ( x*x + z*z ); }    
      public function getUpVz():Number          { return  2.0 * ( y*z + x*w ); }
      public function getFrontVx():Number        {    return  2.0 * ( x*z + y*w ); }        
      public function getFrontVy():Number        { return  2.0 * ( y*z - x*w );     }        
      public function getFrontVz():Number        {    return  1.0 - 2.0 * ( x*x + y*y );  }
  
 
}//xquat

internal class xVertDraw 
{   
  public var projMat:Vector.<Number> = Vector.<Number>([1, 0, 0, 0, 0, 1, 0, 0,  0, 0, 1, 0, 0, 0, 0, 1]); 
  public var scrw:Number = 465;
  public var scrh:Number = 465;
  public var rendw:Number = 465 * 0.5;
  public var rendh:Number = 465 * 0.5;  
  
  public function xVertDraw()
  {    setProjMat(projMat, 90, 1, 1, 1000);  }//ctor  
  
  
  
    public var btx:Number = 0;
    public var bty:Number = 0;
    public var btm:Number = -1;    
    public var btw:Number = -1;    
      
    public function drawVert(g:Graphics, ax:Number, ay:Number, az:Number, rad:Number):void
    {
     var mat:Vector.<Number>;    var m:Number;
     var sx:Number;    var sy:Number;   var sz:Number;
     var w:Number; 
        mat = projMat;
       
        w = ax * mat[3] + ay * mat[7] + az * mat[11] + mat[15];
        if (w<0) { btm=-1; btw=-1; return; }
        
        sx = ax * mat[0] + ay * mat[4] + az * mat[8] + mat[12];
        sy = ax * mat[1] + ay * mat[5] + az * mat[9] + mat[13];
        sz = ax * mat[2] + ay * mat[6] + az * mat[10] + mat[14];
       
        sx /= w; sy /= w;
        sx *= rendw;            sy *= -rendh;
        sx += rendw;            sy += rendh;
            
        m = (1 / w) * rendh * rad; 
            
        g.drawCircle(sx, sy, m);
        
        btx=sx; bty=sy; btm=m;
        btw = (1 / w) * rendh;
    }//drawvert
  
  
 

    public function drawBox3(g:Graphics, ax:Number, ay:Number, az:Number,
    rx:Number, ry:Number, rz:Number):void
    { drawBox(g, ax-rx, ay-ry, az-rz,   ax + rx, ay + ry, az + rz); }
  
    public function drawBox2(g:Graphics, ax:Number, ay:Number, az:Number,
    aw:Number, ah:Number, ad:Number):void
    { drawBox(g, ax, ay, az,   ax + aw, ay + ah, az + ad); }
  
    public function drawBox(g:Graphics, minx:Number, miny:Number, minz:Number,
      maxx:Number, maxy:Number, maxz:Number):void
        {
          //bottom
          drawLine2(g, minx, miny, minz,   maxx, miny, minz);
          drawLine2(g, minx, miny, maxz,   maxx, miny, maxz);
          drawLine2(g, minx, miny, minz,   minx, miny, maxz);
          drawLine2(g, maxx, miny, minz,   maxx, miny, maxz);  
          
          //top
          drawLine2(g,minx, maxy, minz,   maxx, maxy, minz);
          drawLine2(g,minx, maxy, maxz,   maxx, maxy, maxz);
          drawLine2(g,minx, maxy, minz,   minx, maxy, maxz);
          drawLine2(g, maxx, maxy, minz,   maxx, maxy, maxz);
           
          drawLine(g,minx, miny, minz,   minx, maxy, minz);
          drawLine(g,maxx, miny, minz,   maxx, maxy, minz);
          drawLine(g,minx, miny, maxz,   minx, maxy, maxz);
          drawLine(g,maxx, miny, maxz,   maxx, maxy, maxz);
  
        }//drawbox

    public function drawRect3(g:Graphics, ax:Number, ay:Number, az:Number,
    rx:Number, ry:Number, rz:Number):void
    { drawRect(g, ay, ax-rx,  az-rz,   ax + rx,  az + rz); }

   public function drawRect(g:Graphics, ay:Number, minx:Number, minz:Number, maxx:Number, maxz:Number):void
   {
      drawLine2(g,minx, ay, minz,   maxx, ay, minz);
          drawLine2(g,minx, ay, maxz,   maxx, ay, maxz);
          drawLine2(g,minx, ay, minz,   minx, ay, maxz);
          drawLine2(g, maxx, ay, minz,   maxx, ay, maxz);      
    }//drawrect
        
        
  public function drawLine2(g:Graphics, ax:Number, ay:Number, az:Number,
  bx:Number, by:Number, bz:Number):void
  {  
    var mat:Vector.<Number>;  var w0:Number, w1:Number;
    mat = projMat;
    w0 = ax * mat[3] + ay * mat[7] + az * mat[11] + mat[15];
    w1 = bx * mat[3] + by * mat[7] + bz * mat[11] + mat[15];
    if (w0 <= 0 && w1 <= 0) { return; }
    if (w0 > 0 && w1 > 0) { drawLine(g, ax, ay, az, bx, by, bz); return; }
    var u:Number; var t:Number;
    var kx:Number; var ky:Number; var kz:Number;
    kx = (bx - ax); ky = (by - ay); kz = (bz - az);
    t = 0.2;
    for (u = 0; u < 1; u += t)
    {  drawLine(g, ax + kx * u, ay + ky * u, az + kz * u,
       ax + kx * (u + t), ay + ky * (u + t), az + kz * (u + t));  }
  }//dline2
  
     
  public function drawLine(g:Graphics, ax:Number, ay:Number, az:Number,
  bx:Number, by:Number, bz:Number):void
  {
    var mat:Vector.<Number>;
    var sx0:Number, sy0:Number, sz0:Number, w0:Number; 
    var sx1:Number, sy1:Number, sz1:Number, w1:Number; 
     mat = projMat;
    

    w0 = ax * mat[3] + ay * mat[7] + az * mat[11] + mat[15];
    w1 = bx * mat[3] + by * mat[7] + bz * mat[11] + mat[15];
    if (w0 <= 0 && w1 <= 0) { return; }

    sx0 = ax * mat[0] + ay * mat[4] + az * mat[8] + mat[12];
    sy0 = ax * mat[1] + ay * mat[5] + az * mat[9] + mat[13];
    //sz0 = ax * mat[2] + ay * mat[6] + az * mat[10] + mat[14];

    sx1 = bx * mat[0] + by * mat[4] + bz * mat[8] + mat[12];
    sy1 = bx * mat[1] + by * mat[5] + bz * mat[9] + mat[13];
    //sz1 = bx * mat[2] + by * mat[6] + bz * mat[10] + mat[14];
 
    sx0 /= w0; sy0 /= w0;

    //projection fix
    if (w0 < 0.0)
    { if (sx0 > 0) {  sx0 = -1.0 - sx0;  } else { sx0 = 1.0 - sx0; }
      if (sy0 > 0) {  sy0 = -1.0 - sy0; }  else { sy0 = 1.0 - sy0; }          
    }//endif  
    sx0 *= rendw;  sy0 *= -rendh;    sx0 += rendw;  sy0 += rendh;
    
    sx1 /= w1; sy1 /= w1;
    //projection fix
    if (w1 < 0.0)
    { if (sx1 > 0) {  sx1 = -1.0 - sx1;  } else { sx1 = 1.0 - sx1; }
      if (sy1 > 0) {  sy1 = -1.0 - sy1; }  else { sy1 = 1.0 - sy1; }          
    }//endif  
    sx1 *= rendw;  sy1 *= -rendh;    sx1 += rendw;  sy1 += rendh;    
    
    //my pathetic attempt at optimisation
    if (sx0 < 0 && sx1 < 0) { return; }    if (sy0 < 0 && sy1 < 0) { return; }
    if (sx0 > scrw && sx1 > scrw) { return; }  if (sy0 > scrh && sy1 > scrh) { return; }

    g.moveTo(sx0, sy0);    g.lineTo(sx1, sy1);        
  }//drawline  

    public static function setProjMat(vec:Vector.<Number>,
     fovdeg:Number = 60.0, aspect:Number=1.0, nearp:Number = 1.0, farp:Number=1000.0):void
    {   var f:Number;   var i:int;
        for (i = 0; i < 16; i++) { vec[i] = 0.0;  } 
        f = 1.0 / Math.tan( (fovdeg * (3.1415 / 180.0)) * 0.5 );
        if (nearp == 0) { nearp = 0.0001; }
        if (farp == 0) { farp = 0.0001; }
        vec[0] = f / aspect;          vec[5] = f;
        vec[10] = (farp + nearp) / (nearp - farp);
        vec[14] = (2.0 * farp * nearp) / (nearp - farp);
        vec[11] = -1.0;          vec[15] = 0.0;
    }//projmatrix
    
    public static function setViewMat(vec:Vector.<Number>, 
        cx:Number, cy:Number, cz:Number,  qx:Number, qy:Number, qz:Number, qw:Number):void
        { var forwx:Number; var forwy:Number; var forwz:Number;
          var sidex:Number; var sidey:Number; var sidez:Number;
          var upx:Number;   var upy:Number;   var upz:Number;     
          var i:int; for(i=0;i<16;i++){vec[i]=0.0;}vec[0]=vec[5]=vec[10]=vec[15]=1.0;
          forwx=2.0*(qx*qz+qy*qw);forwy=2.0*(qy*qz-qx*qw);forwz=1.0-2.0*(qx*qx+qy*qy);
          upx=2.0*(qx*qy-qz*qw);upy=1.0-2.0*(qx*qx+qz*qz);upz=2.0*(qy*qz+qx*qw);
          sidex=1.0-2.0*(qy*qy+qz*qz);sidey=2.0*(qx*qy+qz*qw);sidez=2.0*(qx*qz-qy*qw);
          vec[0]=sidex;vec[4]=sidey;vec[8]=sidez;
          vec[1]=upx;vec[5]=upy;vec[9]=upz;
          vec[2]=forwx;vec[6]=forwy;vec[10]=forwz;
          vec[12]=(sidex*-cx)+(sidey*-cy)+(sidez*-cz);
          vec[13]=(upx*-cx)+(upy*-cy)+(upz*-cz);
          vec[14]=(forwx*-cx)+(forwy*-cy)+(forwz*-cz);
        }//setlookat

        //only works with 4x4 matrices
        public static function multMatrix(a:Vector.<Number>, b:Vector.<Number>, r:Vector.<Number>):void
        {   var i:int;  for (i = 0; i < 16; i += 4)
            {
                r[i] =     a[0] * b[i] + a[4] * b[i + 1] + a[8] * b[i + 2] + a[12] * b[i + 3];
                r[i + 1] = a[1] * b[i] + a[5] * b[i + 1] + a[9] * b[i + 2] + a[13] * b[i + 3];
                r[i + 2] = a[2] * b[i] + a[6] * b[i + 1] + a[10] * b[i + 2] + a[14] * b[i + 3];
                r[i + 3] = a[3] * b[i] + a[7] * b[i + 1] + a[11] * b[i + 2] + a[15] * b[i + 3];
            }//nexti
        }//multmat  
    
}//vertdraw