Recipe 3.1: Splashing Water
The Second Life world includes water. However, all water in Second Life is at exactly the same level. Water is usually at 20 meters on the z-coordinate. However, this is not always the case, as private islands can set the water height to any level desired. Second life water can be seen in Figure 3.1.
Figure 3.1: Beach Front Land in Second Life

Sometimes water is needed at a higher altitude than “sea level”. A good example of this is a swimming pool. A swimming pool could be above sea level if it occurs inland. To give the swimming pool the effect of water, a phantom rectangle is created with a water texture. A small swimming pool can be seen in Figure 3.2.
Figure 3.2: Swimming Pool

However, the water in the swimming pool is fairly boring without a script. A script allows the water to “flow” and also produce a “splash” sound when an avatar enters the water. Listing 3.1 shows the script used for water.
Listing 3.1: Splashing Water (Splash.lsl)
// From the book: // // Scripting Recipes for Second Life // by Jeff Heaton (Encog Dod in SL) // ISBN: 160439000X // Copyright 2007 by Heaton Research, Inc. // // This script may be freely copied and modified so long as this header // remains unmodified. // // For more information about this book visit the following web site: // // http://www.heatonresearch.com/articles/series/22/ default { state_entry() { llSetTextureAnim(ANIM_ON | ROTATE | LOOP | SMOOTH, ALL_SIDES, 0, 0, 0, 100, .05); llVolumeDetect(TRUE); } collision_start( integer num_detected ) { llTriggerSound("splash", 1); } }
The state_entry function sets up both the flow of the water and the splash sound effect. First, a texture animation is created. Texture animations are created once by calling llSetTextureAnim. Once the texture animation is started, it continues with no further interaction required from the script. The texture animation for the water is started with the following command:
llSetTextureAnim(ANIM_ON | ROTATE | LOOP | SMOOTH, ALL_SIDES, 0, 0, 0, 100, .05);
The above function call rotates the texture every 0.05 frames. This causes the water to slowly rotate. Next, a call to llVolumeDetect instructs Second Life to call the collision event handlers whenever an avatar comes into contact with the object. The following function call does this.
llVolumeDetect(TRUE);
Whenever the collision_start event handler is called, the “splash” sound should be played. The following lines do this.
collision_start( integer num_detected )
{
llTriggerSound("splash", 1);
}The llTriggerSound plays the sound “splash” at the maximum volume of one. The sound must be stored in the object's inventory.




