jeffheaton's picture

    Explosions can be a useful effect in Second Life. This recipe implements an explosion that includes fire, smoke and a bang sound effect. To see the explosion in action, touch the black sphere that contains it. The object will explode, as seen in Figure 4.5.

Figure 4.5: Explosion

Explosion

    The explosion script is not based on the basic particle script, as were previous recipes. The script for the explosion script can be seen in Listing 4.5.

Listing 4.5: Explosion (Explode.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/
    
        
fakeMakeExplosion(integer particle_count, float particle_scale, float particle_speed, 
                 float particle_lifetime, float source_cone, string source_texture_id, 
                 vector local_offset) 
{
    //local_offset is ignored
    llParticleSystem([
        PSYS_PART_FLAGS,            PSYS_PART_INTERP_COLOR_MASK|PSYS_PART_INTERP_SCALE_MASK|PSYS_PART_EMISSIVE_MASK|PSYS_PART_WIND_MASK,
        PSYS_SRC_PATTERN,           PSYS_SRC_PATTERN_ANGLE_CONE,
        PSYS_PART_START_COLOR,      <1.0, 1.0, 1.0>,
        PSYS_PART_END_COLOR,        <1.0, 1.0, 1.0>,
        PSYS_PART_START_ALPHA,      0.50,
        PSYS_PART_END_ALPHA,        0.25,
        PSYS_PART_START_SCALE,      <particle_scale, particle_scale, 0.0>,
        PSYS_PART_END_SCALE,        <particle_scale * 2 + particle_lifetime, particle_scale * 2 + particle_lifetime, 0.0>,
        PSYS_PART_MAX_AGE,          particle_lifetime,
        PSYS_SRC_ACCEL,             <0.0, 0.0, 0.0>,
        PSYS_SRC_TEXTURE,           source_texture_id,
        PSYS_SRC_BURST_RATE,        1.0,
        PSYS_SRC_ANGLE_BEGIN,       0.0,
        PSYS_SRC_ANGLE_END,         source_cone * PI,
        PSYS_SRC_BURST_PART_COUNT,  particle_count / 2,
        PSYS_SRC_BURST_RADIUS,      0.0,
        PSYS_SRC_BURST_SPEED_MIN,   particle_speed / 3,
        PSYS_SRC_BURST_SPEED_MAX,   particle_speed * 2/3,
        PSYS_SRC_MAX_AGE,           particle_lifetime / 2,
        PSYS_SRC_OMEGA,             <0.0, 0.0, 0.0>
        ]);
}

default
{
    state_entry()
    {
        llPreloadSound("explosion");
        llSetText("Touch to Explode", <0.0, 1.0, 0.0>, 1.0);
    }
    

    
    touch_start(integer total_number)
    {
        fakeMakeExplosion(80, 1.0, 13.0, 2.2, 1.0, "fire", <0.0, 0.0, 0.0>);
        llTriggerSound("explosion", 10.0);
        llSleep(.5);
        fakeMakeExplosion(80, 1.0, 13.0, 2.2, 1.0, "smoke", <0.0, 0.0, 0.0>);
        llSleep(1);
        llParticleSystem([]);
    }
}

    The explosion script uses a function named fakeMakeExplosion. The fakeMakeExplosion is a function that was posted to the Linden Scripting Language Wiki (http://wiki.secondlife.com/wiki/
) and which emulates the behavior of the older function llMakeExplosion. The llMakeExplosion function has been deprecated and should no longer be used. However, the fakeMakeExplosion closely emulates the behavior of the original llMakeExplosion.

    The fakeMakeExplosion function accepts the seven parameters. These parameters are specified in Table 4.4.

Table 4.4: Parameters for fakeMakeExplosion

Parameter Purpose
Particles How many particles should be used for the explosion.
scale How big should the particles be.
vel What is the velocity for the explosion particles.
lifetime How long, in seconds, should the explosion particles last.
arc The explosion should occur between 0 and the angle specified.
texture What texture should be used for the explosion particles.
offset How far from the object should the explosion occur.

    The following code is used in the explosion recipe to create an explosion effect. First fakeMakeExplosion is called to produce an initial blast of fire.

fakeMakeExplosion(80, 1.0, 13.0, 2.2, 1.0, "fire", 
<0.0, 0.0, 0.0>);

    Next the explosion sound is played.

llTriggerSound("explosion", 10.0);

    The explosion lasts for about a half second before the smoke begins.

llSleep(.5);

    Now a similar blast of smoke is produced.

fakeMakeExplosion(80, 1.0, 13.0, 2.2, 1.0, "smoke", 
<0.0, 0.0, 0.0>);

    The smoke is allowed to continue for a second.

llSleep(1);

    The particle system is shut down.

llParticleSystem([]);

    This produces a brief explosion that lasts for two seconds.


Copyright 2005 - 2012 by Heaton Research, Inc.. Heaton Research™ and Encog™ are trademarks of Heaton Research. Click here for copyright, license and trademark information.