Intro to the AndEngine Particle System

It is well know that andengine has little or no documentation to support itself. The code is written in such a way that makes it easy to understand what is occurring if you try, however without much knowledge in certain aspects, learning the system is next to impossible. When attempting to add a particle system to Take Out The Trash! I was overwhelmed by the choices to me, and given my little experience in this aspect of game development I took a good week to familiarize myself to the inner workings of the particle system available within andengine. However, with the help of a nifty tool I found at the Google Play Store, I was able to wrestle control of my understanding, and now I hope to share this with you.
This tutorial assumes that you have little knowledge of andengine particle systems, but enough knowledge of how to create an andengine project and bring it to life. I talk about what a particle system is, how andengine implements this idea, and guide you through the creation of a campfire particle system that has a little breeze to give it life.
You will need:
What is a Particle System?
A particle system, in terms of computer graphics, is a system that is comprised of three components: an emitter, the engine, and the spite or "particle". The engine uses the emitter location to inject the sprites (or particles) that have been initialized to some sort of state, like size and rotation, into the world to behave uniformly with a given modifier over time. Therefore, to understand how to properly manipulate a particle system, it is important to understand the three components: the emitter, the engine, and the particle.
The particle behavior is set by two components, the Initializer and the Modifier.
andengine Particle System.
The andengine particle system was created with flexibility in mind and, as such, offers a choice of emitters with an array of initializers and modifiers that the programmer may use to alter the behavior of the particle over time. The current available emitters are:
Each particle is given a texture, usually a roundish figure with some sort of alpha blend into the background and no larger then 8x8px. However, this texture can be anything that you wish it to be, but you will probably find that the premise of the particle system makes unusual particle textures unnecessary. Here is the image that I used for the con trail in Take Out The Trash! The black background is given because the alpha transparency would not show the particle accurately:
When the particle is created the engine uses the given initializers to apply some form of standard behavior to each particle. These initializers are created and set by the programmer, and here is the list:
After the particles are created, they continue until expiration, and during that time if a modifier is given by the programmer, they will transition between each specified state until the end of the cycle. This is a list of the current Modifiers available in andengine:
The key idea here is "time range". Any modifier can move from one value to another starting at any time during the designated expiration length. The programmer may start at time 1.0 and move to time 5.0 and adjust the color, then could move from time 5.0 to time 6.0 to adjust the color again. This is the concept of the modifier. Although the programmer may only have one initializer per particle system, they may have as many modifiers as need be to generate a system that is aesthetically pleasing and meets the given requirements.
Finally, to create the system andengine needs to know three more things:
Maximum Particle Count: The total particles the system will create over the given expire time.
Minimum Particles per Second: The least amount of particles to emit over the course of the lifetime.
Maximum Particles per Second: The most particles the system will generate over the course of the lifetime.
The position of the particle system is set upon creation, although the method SpriteParticleSystem.setPosition(float, float) will allow the programmer to change this. It is of note that if attaching the particle system to a parent, care be taken so that the relative offset of the particle system in relation to the parent is included when creating the emitter, otherwise offset errors are likely to occur.
Implementation.
Lets now take theory and produce something of meaning. Hopefully by now you have downloaded and opened BitStrawberries Particle System Customizer. This is a wonderful tool in aiding the programmer in visualizing what the particle system will look like, and is a better alternative to recompilation as that takes minutes or more. Using the Particle System Customizer is instant.
I like particle systems, I think they are neat. Emphasis in this tutorial is given to fire systems, as the undulation is somewhat hypnotic. So, with that said, lets create a a campfire. Open the Customizer. The main screen is the basic particle system implementation. Lets leave the background black, since this is a campfire, and fire usually doesn't appear well in the day. Leave everything the same but change:
There should now be a line in the render window that is filling with particles but nothing is happening. In order to produce results, Initializers and Modifiers are needed to alter the particles behavior. To do so, tap the "menu" button, and a selection screen should pop up. We want the fire to move "upward", so a velocity initializer is needed to give the particles motion. Tap "Add/Edit Initializer" and select the "Velocity" initializer. Set the values as follows:
This creates an upward motion to the particles, because -y in andengine is "up", and a slight sideways motion is given with the x values. Hopefully you should see a white upward moving "cloud". Unfortunately this cloud generates then stops, so an Expire initializer is needed to inform andengine when to cycle the particles. Tap the menu button once again and select "Add/Edit Initializer", then select "Expire".
After implementing these changes the particle system should now cycle and not stop. Looking good so far. There is a rather uninteresting artifact in this system in that when the particles reach the end of their cycle they "pop" out of the scene. This is because there are no alpha modifiers to allow them to blend seamlessly with the background just before termination. So, to correct this situation, an alpha modifier is needed to control that behavior. Tap the menu button and select "Add Modifier". The particles need to fade out over their given time sequence, so input these values for the alpha modifier:
Now there is a smooth blend between active and non active states. The reason why the full expiration length is not used as the termination time is because the Expire Initializer generates a range of values, and if the full time is used, some particles will still be visible when they expire. Remember that the change is given over the life time of the particle, and if it takes 7 seconds for the particle to expire, but 8 seconds to blend alpha, the particle will "pop", which is something that needs to be avoided.
It still doesn't look like a campfire though does it? Lets add some color. Open the menu and tap "Add Modifier" and add a Color Modifier. Lets transition from red to orange. Enter these values:
This will create a smooth transition from red to orange over the time frame of one second. But it still doesn't look like a camp fire. Camp fires usually don't stay orange. So, lets add some gray to simulate ash coming from the camp fire. Add another modifier, and enter these values:
Now we are cookin! You should see a red to orange to gray fire. Imagining normal fire behavior more closely, fire tends not to stay the same size over its lifetime, but diminish instead. Lets add a scale modifier and add that. Enter these values:
Looks like its time to go camping! But wouldn't a soft breeze pushing the fire around be a nice touch? Try to add an acceleration initializer that sets a -x acceleration.
Now that the particle system is created and looks somewhat as we intend, its time to implement this system into our andengine game. I'm not going to go through the creation of an andengine game. That is beyond the scope of this article, and you can find many tutorials explaining how to do this. Instead, the addition of the particle system into the existing engine will be covered.
To begin, have a look at the SpriteParticleSystem constructor:
SpriteParticleSystem(final IParticleEmitter pParticleEmitter, final float pRateMinimum, final float pRateMaximum, final int pParticlesMaximum, final ITextureRegion pTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager)
It takes:
The only two objects that have not been mentioned are the TextureRegion and the VertexBufferObectManager. To properly initialize this class these are the entities we need. First, create the Atlas and the Texture to store the image of our particle. Create a particle texture file in PNG format, sized 8x8px, save it in the assets directory, and name it "particle.png". Add these fields to the class you intend to use the particle system in:
//The texture and Atlas for a campfire particle system
public BitmapTextureAtlas mParticleAtlas;
public TextureRegion mParticleTexture;
Now add the particle system fields:
//the Emitter and the Particle System
public RectangleParticleEmitter mCampFireEmitter;
public SpriteParticleSystem mCampFireParticleSystem;
The particle texture needs to first be loaded into memory before the particle system can be created:
//Create the atlas, where getTextureManager is a member of BaseGameActivity
mParticleAtlas = new BitmapTextureAtlas(getTextureManager, 8,8);
//create the texture, where "this" references a valid BaseGameActivity
mParticleTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mParticleAtlas, this, "particle.png", 0,0);
//load the atlas
mParticleAtlas.load();
To make life easier, create a function called createParticleSystems() which will be called from your onCreateResources() function:
//creates the particle system
public void createParticleSystems()
{
//first create the emitter
mCampFireEmitter = new RectangleParticleEmitter(mCamera.getWidth()/2, mCamera.getHeight()/2, 50,0);
The RectangleParticleEmitter takes the center location. Use the camera object that you initialized in your project that this system is being added into. The other two fields are the width and height, respectively. Get these values from the settings that was input into the Customizer home screen.
Now create the SpriteParticleSystemObject:
//the campfire particle system
mCampFireParticleSystem = new SpriteParticleSystem(mCampFireEmitter, 50.0f, 75.0f, 500, mParticleTexture, getVertexBufferObjectManager());
Here input the minimum particles per second, the maximum particles per second, and the total particles that were entered into the Customizer. It should become apparent now that all that is happening is data transference into java code and specifically andengine particle system calls.
Although you may add the initializers and modifiers in any order that you wish, preference is given to create in order initializer to modifier, as it makes reading code easier. However, when creating the particle system andengine makes no distinction as to the order with which these calls are made. So, lets call the initializers first, then the modifiers:
//add the initializers
//Expire Initializer
mCampFireParticleSystem.addParticleInitializer(new ExpireParticleInitializer<Sprite>(7.0f, 8.0f));
//Velocity initializer
mCampFireParticleSystem.addParticleInitializer(new VelocityParticleInitializer<Sprite>(-2.0f, 2.0f, -10.0f, -19.0f));
//Acceleration Initializer
To blend alpha correctly andengine needs to know how to calculate alpha values. A call to BlendFunctionParticleInitializer is made to instruct andengine and OpenGLES how to compute these values. A discussion on OpenGLES blending functions is beyond the scope of this text.
//set the blend function
mCampFireParticleSystem.addParticleInitializer(new BlendFunctionParticleInitializer(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA));
Now that the initializers have been added, its time to add the modifiers to the particle system pipeline. Again, these can be done in any order:
//alpha modifier
mCampFireParticleSystem.addParticleModifier(new AlphaParticleModifier<Sprite>(0.0f, 6.99f, 1.0f, 0.5f));
//color modifier1
mCampFireParticleSystem.addParticleModifier(new ColorParticleModifier<Sprite>(0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.51f, 0.0f, 0.0f));
//color modifier 2
mCampFireParticleSystem.addParticleModifier(new ColorParticleModifier<Sprite>(1.0f, 2.0f, 1.0f, 0.50f, 0.51f, 0.50f, 0.0f, 0.50f));
//scale modifier
mCampFireParticleSystem.addParticleModifier(new ScaleParticleModifier<Sprite>(0.0f, 6.99f, 1.0f, 0.50f));
//close the function
}
Make and run the program. If everything worked correctly there should be a neat campfire looking effect in the center of the screen. Although I only used a few initializers and modifiers, there are more to choose, and the inquisitive mind will delight in the exploration of the code, which can be found at GitHub. Here is the complete source code. I complied everything into one function to ease copy and paste:
//The texture and Atlas for a campfire particle system
public BitmapTextureAtlas mParticleAtlas;
public TextureRegion mParticleTexture;
//the Emitter and the Particle System
public RectangleParticleEmitter mCampFireEmitter;
public SpriteParticleSystem mCampFireParticleSystem;
//creates the particle system
public void createParticleSystems()
{
//Create the atlas, where getTextureManager is a member of BaseGameActivity
mParticleAtlas = new BitmapTextureAtlas(getTextureManager, 8,8);
//create the texture, where "this" references a valid BaseGameActivity
mParticleTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mParticleAtlas, this, "particle.png", 0,0);
//load the atlas
mParticleAtlas.load();
//first create the emitter
mCampFireEmitter = new RectangleParticleEmitter(mCamera.getWidth()/2, mCamera.getHeight()/2, 50,0);
//add the initializers
//Expire Initializer
mCampFireParticleSystem.addParticleInitializer(new ExpireParticleInitializer<Sprite>(7.0f, 8.0f));
//Velocity initializer
mCampFireParticleSystem.addParticleInitializer(new VelocityParticleInitializer<Sprite>(-2.0f, 2.0f, -10.0f, -19.0f));
//Acceleration Initializer
//set the blend function
mCampFireParticleSystem.addParticleInitializer(new BlendFunctionParticleInitializer(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA));
//alpha modifier
mCampFireParticleSystem.addParticleModifier(new AlphaParticleModifier<Sprite>(0.0f, 6.99f, 1.0f, 0.5f));
//color modifier1
mCampFireParticleSystem.addParticleModifier(new ColorParticleModifier<Sprite>(0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.51f, 0.0f, 0.0f));
//color modifier 2
mCampFireParticleSystem.addParticleModifier(new ColorParticleModifier<Sprite>(1.0f, 2.0f, 1.0f, 0.50f, 0.51f, 0.50f, 0.0f, 0.50f));
//scale modifier
mCampFireParticleSystem.addParticleModifier(new ScaleParticleModifier<Sprite>(0.0f, 6.99f, 1.0f, 0.50f));
//close the function
}
Hope that this was helpful for you. I spent a lot of time trying to understand the andengine particle system, and there are few tutorials on the subject, with most just saying "just the read the code". While that's all fine and good, for the amateur or non-computer science junky trying their hand at creating something with andengine it makes it more difficult. Enjoy.
If you have any questions about this tutorial, please contact me:
This tutorial assumes that you have little knowledge of andengine particle systems, but enough knowledge of how to create an andengine project and bring it to life. I talk about what a particle system is, how andengine implements this idea, and guide you through the creation of a campfire particle system that has a little breeze to give it life.
You will need:
- Some sort of Android compiler.
- BitStrawberries Particle System Customizer
What is a Particle System?
A particle system, in terms of computer graphics, is a system that is comprised of three components: an emitter, the engine, and the spite or "particle". The engine uses the emitter location to inject the sprites (or particles) that have been initialized to some sort of state, like size and rotation, into the world to behave uniformly with a given modifier over time. Therefore, to understand how to properly manipulate a particle system, it is important to understand the three components: the emitter, the engine, and the particle.
- Emitter: The location of the origin with which the particles will emanate, and the size of area to fill.
- Engine: Responsible for the initialization, updating, and the destruction of the particles.
- Particle: The element that moves through a given sequence that changes its base look and behavior over time.
The particle behavior is set by two components, the Initializer and the Modifier.
- Initializer: Sets the default behavior of every particle. Some sort of variance, like position or velocity, is given by the initializer. In andengine once the particle is initialized, it cannot be changed.
- Modifier: Determines how the particle will behave over time. Usually this involves changing color, size, or alpha. In andengine there is no variance to the modifier, and all modifier settings are applied uniformly to all particles.
andengine Particle System.
The andengine particle system was created with flexibility in mind and, as such, offers a choice of emitters with an array of initializers and modifiers that the programmer may use to alter the behavior of the particle over time. The current available emitters are:
- Point: All particles are created at a single source.
- Rectangular: Particles will be created within the specified boundaries.
- Circular: Particles will be created within the specified radius.
- Rectangular Outline: Particles will be created within the rectangles outline, but not within the rectangle.
- Circular Outline: Particles will be created within the outer perimeter, but not within the circle.
Each particle is given a texture, usually a roundish figure with some sort of alpha blend into the background and no larger then 8x8px. However, this texture can be anything that you wish it to be, but you will probably find that the premise of the particle system makes unusual particle textures unnecessary. Here is the image that I used for the con trail in Take Out The Trash! The black background is given because the alpha transparency would not show the particle accurately:
When the particle is created the engine uses the given initializers to apply some form of standard behavior to each particle. These initializers are created and set by the programmer, and here is the list:
- Acceleration: Sets the variance of the particles change in velocity over time.
- Alpha: Sets the beginning alpha variance.
- Color: Sets the beginning Color variance.
- Expire: Sets the beginning variance in terms of the length of time the particle will stay in existence.
- Gravity: Sets the gravity change.
- Rotation: Sets the variance in rotation of the particle.
- Scale: Sets the size variance of each particle.
- Velocity: Sets the initial speed variance within the x and y planes.
After the particles are created, they continue until expiration, and during that time if a modifier is given by the programmer, they will transition between each specified state until the end of the cycle. This is a list of the current Modifiers available in andengine:
- Alpha: During what time range should the alpha transition from one value to another.
- Color: During what time range should the color transition from one value to another.
- Rotation: During what time range should the rotation move from one value to another.
- Scale: During what time range should the scale move from one value to another.
The key idea here is "time range". Any modifier can move from one value to another starting at any time during the designated expiration length. The programmer may start at time 1.0 and move to time 5.0 and adjust the color, then could move from time 5.0 to time 6.0 to adjust the color again. This is the concept of the modifier. Although the programmer may only have one initializer per particle system, they may have as many modifiers as need be to generate a system that is aesthetically pleasing and meets the given requirements.
Finally, to create the system andengine needs to know three more things:
Maximum Particle Count: The total particles the system will create over the given expire time.
Minimum Particles per Second: The least amount of particles to emit over the course of the lifetime.
Maximum Particles per Second: The most particles the system will generate over the course of the lifetime.
The position of the particle system is set upon creation, although the method SpriteParticleSystem.setPosition(float, float) will allow the programmer to change this. It is of note that if attaching the particle system to a parent, care be taken so that the relative offset of the particle system in relation to the parent is included when creating the emitter, otherwise offset errors are likely to occur.
Implementation.
Lets now take theory and produce something of meaning. Hopefully by now you have downloaded and opened BitStrawberries Particle System Customizer. This is a wonderful tool in aiding the programmer in visualizing what the particle system will look like, and is a better alternative to recompilation as that takes minutes or more. Using the Particle System Customizer is instant.
I like particle systems, I think they are neat. Emphasis in this tutorial is given to fire systems, as the undulation is somewhat hypnotic. So, with that said, lets create a a campfire. Open the Customizer. The main screen is the basic particle system implementation. Lets leave the background black, since this is a campfire, and fire usually doesn't appear well in the day. Leave everything the same but change:
- Emitter = Rectangle
- Height = 0
- Max Particles = 500
- System Lifetime = 0
- Min Particles per second = 50
- Max Particles per second = 75
There should now be a line in the render window that is filling with particles but nothing is happening. In order to produce results, Initializers and Modifiers are needed to alter the particles behavior. To do so, tap the "menu" button, and a selection screen should pop up. We want the fire to move "upward", so a velocity initializer is needed to give the particles motion. Tap "Add/Edit Initializer" and select the "Velocity" initializer. Set the values as follows:
- Velocity Min Y = -10
- Velocity Max Y = -19
- Velocity Min X = -2
- Velocity Max X = 2
This creates an upward motion to the particles, because -y in andengine is "up", and a slight sideways motion is given with the x values. Hopefully you should see a white upward moving "cloud". Unfortunately this cloud generates then stops, so an Expire initializer is needed to inform andengine when to cycle the particles. Tap the menu button once again and select "Add/Edit Initializer", then select "Expire".
- Min Expire Time = 7.0
- Max Expire Time = 8.0
After implementing these changes the particle system should now cycle and not stop. Looking good so far. There is a rather uninteresting artifact in this system in that when the particles reach the end of their cycle they "pop" out of the scene. This is because there are no alpha modifiers to allow them to blend seamlessly with the background just before termination. So, to correct this situation, an alpha modifier is needed to control that behavior. Tap the menu button and select "Add Modifier". The particles need to fade out over their given time sequence, so input these values for the alpha modifier:
- Start Time = 0.0
- End Time = 6.99
- Start Alpha = 1.0
- Finish Alpha = 0.0
Now there is a smooth blend between active and non active states. The reason why the full expiration length is not used as the termination time is because the Expire Initializer generates a range of values, and if the full time is used, some particles will still be visible when they expire. Remember that the change is given over the life time of the particle, and if it takes 7 seconds for the particle to expire, but 8 seconds to blend alpha, the particle will "pop", which is something that needs to be avoided.
It still doesn't look like a campfire though does it? Lets add some color. Open the menu and tap "Add Modifier" and add a Color Modifier. Lets transition from red to orange. Enter these values:
- Start Time = 0.0
- Finish Time = 1.0
- Red Start = 1.0
- Red Finish = 1.0
- Green Start = 0.0
- Green Finish = 0.51
- Blue Start = 0.0
- Blue Finish = 0.0
This will create a smooth transition from red to orange over the time frame of one second. But it still doesn't look like a camp fire. Camp fires usually don't stay orange. So, lets add some gray to simulate ash coming from the camp fire. Add another modifier, and enter these values:
- Start Time = 1.0
- Finish Time = 2.0
- Red Start = 1.0
- Red Finish = 0.5
- Green Start = 0.51
- Green Finish = 0.50
- Blue Start = 0.0
- Blue Finish = 0.50
Now we are cookin! You should see a red to orange to gray fire. Imagining normal fire behavior more closely, fire tends not to stay the same size over its lifetime, but diminish instead. Lets add a scale modifier and add that. Enter these values:
- Start Time = 0.0
- End Time = 6.99
- Start Alpha = 1.0
- End Alpha = 0.50
Looks like its time to go camping! But wouldn't a soft breeze pushing the fire around be a nice touch? Try to add an acceleration initializer that sets a -x acceleration.
Now that the particle system is created and looks somewhat as we intend, its time to implement this system into our andengine game. I'm not going to go through the creation of an andengine game. That is beyond the scope of this article, and you can find many tutorials explaining how to do this. Instead, the addition of the particle system into the existing engine will be covered.
To begin, have a look at the SpriteParticleSystem constructor:
SpriteParticleSystem(final IParticleEmitter pParticleEmitter, final float pRateMinimum, final float pRateMaximum, final int pParticlesMaximum, final ITextureRegion pTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager)
It takes:
- Emitter
- Minimum Rate of Particles
- Maximum Rate of Particles
- Maximum Particle Count
- TextureRegion
- VertexBufferObjectManager
The only two objects that have not been mentioned are the TextureRegion and the VertexBufferObectManager. To properly initialize this class these are the entities we need. First, create the Atlas and the Texture to store the image of our particle. Create a particle texture file in PNG format, sized 8x8px, save it in the assets directory, and name it "particle.png". Add these fields to the class you intend to use the particle system in:
//The texture and Atlas for a campfire particle system
public BitmapTextureAtlas mParticleAtlas;
public TextureRegion mParticleTexture;
Now add the particle system fields:
//the Emitter and the Particle System
public RectangleParticleEmitter mCampFireEmitter;
public SpriteParticleSystem mCampFireParticleSystem;
The particle texture needs to first be loaded into memory before the particle system can be created:
//Create the atlas, where getTextureManager is a member of BaseGameActivity
mParticleAtlas = new BitmapTextureAtlas(getTextureManager, 8,8);
//create the texture, where "this" references a valid BaseGameActivity
mParticleTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mParticleAtlas, this, "particle.png", 0,0);
//load the atlas
mParticleAtlas.load();
To make life easier, create a function called createParticleSystems() which will be called from your onCreateResources() function:
//creates the particle system
public void createParticleSystems()
{
//first create the emitter
mCampFireEmitter = new RectangleParticleEmitter(mCamera.getWidth()/2, mCamera.getHeight()/2, 50,0);
The RectangleParticleEmitter takes the center location. Use the camera object that you initialized in your project that this system is being added into. The other two fields are the width and height, respectively. Get these values from the settings that was input into the Customizer home screen.
Now create the SpriteParticleSystemObject:
//the campfire particle system
mCampFireParticleSystem = new SpriteParticleSystem(mCampFireEmitter, 50.0f, 75.0f, 500, mParticleTexture, getVertexBufferObjectManager());
Here input the minimum particles per second, the maximum particles per second, and the total particles that were entered into the Customizer. It should become apparent now that all that is happening is data transference into java code and specifically andengine particle system calls.
Although you may add the initializers and modifiers in any order that you wish, preference is given to create in order initializer to modifier, as it makes reading code easier. However, when creating the particle system andengine makes no distinction as to the order with which these calls are made. So, lets call the initializers first, then the modifiers:
//add the initializers
//Expire Initializer
mCampFireParticleSystem.addParticleInitializer(new ExpireParticleInitializer<Sprite>(7.0f, 8.0f));
//Velocity initializer
mCampFireParticleSystem.addParticleInitializer(new VelocityParticleInitializer<Sprite>(-2.0f, 2.0f, -10.0f, -19.0f));
//Acceleration Initializer
To blend alpha correctly andengine needs to know how to calculate alpha values. A call to BlendFunctionParticleInitializer is made to instruct andengine and OpenGLES how to compute these values. A discussion on OpenGLES blending functions is beyond the scope of this text.
//set the blend function
mCampFireParticleSystem.addParticleInitializer(new BlendFunctionParticleInitializer(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA));
Now that the initializers have been added, its time to add the modifiers to the particle system pipeline. Again, these can be done in any order:
//alpha modifier
mCampFireParticleSystem.addParticleModifier(new AlphaParticleModifier<Sprite>(0.0f, 6.99f, 1.0f, 0.5f));
//color modifier1
mCampFireParticleSystem.addParticleModifier(new ColorParticleModifier<Sprite>(0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.51f, 0.0f, 0.0f));
//color modifier 2
mCampFireParticleSystem.addParticleModifier(new ColorParticleModifier<Sprite>(1.0f, 2.0f, 1.0f, 0.50f, 0.51f, 0.50f, 0.0f, 0.50f));
//scale modifier
mCampFireParticleSystem.addParticleModifier(new ScaleParticleModifier<Sprite>(0.0f, 6.99f, 1.0f, 0.50f));
//close the function
}
Make and run the program. If everything worked correctly there should be a neat campfire looking effect in the center of the screen. Although I only used a few initializers and modifiers, there are more to choose, and the inquisitive mind will delight in the exploration of the code, which can be found at GitHub. Here is the complete source code. I complied everything into one function to ease copy and paste:
//The texture and Atlas for a campfire particle system
public BitmapTextureAtlas mParticleAtlas;
public TextureRegion mParticleTexture;
//the Emitter and the Particle System
public RectangleParticleEmitter mCampFireEmitter;
public SpriteParticleSystem mCampFireParticleSystem;
//creates the particle system
public void createParticleSystems()
{
//Create the atlas, where getTextureManager is a member of BaseGameActivity
mParticleAtlas = new BitmapTextureAtlas(getTextureManager, 8,8);
//create the texture, where "this" references a valid BaseGameActivity
mParticleTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mParticleAtlas, this, "particle.png", 0,0);
//load the atlas
mParticleAtlas.load();
//first create the emitter
mCampFireEmitter = new RectangleParticleEmitter(mCamera.getWidth()/2, mCamera.getHeight()/2, 50,0);
//add the initializers
//Expire Initializer
mCampFireParticleSystem.addParticleInitializer(new ExpireParticleInitializer<Sprite>(7.0f, 8.0f));
//Velocity initializer
mCampFireParticleSystem.addParticleInitializer(new VelocityParticleInitializer<Sprite>(-2.0f, 2.0f, -10.0f, -19.0f));
//Acceleration Initializer
//set the blend function
mCampFireParticleSystem.addParticleInitializer(new BlendFunctionParticleInitializer(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA));
//alpha modifier
mCampFireParticleSystem.addParticleModifier(new AlphaParticleModifier<Sprite>(0.0f, 6.99f, 1.0f, 0.5f));
//color modifier1
mCampFireParticleSystem.addParticleModifier(new ColorParticleModifier<Sprite>(0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.51f, 0.0f, 0.0f));
//color modifier 2
mCampFireParticleSystem.addParticleModifier(new ColorParticleModifier<Sprite>(1.0f, 2.0f, 1.0f, 0.50f, 0.51f, 0.50f, 0.0f, 0.50f));
//scale modifier
mCampFireParticleSystem.addParticleModifier(new ScaleParticleModifier<Sprite>(0.0f, 6.99f, 1.0f, 0.50f));
//close the function
}
Hope that this was helpful for you. I spent a lot of time trying to understand the andengine particle system, and there are few tutorials on the subject, with most just saying "just the read the code". While that's all fine and good, for the amateur or non-computer science junky trying their hand at creating something with andengine it makes it more difficult. Enjoy.
If you have any questions about this tutorial, please contact me: