Graphics Programs Reference
In-Depth Information
When these values are multiplied by 300 and then 300 is subtracted from the result,
the values -300, 0, 300 are created. Using the modulo (%) function is a handy way
of organizing multiple objects into some kind of structured formation.
Now let's look at the
displayObj()
function. Since we have now made the viewer an
integral part of the 3D world, we will need to measure relative distances from the view-
er to the objects. This is done by taking the coordinates of each object and subtracting
the coordinates of the viewer (lines 39-41). After that has been determined, we are
able to calculate the perspective ratio
pRatio
. This is equivalent to the distance ratio
that we have used previously, the only difference being that we are now dealing with
relative coordinates.
35
36
37
38
39
40
41
42
43
44
45
// display the 3d objects in the picture plane
function displayObj()
{
// calculate the relative translation
var tx:Number = thisObj.x - viewer.x;
// tx,ty,tz are the
var ty:Number = thisObj.y - viewer.y;
// object's coordinates
var tz:Number = thisObj.z - viewer.z;
// relative to viewer
// calculate the perspective ratio
var pRatio:Number = d/tz;
Once the perspective ratio has been calculated, we can then easily determine the loca-
tion of an object by multiplying the relative distances by the perspective ratio. This
can be seen in Figure 7.6 with an object at point P(x1,y1,z1) and the viewer at point
P(x2,y2,z2). Using similar triangles as we did in Chapter 6, we find that
ys/d = ty/tz
and then multiplying both sides by
d
gives
ys = ty*(d/tz)
.
Figure 7.6 also shows that the perspective ratio
d/tz
will have a value of 1 when
tz = d
and will have a value less than 1 when
tz > d
. Multiplying the perspective ratio by 100
will give the correct scale.
46
47
48
49
50
// apply the perspective ratio to the location and size
thisObj._x = tx * pRatio;
thisObj._y = -ty * pRatio;
// minus sign for Flash coords
thisObj._xscale = thisObj._yscale = 100 * pRatio;
Search WWH ::
Custom Search