Graphics Programs Reference
In-Depth Information
The last action in the scene3D script is to set the destination coordinates of the viewer.
The destination coordinates, shown in lines 104-106, are the horizontal and vertical
coordinates of the selected flag and half of the z-distance from the viewer to the flag.
We do not want the viewer to move all the way to the flag, since we would then be on
top of the flag and unable to see anything. Later, we will see that the viewer will move
from its current location to the destination coordinates. Going half of the z-distance
provides the opportunity for some flags to get larger, some smaller, depending on the
relative z-values of the flag and viewer.
The last line of interest in the scene script is the break command in line 107. What
this does is to break out of the for loop once a flag has been selected to avoid further
testing. While this works well enough for our example here, it is not entirely foolproof.
Why? The reason is that we can have partially or fully overlapping flags, since our
objects are defined in three-dimensional space and our method of assigning coordinates
does not prevent it.
For example, consider the situation shown in Figure 7.15, where we have several flags
overlapping in the area inside the black circle. You may be interested in the blue, white,
and red flag that's in the foreground. If you happen to click where the black dot is
located, you will discover that you have clicked on the flag of Germany rather than the
flag of France. Reference to flagArray identifies part of the problem. The entries are in
reverse alphabetical order so that in the for loop, the hit test for Germany happens to
be performed before the hit test for France. The same type of situation arises with nor-
mal alphabetical order. What is needed is to find all objects that pass the hit test and
then check to see which one has the smallest z-value, which marks it as closest to the
viewer. We'll leave that as an exercise for you.
Figure 7.15 Overlapping flags
Search WWH ::




Custom Search