Table of content

Previous: 8.b Spherical distribution of leaders

# c. Slowing down the leaders and shifting the color of the tail

Next, we have the function for the leader:

    bmap leader_image = <superw.pcx>;
    var leader_life = 20;
    var leader_size = 200;
    var position;

    var tail_p = 0.3;
    var tail_size = 50;

    function leader()
    {
    	if (my.lifespan==80)
    	{
    		my.bmap = leader_image;
    		my.size = 0;
    		my.bright = on;
    		my.flare = on;
    		my.alpha = 100;
    		my.move  = on;
    		my.red = 255;
    		my.green = 55;
    		my.blue = 55;
    		my.gravity = 0;
    		my.lifespan = leader_life;

    		// used to modify the velocity
    		my.skill_x = my.vel_x;
    		my.skill_y = my.vel_y;
    		my.skill_z = my.vel_z;
    	}

    	// variation of the speed
    	my.vel_x = my.skill_x*(0.1+sin(90*my.lifespan/leader_life));
    	my.vel_y = my.skill_y*(0.1+sin(90*my.lifespan/leader_life));
    	my.vel_z = my.skill_z*(0.1+sin(90*my.lifespan/leader_life));

    	// variation of the size
    	my.size = leader_size*sin(my.lifespan*180/leader_life);

    	// we build the tail
    	my.skill_a =
    	sqrt(my.vel_x*my.vel_x+my.vel_y*my.vel_y+my.vel_z*my.vel_z);
    	my.skill_b = ceiling(my.skill_a*time/(tail_size*tail_p));
    	my.skill_c = 0;
    	while(my.skill_c<my.skill_b)
    	{
    		position.x = my.x - time*my.vel_x*my.skill_c/my.skill_b
    		+ time*my.vel_x;
    		position.y = my.y - time*my.vel_y*my.skill_c/my.skill_b
    		+ time*my.vel_y;
    		position.z = my.z - time*my.vel_z*my.skill_c/my.skill_b
    		+ time*my.vel_z;

    		// emit tail and change leader color to red
    		if (my.lifespan<start_tail)
    		{
    			my.red = 255;
    			my.green = 0;
    			my.blue = 0;
    			effect(tail,1,position.x,nullvector);
    		}
    		my.skill_c += 1;
    	}
    }

It is almost identical to that of lesson 4 except for two details. First, we put a timer on the emission of tail particles: leader() will only produce a trail if my.lifespan<start_tail = 16. This gives the effect a nice hollow center. From that time forward, the leader particle will also switch to bright red (see Figure 8.1).

Second, we add code which abruptly slows down the leaders without changing their direction of travel. This gives to the effect a more explosion-like appearance. This is done first by storing in the skill_x, skill_y and skill_z variables the initial velocity of the particle. Then, afterwards, we simply multiply the three components of this velocity by sin(90*my.lifespan/leader_life) which goes from 1 for my.lifespan = leader_life to 0 if my.lifespan = 0 (similarly to size on Figure 2.2). This way the length of the velocity vector decreases but its direction remains unchanged. Since we do not want the leaders to completely stop, we add also a small number, 0.1, in the sine function. This way, when my.lifespan is near 0, their velocity is not completely null.

Now for the final function: the tail particles.

    bmap tail_image = <novaw.pcx>;
    var tail_life = 5;
    var start_tail = 16;

    function tail()
    {
    	if (my.lifespan==80)
    	{
    		my.bmap = tail_image;
    		my.size = tail_size;
    		my.bright = on;
    		my.flare = on;
    		my.alpha = 100;
    		my.move  = on;
    		my.red = 100;
    		my.green = 255;
    		my.blue = 255;
    		my.gravity = 0;
    		my.lifespan = tail_life;
    	}

    	// we dim the particles
    	my.alpha = 100*(my.lifespan/tail_life);

    	// and make them more blue-ish
    	my.green = 255*(my.lifespan/tail_life);
    	my.red = 100*(my.lifespan/tail_life);
    }

It is quite similar to that used in lesson 4. The main difference is that the tail particles' color changes with time. It starts out bright blue ( red = 100, green = 255 and blue = 255) and end up just blue ( red = 0, green = 0 and blue = 255). They also become more transparent with time. As usual, we do this using the my.lifespan variable as timer. Check out Figure 8.3 to see how my.red, my.green and my.alpha evolve with time.

Figure 8.3: Variation with time of the red, green, blue and alpha components of tail particles as a function of the tail particle life span. The particle goes from full opacity to completely transparent. It also looses all its red and green color components.

That is it! :-) Be sure to try some changes in the parameters of the functions: you'll get all sorts of neat effects!!!

Next: 9. Conclusion: what to try next?