package { import flash.display.Stage; import flash.display.MovieClip; import flash.display.Shape; import flash.display.Sprite; import flash.events.MouseEvent; import flash.events.Event; // import flash.media.Sound; // import flash.events.MouseEvent; // import flash.display.Sprite; // import flash.display.Bitmap; // import flash.display.BitmapData; // import flash.display.Shape; // import flash.display.MovieClip; // import flash.display.Stage; // import flash.display.StageQuality; // import flash.events.MouseEvent; // import flash.display.SpreadMethod; // import flash.text.TextField; // import flash.text.TextFieldAutoSize; // import flash.display.LoaderInfo; // import flash.display.InterpolationMethod; public class Base_Script extends Sprite { var spBoard:Sprite; var shBack:Shape; var shCube:Shape; public function Base_Script() // constructor { stage.addEventListener( Event.ENTER_FRAME , init ); } public function init ( event:Event ):void { stage.removeEventListener( Event.ENTER_FRAME , init ); // We define a new Sprite, spBoard, in which all dynamic // drawings will reside. We are adding spBoard to the // Display List and positioning it within the main movie. spBoard = new Sprite(); this.addChild(spBoard); //addChild(spBoard); //spBoard.x=200; //spBoard.y=190; //spBoard.x=100; //spBoard.y=100; spBoard.x=150; spBoard.y=150; // We will draw dynamically a background for spBoard. // The background will be drawn in its child, shBack. // var shBack = new Shape(); // caused an error in: function drawBack() shBack = new Shape(); spBoard.addChild(shBack); // We are calling a function that will draw a background // in spBoard. drawBack(); // Our cube will be drawn in a child of spBoard called shCube. shCube = new Shape(); spBoard.addChild(shCube); // We are calling the functions that define the vertices // and the faces of the cube in 3D space, and then the // function that renders an initial view. The three // functions are defined and described below. setVertices(); setFaces(); renderView(curTheta,curPhi); // We add listeners to spBoard that allow the user // to rotate the cube. The MOUSE_MOVE listener calls // renderView function with the values of t and p // that correspond to the horizotal and vertical // changes in the position of the mouse. spBoard.addEventListener(MouseEvent.ROLL_OUT,boardOut); spBoard.addEventListener(MouseEvent.MOUSE_MOVE,boardMove); spBoard.addEventListener(MouseEvent.MOUSE_DOWN,boardDown); spBoard.addEventListener(MouseEvent.MOUSE_UP,boardUp); }// bottom of function: "init" // vertsArray will store vertices of our cube. var vertsArray:Array=[]; var facesArray:Array=[]; // fLen variable stores the distance from the camera to the // origin of the xyz-coordinate system. The origin coincides // with the center of the cube. Large values for fLen produce // less distortion for perspective, smaller values more // perspective. Anything below 200 is too close to the // surface and will give undesirable effects. var fLen:Number=2000; // The next two variables store the number of vertices // and the number of polygonal faces for our surface. // Since we are drawing a cube, the values are 8 and 6. var numVertices:uint=8; var numFaces:uint=6; // doRotate remembers if the user is rotating the cube // with the mouse or not. prevX, prevY store the latest // mouse coordinates. curPhi, curTheta store the current // view angles, faceColors the colors of the cube's sides. var doRotate:Boolean=false; var prevX:Number; var prevY:Number; var curTheta:Number=20; var curPhi:Number=70; //var facesColors:Array=[0xFFFFCC,0x00FF66,0x0066FF, // 0x33FFFF,0x9A7DDF,0xFFCCFF]; var facesColors:Array=[0xCCCCFF,0xCC99FF,0x9966FF, 0x9933FF,0x6633FF,0x6600FF]; // -------------------------------------------------------- // // The function that draws the back of spBoard. // function drawBack():void { //shBack.graphics.beginFill(0x000000); shBack.graphics.beginFill(0x000000, 0.0); // a transparent fill // shBack.graphics.drawRect(-160,-160,320,320); //shBack.graphics.drawRect(-125,-125,300,300); shBack.graphics.drawRect(-200,-200,350,350); shBack.graphics.endFill(); } // The function setVertices populates the array of vertices, // vertsArray, with three-elements arrays. Each three-element // array represents x, y, and z coordinates of a vertex // in 3D space. The units are pixels. The origin is in // the center of spBoard. The x-axis is perpendicular to // the computer screen pointing away from the screen, // the y-axis lies on the horizontal plane and points to // the right, the z-axis is vertical and points upward. // Thus, we are in the standard xyz-coordinate system and // the observer is located on the positive x-axis fLen // units from the origin. We will translate the coordinates // to Flash's coordinate system after moving the observer // in 3D and projecting the cube onto the new view plane. // -------------------------------------------------------- // function setVertices():void { vertsArray[0] = [70,-70,70]; vertsArray[1] = [70,70,70]; vertsArray[2] = [-70,70,70]; vertsArray[3] = [-70,-70,70]; vertsArray[4] = [70,-70,-70]; vertsArray[5] = [70,70,-70]; vertsArray[6] = [-70,70,-70]; vertsArray[7] = [-70,-70,-70]; } // The setFaces function populates the array of faces, // facesArray. Each face is defined as a list of vertices // that will be joined, in the order they appear, by // lineal elements to create the face. Each vertex is // referred to by its index in the vertsArray. Since we // are drawing a cube, each face has four vertices. // -------------------------------------------------------- // function setFaces():void { facesArray[0] = [0,4,5,1]; facesArray[1] = [1,5,6,2]; facesArray[2] = [2,6,7,3]; facesArray[3] = [3,7,4,0]; facesArray[4] = [4,5,6,7]; facesArray[5] = [0,1,2,3]; } // The next function rotates the positon of the observer // horizontally by the angle t, and vertically by the // angle phi. Both angles are measured in degrees. t measured // counterclockwise from the positive x-axis. p is the // angle between the positive z-axis and the new position // vector of the observer (called the new view vector). // // In other words, t is the theta coordinate in spherical // coordinates and p is the phi coordinate in spherical // coordinates of the new position of the observer. // The new view plane is perpendicular to the new view vector // and passes through the origin. We assume that there // is no rotation about the new view vector. See the pictures // on the previous html page of this tutorial. The pictures // illuminate the process of moving of the observer. // // Mathematically, we choose a new 3D coordinate system // such that the new view vector (normalized) is the new // first coordinate. We choose an orthogonal coordinate // system on the view plane such that the old positve // z-axis projects onto the positive vertical axes on the // new view plane and the new coordinate system is // right-handed. // -------------------------------------------------------- // function renderView(t:Number,p:Number):void { //We define local variables whose meaning will become //clear below. var i:int; var ii:int; var distArray=[]; var dispArray=[]; var vertsNewArray=[]; var midPoint:Array=[]; var dist:Number; var curFace:uint; //We convert t and p into radians. t=t*Math.PI/180; p=p*Math.PI/180; //We clear the previous view of the cube. shCube.graphics.clear(); for(ii=0; ii w[0]) { return -1; } else if (v[0] < w[0]) { return 1; } else { return 0; } } // -------------------------------------------------------- // function pointNewView(v:Array,theta:Number,phi:Number):Array { var newCoords:Array=[]; newCoords[0]=v[0]*Math.cos(theta)*Math.sin(phi)+ v[1]*Math.sin(theta)*Math.sin(phi)+ v[2]*Math.cos(phi); newCoords[1]=-v[0]*Math.sin(theta)+v[1]*Math.cos(theta); newCoords[2]=-v[0]*Math.cos(theta)*Math.cos(phi)- v[1]*Math.sin(theta)*Math.cos(phi)+ v[2]*Math.sin(phi); return newCoords; } // -------------------------------------------------------- // function boardOut(ee:MouseEvent):void { doRotate=false; } // -------------------------------------------------------- // function boardDown(ee:MouseEvent):void { prevX = spBoard.mouseX; prevY = spBoard.mouseY; doRotate = true; } // -------------------------------------------------------- // function boardUp(ee:MouseEvent):void { doRotate=false; } // -------------------------------------------------------- // function boardMove(ee:MouseEvent):void { var locX:Number = prevX; var locY:Number = prevY; if(doRotate) { prevX = spBoard.mouseX; prevY = spBoard.mouseY; curTheta += (prevX - locX); curPhi += (prevY - locY); renderView( curTheta, curPhi ); ee.updateAfterEvent(); } } } // bottom of class: "Base_Script" } // end of package