# Gas Cloud

## Description

Particles can be used in a wide variety of situations and it's possible to create all kinds of interesting effects. In this example we look at how the commands can be used to create an effect that makes it look as if the particles form some kind of gas cloud.

This example program relies on an external image named "particle.png":

## Set up

Only a few lines of code are required for this program, beginning with a call to set the virtual resolution and loading an image that will be used by the particles:

    SetVirtualResolution ( 320, 480 )
    LoadImage ( 1, "particle.png" )

    agk::SetVirtualResolution ( 320, 480 );
    agk::LoadImage ( 1, "particle.png" );

## Creating the gas cloud

A particle emitter is created and positioned at coordinates 150, 250, followed by properties being set such as the frequency, life and forces. All of these combine together to create the effect demonstrated by this program.

Here's the code used:

    CreateParticles ( 1, 150, 250 )
    SetParticlesFrequency ( 1, 500 )
    SetParticlesLife ( 1, 3 )
    SetParticlesSize ( 1, 64 );
    SetParticlesStartZone ( 1, -2, 0, 2, 0 )
    SetParticlesImage ( 1, 1 )
    SetParticlesDirection ( 1, 25, 50 )
    SetParticlesAngle ( 1, 360 )
    AddParticlesForce ( 1, 2.5, 3.0, 0, 200 )
    AddParticlesColorKeyFrame ( 1, 0.0, 0, 0, 0, 0 )
    AddParticlesColorKeyFrame ( 1, 0.5, 0, 0, 0, 0 )
    AddParticlesColorKeyFrame ( 1, 1.5, Random ( 0, 255 ), Random ( 0, 255 ), Random ( 0, 255 ), 255 )
    AddParticlesColorKeyFrame ( 1, 3.0, Random ( 0, 255 ), Random ( 0, 255 ), Random ( 0, 255 ), 0 )

    agk::CreateParticles ( 1, 150, 250 );
    agk::SetParticlesFrequency ( 1, 500 );
    agk::SetParticlesLife ( 1, 3.0f );
    agk::SetParticlesSize ( 1, 64 );
    agk::SetParticlesStartZone ( 1, -2, 0, 2, 0 );
    agk::SetParticlesImage ( 1, 1 );
    agk::SetParticlesDirection ( 1, 25, 50.0f );
    agk::SetParticlesAngle ( 1, 360.0f );
    agk::AddParticlesForce ( 1, 2.5, 3.0, 0, 200 );
    agk::AddParticlesColorKeyFrame ( 1, 0.0, 0, 0, 0, 0 );
    agk::AddParticlesColorKeyFrame ( 1, 0.5, 0, 0, 0, 0 );
    agk::AddParticlesColorKeyFrame ( 1, 1.5, agk::Random ( 0, 255 ), agk::Random ( 0, 255 ), agk::Random ( 0, 255 ), 255 );
    agk::AddParticlesColorKeyFrame ( 1, 3.0, agk::Random ( 0, 255 ), agk::Random ( 0, 255 ), agk::Random ( 0, 255 ), 0 );

Try experimenting with the properties in order to see the impact that they make on the program. Here's a few suggestions:

- alter the frequency and see either more or less particles emitted
- adjust the life span to make the particles last longer
- try changing the size of the particles
- alter the offsets used in the start zone to widen the area from which particles are emitted
- see the effect of increasing the initial velocity of particles
- notice how the angle can be used to limit the way in which particles are emitted
- try controlling the key frames, the first two ensure that all particles are black, resulting in the black hole effect, experiment with the timing to make the black hole larger or smaller

## The main loop

When the particles were created no maximum amount was set, therefore particles will continue to emit until such time that the emitter is destroyed, so it's not necessary to do anything other than update the contents of the screen within the main loop:

    do
        sync ( )
    loop

    void app::Loop ( void )
    {
        agk::Sync ( );
    }

## Full code listing

Everything is now in place. Here's the final code for our program:

    SetVirtualResolution ( 320, 480 )

    CreateSprite ( LoadImage ( "alien_backdrop.jpg" ) )
    LoadImage ( 1, "particle.png" )

    CreateParticles ( 1, 150, 250 )
    SetParticlesFrequency ( 1, 500 )
    SetParticlesLife ( 1, 3 )
    SetParticlesSize ( 1, 64 );
    SetParticlesStartZone ( 1, -2, 0, 2, 0 )
    SetParticlesImage ( 1, 1 )
    SetParticlesDirection ( 1, 25, 50 )
    SetParticlesAngle ( 1, 360 )
    AddParticlesForce ( 1, 2.5, 3.0, 0, 200 )
    AddParticlesColorKeyFrame ( 1, 0.0, 0, 0, 0, 0 )
    AddParticlesColorKeyFrame ( 1, 0.5, 0, 0, 0, 0 )
    AddParticlesColorKeyFrame ( 1, 1.5, Random ( 0, 255 ), Random ( 0, 255 ), Random ( 0, 255 ), 255 )
    AddParticlesColorKeyFrame ( 1, 3.0, Random ( 0, 255 ), Random ( 0, 255 ), Random ( 0, 255 ), 0 )

    do
        sync ( )
    loop

    void app::Begin ( void )
    {
        agk::SetVirtualResolution ( 320, 480 );

        agk::LoadImage ( 1, "particle.png" );
    }

    void app::Loop ( void )
    {
        agk::CreateParticles ( 1, 150, 250 );
        agk::SetParticlesFrequency ( 1, 500 );
        agk::SetParticlesLife ( 1, 3.0f );
        agk::SetParticlesSize ( 1, 64 );
        agk::SetParticlesStartZone ( 1, -2, 0, 2, 0 );
        agk::SetParticlesImage ( 1, 1 );
        agk::SetParticlesDirection ( 1, 25, 50.0f );
        agk::SetParticlesAngle ( 1, 360.0f );
        agk::AddParticlesForce ( 1, 2.5, 3.0, 0, 200 );
        agk::AddParticlesColorKeyFrame ( 1, 0.0, 0, 0, 0, 0 );
        agk::AddParticlesColorKeyFrame ( 1, 0.5, 0, 0, 0, 0 );
        agk::AddParticlesColorKeyFrame ( 1, 1.5, agk::Random ( 0, 255 ), agk::Random ( 0, 255 ), agk::Random ( 0, 255 ), 255 );
        agk::AddParticlesColorKeyFrame ( 1, 3.0, agk::Random ( 0, 255 ), agk::Random ( 0, 255 ), agk::Random ( 0, 255 ), 0 );

        agk::Sync ( );
    }

## Conclusion

The effect that has been created in this example is another good demonstration of what is achievable when using the particle commands.