Graphics Programs Reference
In-Depth Information
Next, let's define our placeObj() function. Since dolphins are both smart and playful,
let's apply a combination of some structure plus some randomness to our placement of
them as shown in Figure 7.5. We'll place them randomly within limits in both the x- and
z-directions as we have done before.
27
28
29
30
31
32
33
34
// place an object in 3D space (with z relative to the viewer)
function placeObj()
{
thisObj.x = Math.random() * 1000 - 500; // -500 < x < 500
thisObj.y = 300 *(i % 3) - 300; // y = -300, 0, 300
thisObj.z = Math.random() * 400 + 150; // 150 < z < 450
}
In the y-direction, we'll cycle the position of each dolphin to be in one of three locations,
either at y = -300 , y = 0 , or y = 300 . This is how it works. We have defined the num-
ber of objects to be 15. The expression (i % 3) takes the remainder left when any num-
ber for i is divided by 3 as shown below:
i = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
i % 3 = 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2
Figure 7.5 Example placement of dolphins
Search WWH ::




Custom Search