Android game engine andengine Learning Series 5: superb effect of particle Transmitters

Source: Internet
Author: User

When we try to write game applications using andengine, if we want to add rich and colorful elements to the game, but try to use particle transmitters,

Andengine examples provides three examples to demonstrate the formation of two kinds of dynamic flames. The effect is very vivid. The particle emitter can form a flame. The principle can be understood as similar to that of an oscilloscope to form a graph, the particle transmitter emits custom effect elements, and the oscilloscope emits electrons, all of which are refreshed and drawn by a large number of elements. The difference is that, the range of particles emitted by the particle transmitter is limited, and the expired particles need to be manually killed to form a dynamic effect, while the oscilloscope is an electronic mark formed by the screen.

Next I will try to explain the formation of the flame effect from the source code.

I. Disc particle Transmitters:

A. B.

C. D.

Onloadresources () method:

@Overridepublic void onLoadResources() {this.mTexture = new Texture(32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);this.mParticleTextureRegion = TextureRegionFactory.createFromAsset(this.mTexture, this, "gfx/particle_point.png", 0, 0);this.mEngine.getTextureManager().loadTexture(this.mTexture);}

This method does not need to be discussed. The previous blog posts have introduced it. Note: The Particle Style image is a spot.

Onloadscene () method:

@ Overridepublic scene onloadscene () {This. mengine. registerupdatehandler (New fpslogger (); final scene = new scene (1); // circleoutlineparticleemitter is the circular example transmitter. The parameters in its constructor are: the first and second are the center of the circle, and the third is the final circleoutlineparticleemitter (camera_width * 0.5f, camera_height * 0.5f + 20, 80) with the radius final circleoutlineparticleemitter; // The Particle System, all the particles on the screen belong to this class for unified management of final particle system (participant system, 60, 60,360, this. mparticletextureregion); // remarks 1scene. setonscenetouchlistener (New ionscenetouchlistener () {@ overridepublic Boolean onscenetouchevent (scene pscene, touchevent pscenetouchevent) {participant emitter. setcenter (pscenetouchevent. getx (), pscenetouchevent. gety (); Return true ;}}); // initialize the particle state. // The parameters in colorinitializer are RGB colors (, 0) it indicates that the parameter in red // alphainitializer is the transparent value // setblendfunction. This is a mixed color method. Here we will introduce in detail: Random (New colorinitializer (1, 0, 0 )); particle System. addparticle initializer (New alphainitializer (0); particle system. setblendfunction (gl10.gl _ src_alpha, gl10.gl _ one); participant system. addparticle initializer (New velocityinitializer (-2, 2,-20,-10); particle system. addparticipant leinitializer (New rotationinitializer (0.0f, 360.0f); // modify the particle state. // parameters in scalemodifier: the first is in the from state, and the second is in the to state, the third is the start time, and the fourth is the end time. // scalemodifier (1.0f, 2.0f, 0, 5) indicates that the particle size is doubled in seconds; // optional // colormodifier parameter: the first, third, and fifth represent the rbg value of the starting color, and the second, fourth, and sixth represent the final color RGB value, seventh, the eighth parameter is the time range // colormodifier (1, 1, 0, 0.5f, 0, 0, 0, 3) represents 0-3 seconds, color (, 0) change to (1, 0.5f, 0); // parameters in alphamodifier: The first represents the transparency value of from, and the second represents the transparency value of, the third and fourth time ranges from time to time. // alphamodifier (0, 1, 0, 1) indicates that the particle changes from opacity to transparency in the 0-1 second. // opacity // expiremodifier parameter: the first parameter is the minimum survival time of the particle, and the second parameter is the longest survival time of the particle. // expiremodifier (6, 6) indicates that the particle can survive for 6 seconds. addparticle modifier (New scalemodifier (1.0f, 2.0f, 0, 5); particle system. addparticle modifier (New colormodifier (1, 1, 0, 0.5f, 0, 0, 0, 3); particle system. addparticle modifier (New colormodifier (1, 1, 0.5f, 1, 0, 1, 4, 6); particle system. addparticle modifier (New alphamodifier (0, 1, 0, 1); particle system. addparticle modifier (New alphamodifier (1, 0, 5, 6); particle system. addparticipant lemodifier (New expiremodifier (6, 6); // 2scene. gettoplayer (). addentity (participant system); Return scene ;}

NOTE 1: The construction parameter in the particle system class, the first is the defined particle transmitter, and the second is the minimum number of particles simultaneously emitted to the screen in one second, the third is the maximum number of particles that can be emitted to the screen at the same time in one second. The fourth parameter is the maximum number of particles that can survive on the screen, and the fifth parameter is the particle texture.

NOTE 2: Here is the most critical code to form a dynamic effect. When expired particles are killed, the particle system will release new particles to meet the maximum number of particles set by itself, 6 seconds is specified as the expiration time, so when the time reaches 6th seconds, the first emitted particle is killed.

You can see the updated source code in the particle system class:

final int particlesToSpawnThisFrame = Math.min(this.mMaxParticles - this.mParticlesAlive, (int)FloatMath.floor(this.mParticlesDueToSpawn));this.mParticlesDueToSpawn -= particlesToSpawnThisFrame;for(int i = 0; i < particlesToSpawnThisFrame; i++){this.spawnParticle();}

The maximum number of particles we set above is 360, while the number of particles that remain on the screen is mparticlesalive, this code is used to get the particle system to determine the number of particles to be emitted based on the number of surviving particles on the screen and the number of particles to be sent. Therefore, we know how many more particles we need to launch, a For Loop is used for launch.

The update method is as follows:

@Overrideprotected void onManagedUpdate(final float pSecondsElapsed) {if(this.mParticlesSpawnEnabled) {this.spawnParticles(pSecondsElapsed);}final Particle[] particles = this.mParticles;final ArrayList<IParticleModifier> particleModifiers = this.mParticleModifiers;final int particleModifierCountMinusOne = this.mParticleModifierCount - 1;for(int i = this.mParticlesAlive - 1; i >= 0; i--) {final Particle particle = particles[i];/* Apply all particleModifiers */for(int j = particleModifierCountMinusOne; j >= 0; j--) {particleModifiers.get(j).onUpdateParticle(particle);}particle.onUpdate(pSecondsElapsed);if(particle.mDead){this.mParticlesAlive--;final int particlesAlive = this.mParticlesAlive;particles[i] = particles[particlesAlive];particles[particlesAlive] = particle;}}}

As you can see, the final if judgment shows that if the particle has been killed, the number of surviving particles on the screen will be reduced by one and assigned a value again. The statement we killed is in note 2. As we wrote above, when we were 6th seconds, the first particle was killed, and the number of surviving particles on the screen was reduced by one, then the particle system finds that the number does not reach the maximum number set by itself, so it Then emits particles to meet its maximum number. In this way, killing, then generating, killing, and then generating appear dynamic.

2. The principle of the dot particle transmitter is the same as above. Let's look at it first:

1. 2.

3. 4.

Check whether the results are dazzling. Let's look at the code, which is similar to the following:

Onloadresources () method:

@Overridepublic void onLoadResources() {this.mTexture = new Texture(32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);this.mParticleTextureRegion = TextureRegionFactory.createFromAsset(this.mTexture, this, "gfx/particle_fire.png", 0, 0);this.mEngine.getTextureManager().loadTexture(this.mTexture);}

Onloadscene () method:

public Scene onLoadScene() {this.mEngine.registerUpdateHandler(new FPSLogger());final Scene scene = new Scene(1);scene.setBackground(new ColorBackground(0.0f, 0.0f, 0.0f));/* Left to right Particle System. */{final ParticleSystem particleSystem = new ParticleSystem(new PointParticleEmitter(0, CAMERA_HEIGHT), 6, 10, 200, this.mParticleTextureRegion);particleSystem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE);particleSystem.addParticleInitializer(new VelocityInitializer(15, 22, -60, -90));particleSystem.addParticleInitializer(new AccelerationInitializer(5, 15));particleSystem.addParticleInitializer(new RotationInitializer(0.0f, 360.0f));particleSystem.addParticleInitializer(new ColorInitializer(1.0f, 0.0f, 0.0f));particleSystem.addParticleModifier(new ScaleModifier(0.5f, 2.0f, 0, 5));particleSystem.addParticleModifier(new ExpireModifier(11.5f));particleSystem.addParticleModifier(new AlphaModifier(1.0f, 0.0f, 2.5f, 3.5f));particleSystem.addParticleModifier(new AlphaModifier(0.0f, 1.0f, 3.5f, 4.5f));particleSystem.addParticleModifier(new ColorModifier(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 11.5f));particleSystem.addParticleModifier(new AlphaModifier(1.0f, 0.0f, 4.5f, 11.5f));scene.getTopLayer().addEntity(particleSystem);}/* Right to left Particle System. */{final ParticleSystem particleSystem = new ParticleSystem(new PointParticleEmitter(CAMERA_WIDTH - 32, CAMERA_HEIGHT), 8, 12, 200, this.mParticleTextureRegion);particleSystem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE);particleSystem.addParticleInitializer(new VelocityInitializer(-15, -22, -60, -90));particleSystem.addParticleInitializer(new AccelerationInitializer(-5, 15));particleSystem.addParticleInitializer(new RotationInitializer(0.0f, 360.0f));particleSystem.addParticleInitializer(new ColorInitializer(0.0f, 0.0f, 1.0f));particleSystem.addParticleModifier(new ScaleModifier(0.5f, 2.0f, 0, 5));particleSystem.addParticleModifier(new ExpireModifier(11.5f));particleSystem.addParticleModifier(new AlphaModifier(1.0f, 0.0f, 2.5f, 3.5f));particleSystem.addParticleModifier(new AlphaModifier(0.0f, 1.0f, 3.5f, 4.5f));particleSystem.addParticleModifier(new ColorModifier(0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 11.5f));particleSystem.addParticleModifier(new AlphaModifier(1.0f, 0.0f, 4.5f, 11.5f));scene.getTopLayer().addEntity(particleSystem);}return scene;}

It can be found that apart from the change of the transmitter class, the number of transmitters has changed unexpectedly, and the others are almost identical. If the initialization and status modification are slightly different, you can also understand this as follows. Now, we are here today.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.