Table of content

Previous: 2.b Choosing the initial velocity and position of the particles

# c. Making sparks out of particles

Finally, we modify the code for the particles themselves. This is when the really fun stuff begins: we can play endlessly with their color, size, transparency, etc. This is done by adding instructions to the particles' function outside of the if (my.lifespan==80){} condition , so that the new code affects the particle during its whole life span. Here I propose the following very simple modification:

    var start_glow;
    ...
    start_glow = 5 + 10*key_shift;
    ...
    function improved_part()
    {
    	if (my.lifespan==80)
    	{
    		my.bmap = part_image;
    		my.size = 0;
    		my.bright = on;
    		my.flare = on;
    		my.alpha = 100;
    		my.move  = on;
    		my.red = 100;
    		my.green = 100;
    		my.blue = 255;
    		my.gravity = 2;
    		my.lifespan = 5 + random(part_life-5);
    	}

    	if (my.lifespan<start_glow)
    	{
    		my.size = part_size*sin(180*my.lifespan/start_glow);
    	}
    }

Basically, I did not like the fact that the particles' aspect remained unchanged all the time. They look more like bright dinner plates than sparks! Real sparks start up very dim but as they get further up, they enter fast combustion and give a nice bright glow before burning themselves out. To simulate this, we set the particles' size as on Figure 2.2, using the system-defined and updated variable my.lifespan as timer.

Figure 2.2: Size of the particle as a function of its life span (read the graph from the right to the left! my.lifespan always decreases). The particle starts out with zero size until it reaches the start_glow = 15 last ticks of its existence. Then, it expands (until reaching size 100) and collapses following a sine function.

The particle is created with size 0 and it will stay that way until the last start_glow ticks of it existence where it will expand and collapse. This is done with a factor sin(180*my.lifespan/start_glow) which is zero at my.lifespan = 0 and my.lifespan = start_glow, and between 0 and 1 for the other values (see Figure 2.2). This makes the particle 'pop' and adds visual appeal to it. Using the variable my.lifespan makes sure that the effect will not depend on the frame rate. The variable start_glow varies depending on user input: it takes value 5 if the 'shift' key is not pressed and 15 otherwise. This adapts the popping effect to the amount of time the particle flies in the air. Finally, to add some variation to the effect, we draw the life span of the particles randomly between 5 and 20 ticks. The overall affect is a "flame" near the fountain tip and together with particles popping in mid-air.

Of course, the same principles can be used to modify the particles' color, transparency, brightness, etc. I refer the reader to other particle tutorials for ideas.

The reader might ask the following question: "Why use the variable my.lifespan as timer? It requires backward logic to use since its value decreases from some value to 0. Couldn't we use instead one of the skill variables (which come with every particle) as a timer by doing my.skill_a += time, for instance? It would be simpler."

The answer is of course we could, and lifespan is indeed a little awkward to use. However it presents the advantages of being a system-defined and -updated variable. Using it leaves precious skill variables and memory available for other more complicated things (such as creating particle trails, for instance). Also, keeping the particle function as short and efficient as possible saves CPU power if you are using a large number of particles in the level.

Well, that is all for this long lesson. We covered a lot of ground and have discussed notions which will be used over and over from now on. Let's move on the next lesson and the laser beam!

Next: Lesson 3. Creating a laser beam (without the beam flag!)