Math and (Quasi) Physics in Action Script 3

Utils3D.projectVectors

Test of projection of a three-dimensional object to two-dimensional space using Utils3D.projectVectors method.

See ‘Utils3D - Adobe ActionScript 3.0 Language and Components reference’ for details.

Main.as

´╗┐package {
  import flash.display.*;
  import flash.events.*;
  import flash.geom.*;
  public class Main extends Sprite {
    private var mtx:Matrix3D;
    private var    p3d:Vector.<Number>,
                p2d:Vector.<Number>,
                uvt:Vector.<Number>,
                idc:Vector.<int>;
    private var canvas:Sprite;
    
    public function Main() {
      mtx = new Matrix3D();
      p3d = Vector.<Number>([  -50, 0, -Math.sqrt(3) * 25,
              50, 0, -Math.sqrt(3) * 25,
              0, 0 , Math.sqrt(3) * 25,
              0,Math.sqrt(6)/1.5 * 50, -Math.sqrt(3) / 2 * 25 / 3
            ]);
      trace(p3d.toString());
      idc = Vector.<int>([0,1,2, 0,2,3, 1,0,3, 1,3,2]);
      p2d = new Vector.<Number>(Math.ceil(p3d.length / 3 * 2));   
      uvt = new Vector.<Number>(Math.ceil(p3d.length / 3 * 4));
      addChild(canvas = new Sprite());
      canvas.x = stage.stageWidth / 2;
      canvas.y = stage.stageHeight / 2;
      addEventListener(Event.ENTER_FRAME, h_enterFrame);
    }
    
    private function h_enterFrame(evt:Event) :void {
      var g:Graphics = canvas.graphics;
      
      mtx.appendRotation(1, Vector3D.X_AXIS);
      mtx.appendRotation(1, Vector3D.Y_AXIS);
      mtx.appendRotation(1, Vector3D.Z_AXIS);
      Utils3D.projectVectors(mtx, p3d, p2d, uvt);
      g.clear();
      g.lineStyle(1,0x000000,1);
      g.drawTriangles(p2d, idc, null);
    }
  }
}