The spring rain surprised the spring and Qing grain days, the summer is full and the summer heat is connected, the autumn is exposed and the autumn is cold, the winter is snow, the winter is small and the winter is cold.
In nature, some large-scale moving objects can be seen everywhere, such as rain, snow, Sparks During explosion, even rotating galaxy, and diffuse clouds.
You can see how beautiful the snow under the microscope is!
When we want to simulate these large-scale moving objects in a game, there are usually two methods. Use Frame Animation for simulation. Designing a frame animation and rendering it as an image sequence to simulate special effects not only makes the generated animation bulky, but also cannot adjust its motion parameters, and also loses its flexibility.
The concept of Particle effect. We regard each object as a particle and assign them certain attributes (such as appearance, position, speed, acceleration, and survival time ), make them generate, move, and eventually disappear according to certain rules. In the particle effect, there is usually an engine for unified scheduling of all particles, called the partical system, which is responsible for the generation of particles and changes the state of particles over time, and finally reclaim unwanted particles. Based on the dimension of the particle system, the particle system can be divided into two types: Two-Dimensional particle system and three-dimensional particle system.
The particle system that Cocos2d-x 3.0 provides us is implemented by the particle system class. Like other particle engines, the particle system implements particle control and scheduling. Operations on particles include the following. 1. particle generation: This part is also called the particle transmitter ). 2. Update particle state: the engine updates the position, speed, and other States of the particle over time. 3. Reclaim ineffective particles: After the particle's lifecycle ends, it will be recycled by the system.
Therefore, to create a particle effect, we need to define how the particles are generated and how the state changes. You can use multiple methods to initialize the object. We can create a particle system by specifying the number of particles, and then we need to set the appearance (usually a small texture), emission mode, and motion mode of the particles. Creating a brand new particle system is usually cumbersome, and in most cases we prefer to save the parameters of the Particle System in the file, and the Cocos2d-x uses the Plist file to save those parameters. Aside from the particle effect file Plist, if we already have a particle effect file, we can use the initialization method of the particle system to directly import a particle effect from the file. The related code is as follows:
static ParticleSystem * create(const std::string& plistFile);
The Plist file is essentially an XML file, which can be created or modified using any text editor. To create a new particle effect, we can find an existing Particle System Plist file under the test sample directory of the Cocos2d-x, modify and then use it.
In fact, the engine already has several built-in particle effects. As a sample of the particle system, they can be created with just a few lines of code, built-in particle effects: particle fire flame effect: particle sun effect: particle explosion effect: particle snow snowflake Effect
The particle system inherits from the Node and can be added to other nodes. Displaying a particle in a game is very simple. Directly Add the following code to the game scenario initialization method, and add a snowflake effect to the game scenario to create a Winter Atmosphere. Prepare a snowflake image snow.png and add the following code in the GameScene: init method:
ParticleSnow *snow = ParticleSnow::create(); snow->setPosition(Point(480,670)); snow->setTextureWithRect(Director::getInstance()->getTextureCache()->addImage(snow.png),Rect(0,0,32,32)); addChild(snow);
After successful addition, you can see the effect of snow dancing.
Texture is a required attribute for any particle system. Each particle in the particle system uses this Texture to render it. In this example, each particle is endowed with a snowflake texture, so that the snowflake falls.
Creating a new particle effect requires too many parameters. We can use the built-in particle example of the engine to achieve a particle effect: first, find a particle effect similar to the expected effect, then modify the texture and fine-tune the parameters.
The following example uses the particle snow as an example to describe how to set parameters:
Bool participant snow: Random (int numberofparticipant) {if (participant systemquad: initwithtotalparticipant (numberofparticipant) {// duration // interval _ duration = DURATION_INFINITY; // set gravity mode. // set it to GRAVITY Mode setEmitterMode (Mode: Gravity); // gravity Mode: GRAVITY // Gravity Mode parameter: gravity setGravity (Point (0,-1 )); // Gravity Mode: speed of participation // Gravity Mode parameter: particle speed setSpeed (5); setSpeedVar (1); // Gravity Mode: radial // Gravity mode parameter: radial Acceleration setRadialAccel (0); setRadialAccelVar (1); // Gravity Mode: tangential // Gravity mode parameter: Tangent acceleration setTangentialAccel (0 ); setTangentialAccelVar (1); // emitter position // particle transmitter position Size winSize = ctor: getInstance ()-> getWinSize (); this-> setPosition (Point (winSize. width/2, winSize. height + 10); setPosVar (Point (winSize. width/2, 0); // angle _ angle =-90; _ angleVar = 5; // life Of particles // particle life time _ life = 45; _ lifeVar = 15; // size, in pixels // size (in pixels) _ startSize = 10.0f; _ startSizeVar = 5.0f; _ endSize = START_SIZE_EQUAL_TO_END_SIZE; // emits per second // number of particles emitted per second _ emissionRate = 10; // color of particle/particle coloring _ startColor. r = 1.0f; _ startColor. g = 1.0f; _ startColor. B = 1.0f; _ startColor. a = 1.0f; _ startColorVar. r = 0.0f; _ startColorVar. g = 0.0f; _ startColorVar. B = 0.0f; _ startColorVar. a = 0.0f; _ endColor. r = 1.0f; _ endColor. g = 1.0f; _ endColor. B = 1.0f; _ endColor. a = 0.0f; _ endColorVar. r = 0.0f; _ endColorVar. g = 0.0f; _ endColorVar. B = 0.0f; _ endColorVar. a = 0.0f; Texture2D * texture = getdefatexttexture (); if (texture! = Nullptr) {setTexture (texture) ;}// additive // disable linear overlay hybrid mode this-> setBlendAdditive (false); return true ;}return false ;}
Particle editor a particle editor on windows called cocos2d-windows-particle-editor Open Source Address: http://code.google.com/p/cocos2d-windows-particle-editor/
Overview and description:
File: save, save as (export plist File), new, Open
Samples: There are some examples in test. The texture addition function is not added to the current version.
Editor function description:
Radius mode: This mode only involves center movement. The following parameters must be valid.
Editor: whether the IsBackgroundMove background is dynamic and scaled.
Size: particle start size and end size
Angle: The moving direction of the particle... (-90) vertical downward
Life: particle life
Location: Set the PosVar value. SourcePosition is unnecessary.
Texture rendering: In general cases, you only need to remember: "The most common CC_BLEND_SRC and CC_BLEND_DST correspond to GL_ONE and GL_ONE_MINUS_SRC_ALPHA respectively ".
Color: StartColor, EndColor, StartColorVar, and EndColorVar indicate the initial color, ending color, and floating value of the particle.
Gravity mode: Setting of gravity and speed
Spin: particle Rotation
EmissionRate: the particle emission rate, that is, the number of particles emitted per second.
Haomeng master friendly reminder: Use your imagination to create their own distinctive particle effects ,,,