Graphics Programs Reference
In-Depth Information
The answer lies in the expression for the distance ratio dr = d/(d + thisObj.z) .
As long as the z - value of a fish is greater than -d so that d + thisObj.z is a positive
number, everything is fine, because the fish is in front of the viewer.
When the z - value of a fish is less than -d, the fish is behind the viewer, and then dr
becomes a negative number. Since the screen coordinates and the size of the fish are
based on dr , this causes a reverse in the direction and the scale factors of the fish.
What happens is that, like a lens, the image gets inverted both horizontally and verti-
cally, and then its size decreases as it moves away from you. So, unless you are after
an effect where the object seems to bounce off a wall and then go back in space, we
will need to add a test that prevents this from happening.
Step 3: Check whether an object is too close to the viewer
The test to see whether an object is too close to the viewer is straightforward. There
are two decisions we need to make. The first is to specify what we mean by “too close.”
It could mean that an object has reached the viewer so that its z - value is equal to the
negative of the viewer distance. Or it could mean that an object is sufficiently close to
“in my face” without actually being there. We will specify the latter with 30 pixels being
close enough.
The other decision is what to do with the object when it does get too close. That very
much depends on what we want the user experience to be. For our unruly school of
fish, we will remove any that swim past us. In other situations, often in games, the ob-
ject gets relocated back in 3D space and keeps moving forward. Add the following lines
to the displayObj() function. Save your movie as 6_2_bellyUp_DONE.fla and test it.
Hopefully no more belly up.
48
49
50
51
52
53
54
55
56
57
// set the depth based on its z-location so that
// closer objects are on top of farther objects
thisObj.swapDepths(Math.round(-thisObj.z));
// check if the object is too close to the viewer
if ( thisObj.z <= -d + 30)
{
thisObj.removeMovieClip();
}
}
 
Search WWH ::




Custom Search