A simple emitter
Description
Emitters can be created to deal with effects such as engine trails, weapons, smoke and much more. In this program a simple emitter is created that fires particles up into the air.
This example program relies on an external image named "test2.png":
Set up
The code that creates this emitter is as follows:
LoadImage ( 1, "test2.png" )
CreateParticles ( 1, 150, 250 )
SetParticlesImage ( 1, 1 )
SetParticlesStartZone ( 1, -5, 0, 5, 0 )
SetParticlesDirection ( 1, 0, -145 )
SetParticlesAngle ( 1, 15 )
SetParticlesFrequency ( 1, 60 )
SetParticlesLife ( 1, 1.5 )
SetParticlesSize ( 1, 16 )
AddParticlesColorKeyFrame ( 1, 0, 255, 255, 255, 255 )
AddParticlesColorKeyFrame ( 1, 1.5, 255, 255, 255, 0 )
agk::LoadImage ( 1, "test2.png" );
agk::CreateParticles ( 1, 150, 250 );
agk::SetParticlesImage ( 1, 1 );
agk::SetParticlesStartZone ( 1, -5, 0, 5, 0 );
agk::SetParticlesDirection ( 1, 0, -145.0f );
agk::SetParticlesAngle ( 1, 15 );
agk::SetParticlesFrequency ( 1, 60 );
agk::SetParticlesLife ( 1, 1.5 );
agk::SetParticlesSize ( 1, 16 );
agk::AddParticlesColorKeyFrame ( 1, 0, 255, 255, 255, 255 );
agk::AddParticlesColorKeyFrame ( 1, 1.5, 255, 255, 255, 0 );
Understanding the code
The code performs the following actions:
- loads the image "test2.png" into slot 1
- creates particles with an ID of 1 positioned at 150, 250
- applies image 1 ("test2.png") to the particles
- alters the start zone for particles on the X axis by -5 and +5
- applies a negative value on the Y direction forcing all particles to be emitted so they move up the screen
- sets the angle of emission between 0 and 15 allowing a little variation on the Y axis
- sets the frequency to 60 particles per second
- reduces the life span of particles to 1.5 seconds
- changes the size of particles to be 16 pixels
- adds one color key frame that is used at the beginning of the particle's life - with full alpha
- adds one color key frame that is used at the end of the particle's life - with no alpha thus forcing the particle to fade out
Main Loop
Our main loop simply needs to make a call to Sync to update the screen:
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, "test2.png" )
CreateParticles ( 1, 150, 250 )
SetParticlesImage ( 1, 1 )
SetParticlesStartZone ( 1, -5, 0, 5, 0 )
SetParticlesDirection ( 1, 0, -145 )
SetParticlesAngle ( 1, 15 )
SetParticlesFrequency ( 1, 60 )
SetParticlesLife ( 1, 1.5 )
SetParticlesSize ( 1, 16 )
AddParticlesColorKeyFrame ( 1, 0, 255, 255, 255, 255 )
AddParticlesColorKeyFrame ( 1, 1.5, 255, 255, 255, 0 )
do
Sync ( )
loop
void app::Begin ( void )
{
agk::SetVirtualResolution ( 320, 480 );
agk::CreateSprite ( agk::LoadImage ( "alien_backdrop.jpg" ) );
agk::LoadImage ( 1, "test2.png" );
agk::CreateParticles ( 1, 150, 250 );
agk::SetParticlesImage ( 1, 1 );
agk::SetParticlesStartZone ( 1, -5, 0, 5, 0 );
agk::SetParticlesDirection ( 1, 0, -145.0f );
agk::SetParticlesAngle ( 1, 15 );
agk::SetParticlesFrequency ( 1, 60 );
agk::SetParticlesLife ( 1, 1.5 );
agk::SetParticlesSize ( 1, 16 );
agk::AddParticlesColorKeyFrame ( 1, 0, 255, 255, 255, 255 );
agk::AddParticlesColorKeyFrame ( 1, 1.5, 255, 255, 255, 0 );
}
void app::Loop ( void )
{
agk::Sync ( );
}
Conclusion
With a few lines of code a fairly complex emitter has been generated. This could be used for all kinds of effects such as the exhaust from an engine, poisonous gas escaping from a pipe, smoke and much more.