Cocos2dx Particle System

Source: Internet
Author: User

References:

Http://www.cnblogs.com/shangdahao/archive/2012/04/14/2447571.html

Http://blog.csdn.net/ganpengjin1/article/details/18554225

Http://blog.csdn.net/aa4790139/article/details/8124781

1. What is a particle system?

Particle systems first appeared in 1980s and are mainly used to solve the Computer Generation and display problems of a large number of tiny substances moving according to certain rules. Particle system is widely used. It can simulate atomic bomb explosion, Nebula changes, water waves, flame, fireworks, and clouds, however, it is difficult to reproduce these natural phenomena in a conventional graphic algorithm. Particle System can be said to be a method to solve problems based on physical models. Its core is not how to display, but how to extract rules for small material models. The rules of particle motion (Change) can be very simple or complex, depending on the object you simulate. You will find that the particle in particle system is similar to the class concept in C ++. In fact, you can treat it as a class. A particle is an instance object of a class, however, sometimes you need to discard classes and use simple and fast compact Code in terms of specific details related to program optimization. Although particle system is useful in processing the motion (variation) of a large number of individual particles, it involves scenarios where the interaction between particles needs to be considered, because the computing workload increases exponentially with the number of particles, it seems a little powerless. For example, we can simulate the movements of a large number of stars under mutual gravitation, And the collision of a large number of particles.

Ii. Use of particle Editor

There are many tutorials on how to download and use particle editor. I will not talk about it here.

3. Realization of Cocos2d-x on Particle System

In fact, for the particle system in cocos2dx, everything has been templated. Just do it. It mainly adjusts the parameter configurations in the particle system. The following describes how to support the Particle System in cocos2dx.

(1) effect of using cocos2dx packaged particle system parameters:

Ccparticleexplosion explosion
Ccparticlefire
Ccparticlefireworks fireworks
Ccparticleflower
Ccparticlegalaxy
Ccparticlemeteor popular
Ccparticlerain rain
Ccparticlesmoke snow
Ccparticlesnow smoke
Ccparticlespiral spiral
Ccparticlesun

For example:

 CCParticleSystem* m_emitter; m_emitter = CCParticleFireworks::node(); m_emitter->retain(); this->addChild(m_emitter, 10); m_emitter->setTexture( CCTextureCache::sharedTextureCache()->addImage(“image.png”) ); m_emitter->setPosition(ccp(489,320));

(2) using particle editor to edit parameters

In this way, we will use the following two methods to import the generated (*. plist) file.

 bool initWithFile(const char *plistFile) static CCParticleSystem* create(const char *plistFile)

For example:

Ccparticlesystemquad * system = new ccparticlesystemquad (); system-> initwithfile ("images/spinningpeas. plist "); // The plist file can be obtained through the example editor system-> settexturewithrect (cctexturecache: sharedtexturecache ()-> addimage (" images/Participant s.png "), ccrectmake, 32, 32); addchild (system, 10); system-> setposition

(3) Use code to configure Particle System Parameters

CCParticleSystem* m_emitter;m_emitter = new CCParticleSystemQuad(); m_emitter->initWithTotalParticles(50); this->addChild(m_emitter, 10); m_emitter->setTexture( CCTextureCache::sharedTextureCache()->addImage(s_stars1) ); m_emitter->setDuration(-1);      // gravity m_emitter->setGravity(CCPointZero);      // angle m_emitter->setAngle(90); m_emitter->setAngleVar(360);      // speed of particles m_emitter->setSpeed(160); m_emitter->setSpeedVar(20);      // radial m_emitter->setRadialAccel(-120); m_emitter->setRadialAccelVar(0);      // tagential m_emitter->setTangentialAccel(30); m_emitter->setTangentialAccelVar(0);      // emitter position m_emitter->setPosition( CCPointMake(160,240) ); m_emitter->setPosVar(CCPointZero);      // life of particles m_emitter->setLife(4); m_emitter->setLifeVar(1);      // spin of particles m_emitter->setStartSpin(0); m_emitter->setStartSizeVar(0); m_emitter->setEndSpin(0); m_emitter->setEndSpinVar(0);      // color of particles ccColor4F startColor = {0.5f, 0.5f, 0.5f, 1.0f}; m_emitter->setStartColor(startColor);    ccColor4F startColorVar = {0.5f, 0.5f, 0.5f, 1.0f}; m_emitter->setStartColorVar(startColorVar);    ccColor4F endColor = {0.1f, 0.1f, 0.1f, 0.2f}; m_emitter->setEndColor(endColor);    ccColor4F endColorVar = {0.1f, 0.1f, 0.1f, 0.2f};  m_emitter->setEndColorVar(endColorVar);      // size, in pixels m_emitter->setStartSize(80.0f); m_emitter->setStartSizeVar(40.0f); m_emitter->setEndSize(kParticleStartSizeEqualToEndSize);      // emits per second m_emitter->setEmissionRate(m_emitter->getTotalParticles()/m_emitter->getLife());      // additive m_emitter->setIsBlendAdditive(true);

Cocos2dx Particle System

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.