topical media & game development

talk show tell print

#graphic-flex-image-effects-06-Flex-SimpleVector3DTest.ax

#graphic-flex-image-effects-06-Flex-SimpleVector3DTest.ax [swf] [flash] flex


  package {
  
          import flash.display.Sprite;
          import flash.geom.Vector3D;
          import flash.geom.Point;
  
          [SWF(width=550, height=400, backgroundColor=0xFFFFFF)]
  
          
Demonstrates simple vector addition and subtraction, and how vectors may be represented on the stage through the drawing API.

  
          public class @ax-graphic-flex-image-effects-06-Flex-SimpleVector3DTest extends Sprite {
  
                  public function @ax-graphic-flex-image-effects-06-Flex-SimpleVector3DTest() {
                          // center sprite
                          x = stage.stageWidth/2;
                          y = stage.stageHeight/2;
  
                          // this vector will represent the center of the 3D space;
                          // note that all z depths must be positive so that they will 
                          // be drawn with a positive scale (just for this class)
                          var center:Vector3D = new Vector3D(0, 0, 20);
                          drawVector(center, 0x000000);
  
                          var vector0:Vector3D = new Vector3D(50, 50, 50);
                          var vector1:Vector3D = new Vector3D(100, 100, 100);
                          // add two vector positions for third vector
                          var vector2:Vector3D = vector0.add(vector1);
  
                          drawVector(vector0, 0xFF0000);
                          drawVector(vector1, 0x0000FF);
                          drawVector(vector2, 0xFF00FF);
  
                          vector0 = new Vector3D(-50, -50, 150);
                          // subtract second vector from first for new vector
                          vector2 = vector0.subtract(vector1);
  
                          drawVector(vector0, 0x00FF00);
                          drawVector(vector2, 0x00FFFF);
                  }
  
                  
Draws a vector point on the stage.
parameter: vector The vector to draw.
parameter: color The color to use to represent the vector.

  
                  private function drawVector(vector:Vector3D, color:uint):void {
                          // convert to 2D coordinates
                          var point:Point = local3DToGlobal(vector);
                          // convert to this sprite's coordinate space
                          point = globalToLocal(point);
                          trace(vector, point);
  
                          graphics.beginFill(color);
                          // size of circle is determined by z depth;
                          // for this reason, in this class, all vector should be in positive z quadrant
                          graphics.drawCircle(point.x, point.y, 20*vector.z/100);
                          graphics.endFill();
                  }
  
          }
  
  }


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.