Cocos Basic Tutorials (8) Particle effects

Source: Internet
Author: User
Tags addchild

Brief introduction

Particle system refers to the technology of simulating specific phenomena in computer graphics, which has advantageous advantages in imitating natural phenomena, physical phenomena and space distortion, and provides convenience for us to realize some real natural and random effects (such as explosion, fireworks, water flow).

Particle properties

A powerful particle system, it must have a variety of properties, so as to configure a variety of particles. Here's a look at the main properties of the particle system.

Main properties:
    • _duration transmitter survival time, that is, it can emit particles of time, note that this time and particle life time is different. Unit seconds, 1 means forever; After the particle launch, click the Play button on the toolbar to launch again.
    • _emissionrate number of particles emitted per second
    • _emittermode emitter mode, with Gravity mode (GRAVITY) and radius mode (radius, also called radial mode) two
    • The maximum number of particles present in the _totalparticles scene, often used in conjunction with _emissionrate.
    • _isautoremoveonfinish whether the particles are automatically deleted at the end
Gravity mode (Modea):

As the name implies, Gravity mode simulates gravity, allowing particles to move closer or farther around a central point, which has the advantage of being very dynamic and moving with rules. The following properties work only in Gravity mode.

    • Gravity Gravity X
    • Radiaaccel Particle radial acceleration, which is the acceleration parallel to the gravity direction
    • Variation range of radial acceleration of radiaaccelvar particles
    • Speed
    • Speedvar Speed Variation Range
    • Tangentialaccel particle tangential acceleration, which is the acceleration perpendicular to the gravity direction
    • Tangentialaccelvar Particle tangential acceleration variation range
Radius mode (MODEB):

Radius mode allows the particle to rotate in a circle, and it can also create a spiral effect that allows the particle to move forward or backward rapidly. The following properties work only in radius mode.

    • Endradius End Radius
    • Endradiusvar End radius Variation range, that is, the range of the end radius value is between (Endradius-endradiusvar) and (Endradius + Endradiusvar), similar to the following.
    • Rotatepersecond the rotation of particles per second around the starting point
    • The range of rotation of rotatepersecondvar particles around the starting point per second
    • Startradius Initial radius
    • Startradiusvar Initial RADIUS Variation range
Life attributes:
    • _life particle life, the time of particle survival
    • _lifevar Particle Life Change range
Size property:
    • _endsize the size of the particle at the end, 1 means the same as the initial size
    • _endsizevar particle End size Change range
    • Initial size of _startsize particles
    • Variation range of initial size of _startsizevar particles
Angle properties:
    • _angle Particle angle
    • _anglevar Particle angle Variation range
Color properties:
    • _endcolor Particle End Color
    • _endcolorvar particle end Color variation range
    • _startcolor Particle Initial Color
    • _startcolorvar Particle initial color variation range

If you don't want to edit the colorful particle effects, you should set the _endcolorvar,_startcolorvar as far as possible (0, 0, 0, 0).

Location Properties:
    • _positiontype particle position type with free mode (freely), relative mode (RELATIVE), and group mode (grouped) three kinds
    • _posvar Transmitter position variation range (horizontal and vertical)
    • _sourceposition Emitter Original coordinate position

Free mode, relative to the screen freedom, does not move with the particle node movement (can produce flame, steam and other effects); relative relative mode, the particle emitter moves with node as opposed to the bound node, and can be used to create special effects on mobile characters. The grouped pattern is relative to the launch point, and the particles move with the emitter.

Spin properties:
    • _endspin Particle End Spin angle
    • _endspinvar particle End spin angle variation range
    • _startspin Particle start spin angle
    • _startspinvar particle start spin angle variation range
Texture Rendering Properties:
    • _texture Particle Map Texture
    • _blendfunc texture Blending mode method, there are disable, alpha_premultiplied, alpha_non_premultiplied, additive four kinds.

DISABLE = {gl_one, gl_zero};
Alpha_premultiplied = {gl_one, gl_one_minus_src_alpha};
Alpha_non_premultiplied = {gl_src_alpha, gl_one_minus_src_alpha};
ADDITIVE = {gl_src_alpha, gl_one};

    • Gl_zero: Indicates that using 0.0 as a factor is actually equivalent to not using this color to participate in the blending operation.
    • Gl_one: The use of 1.0 as a factor is actually equivalent to fully using this color to participate in the blending operation.
    • Gl_src_alpha: Indicates that the ALPHA value of the source color is used as a factor.
    • Gl_dst_alpha: Indicates that the ALPHA value of the target color is used as a factor.
    • Gl_one_minus_src_alpha: Represents the ALPHA value of the source color minus 1.0 as a factor.
    • Gl_one_minus_dst_alpha: Indicates that the ALPHA value of the target color is subtracted from 1.0 as a factor.

The COCOS2DX uses the OpenGL blending principle to render the drawing. Mixing refers to mixing two colors together, the specific point is to a certain pixel location of the original color and will be painted in the color, by some way mixed together, so as to achieve special effects. It is a common technique that can often be used to achieve translucency, and you can get mixed results with different settings, producing interesting or strange images.

Particlebatchnode

Particlebatchnode, like a batch node, draws its child nodes by invoking OpenGL one time. An Particlebatchnode instance can refer to an object that references only one texture. Only when Particlesystem contains texture can it be added to Spritebatchnode. All Particlesystem added to the Spritebatchnode will be drawn inside the same OpenGL ES call. If Particlesystem is not added to the Particlebatchnode, OpenGL ES invokes the drawing function for each particle system, which is inefficient.

Limitations:

    • Currently only supports Particlesystemquad
    • All systems are drawn with the same parameters: mixed functions, textures, etc.

The most effective usage

    • Initialize Particlebatchnode textures and sufficient capacity for all particle systems
    • Initialize all particle systems and add them as child nodes of the batch node
Application in the program

Create a particle system and initialize its total number of particles:

Auto _emitter = particlesystemquad::createwithtotalparticles (+);

Full code

Auto _emitter = Particlesystemquad::createwithtotalparticles ( -); AddChild (_emitter,Ten); _emitter->settexture (Director::getinstance ()->gettexturecache ()->addimage ("Closenormal.png")); _emitter->setanchorpoint (Point (0,0)); //Duration_emitter->setduration (particlesystem::D uration_infinity); //radius Mode_emitter->Setemittermode (Particlesystem::mode::radius); //radius mode:start and end radius in pixels_emitter->setstartradius (4); _emitter->setstartradiusvar (1); _emitter-Setendradius (Particlesystem::start_radius_equal_to_end_radius); _emitter->setendradiusvar (0); //radius mode:degrees per second_emitter->setrotatepersecond ( -); _emitter->setrotatepersecondvar (0); //Angle_emitter->setangle ( -); _emitter->setanglevar (0); //Emitter PositionAuto size = Director::getinstance ()getwinsize (); _emitter-Setposvar (Point::zero); //Life of particles_emitter->setlife (0.5); _emitter->setlifevar (0); //Spin of particles_emitter->setstartspin (0); _emitter->setstartspinvar (0); _emitter->setendspin (0); _emitter->setendspinvar (0); //Color of particlescolor4f StartColor (0.0f,0.8f,0.9f,1.0f); _emitter-Setstartcolor (StartColor); color4f Startcolorvar (0,0,0,1.0f); _emitter-Setstartcolorvar (Startcolorvar); color4f EndColor (1.0f,1.0f,1.0f,0.1f); _emitter-Setendcolor (EndColor); color4f Endcolorvar (0,0,0,0.1f); _emitter-Setendcolorvar (Endcolorvar); //size, in pixels_emitter->setstartsize ( -); _emitter->setstartsizevar (1); _emitter->setendsize (0); //emits per second_emitter->setemissionrate (_emitter->gettotalparticles ()/_emitter->Getlife ()); //additive_emitter->setblendadditive (false); //AddChild (_emitter);_emitter->setposition (Point ( $, $));

Cocos Basic Tutorials (8) Particle effects

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.