Datasheet

12
Part 1: Getting Started
import flash.display.Sprite;//imports sprite class
var zposition:Number = 0;//z position
var myAngle:Number =0;//Angle of ball
var fl:Number = 300; //focal length
var ball:Sprite = new Sprite();//instantiates ball sprite
ball.graphics.beginFill(0xFF0000);//Assign a ball color
ball.graphics.drawCircle(0, 0, 40);//draws your ball at 0,0
ball.graphics.endFill();//ends the fill
addChild(ball);//adds the ball to the stage
addEventListener(Event.ENTER_FRAME, onEnterFrame);//loops equations
function onEnterFrame(event:Event):void{
var scale:Number = fl / (fl + zposition);//scale perspective
myAngle=myAngle+.1;//iterates angle
myB all.x = 300*Math.sin(myAngle)*scale; //ball orbit x
ball.y = 300*Math.cos(myAngle)*scale; //ball orbit y
ball.scaleX = scale;//scales perspective in x
ball.scaleY = scale;//scales perspective in y
zposition = 2000*Math.sin(myAngle/10);} //increments z and changes sign
The code, though it s not Papervision3D, illustrates a number of important concepts that every 3D
engine possesses:
A frame looper/renderer
Perspective (z - coordinate)
Projection onto a viewport
Primitive or basic shape
Addition of a color (or material)
And all of this is done in just 19 lines of code. If only it had stayed this simple. Papervision3D started off
with only 20 classes, now it s in the hundreds and growing. But as they say, no pain no gain.
Vanishing Point (Asymptotic Zero)
As your animation runs, the spiraling red ball approaches its vanishing point (asymptotic zero), which
as mentioned earlier, is the Flash origin located at (0, 0) or upper left position of your screen. This is a
little annoying, but can be easily adjusted by moving your vanishing point. This is done by adding
a vanishing point coordinate value to your ball x and y positions. To center the vanishing point to the
Flash stage you must import the stage class and use
stage.stageWidth/2 (which returns the x center
point) and
stage.stageHeight/2 (which returns the y centerpoint) as shown here:
ball.x = 300*Math.sin(myAngle)*scale+stage.stageWidth/2;
ball.y = 300*Math.cos(myAngle)*scale+stage.stageHeight/2;
The results of this change are shown in Figure 1.6.
c01.indd 12c01.indd 12 12/14/09 3:03:26 PM12/14/09 3:03:26 PM