Graphics Programs Reference
In-Depth Information
A similar process is used to provide y- and z-coordinates. Each time the random func-
tion is called, a new number will be generated so that x, y, and z will have different
random values and will have different values for each fish. This will give us a different
location in 3D space for each fish.
Step 4: Display each object
Now that we have positioned each fish in 3D space, we need to convert those coordi-
nates to screen coordinates in order to display it. That task is given to the displayObj()
function. To get the screen coordinates, we first calculate the distance ratio based on
the object's z - value as shown in line 32.
29
30
31
32
33
function displayObj()
{
// calculate the distance ratio dr
var dr:Number = d/ (d + thisObj.z);
The object's screen coordinates (xs, ys) are then computed in lines 35 and 36. Finally,
to position the ball in Flash coordinates, we use lines 41 and 42. Recall that for Flash,
the (0,0) point of the Stage is in the upper-left corner and that the positive y-axis points
down instead of up.
34
35
36
37
38
39
40
41
42
43
// calculate the screen coordinates xs, ys
var xs:Number = thisObj.x * dr;
var ys:Number = thisObj.y * dr;
// set the location of the object on the stage
// note the minus sign for y is because the y-axis
// in Flash points down instead of up
thisObj._x = xo + xs;
thisObj._y = yo - ys;
There is one remaining thing that we must consider. We have discussed how objects
that are farther away from us appear smaller. How can we take this fact into account
when working in 3D? The answer lies in the distance ratio. When an object is at z =
0 so that dr = 1, it seems natural to want to see the object at actual size. When an
object is infinitely far away from us so that dr = 0, then the object size should be 0.
Suppose an object is located at a distance ratio of 1:2. We might reasonably expect
that the object will appear to be half of its original size. To get an appropriate size for
Search WWH ::




Custom Search