Why Particle System
If you are playing a cartoon game, you can use animations to achieve all the game's visual effects. This is the explosive effect in zyg006. However, if we need more real effect simulation to achieve more random and lifelike smoke, lightning, rain, snow, and spark drops, we must use the particle system. The particle system makes your game more real and full of life (Ben Britten Smith-iPhone advanced projects).
Through the analysis of these natural phenomena, we found that the appearance of these phenomena does not involve a touchable and clear entity, in addition, the phenomenon itself is dynamic (rapidly changing over time), and the effect of this change is composed of a large number of tiny particles, A large number of particle effects are superimposed on the overall effect we see. In this way, it is difficult to simulate such effects for clear images and images that can be expressed using mathematical formulas, this is because it contains a large number of fuzzy meanings, such as uncertainty, randomness, and chaos.
In physics, the discipline that involves a large number of particles having irregular movements is thermal statistics. The concept that makes everyone familiar is "entropy ". The typical description of this physical quantity is the second law of thermodynamic, also known as "entropy increasing principle ". Of course, there is another inevitable concept that is even more confusing, but indeed helpless ".
Whether it's our subjective analysis and judgment, or the basic laws of thermal statistics, the common understanding is that the existence of these natural phenomena involves a large number of particles with irregular movements. Therefore, in order to simulate the phenomena produced by such a system, we need to establish such a particle system:
1) contains a large number of so-called particle objects (particles ).
2) Macro Characteristics: The main rule that every example must follow.
3) microscopic characteristics: the random and variant characteristics of each example on the macro attributes.
4) process dynamic characteristics: each particle system simulates a constantly changing dynamic effect rather than a static one. Therefore, this requires continuous updating.
According to the pre-designed rules, a large number of particles are constantly generated and sprayed. Each particle is constantly changing and moving according to its own motion parameters. A large number of particle effects are superimposed into the macro phenomenon we need. In this way, we can simulate many lifelike natural effects.
Around 1982-1983, a game developer called William Reeves first used such a system. HisAlgorithmThe overall control of the particle swarm is realized at the macro level, and the irregular and disorderly motion of each particle is ensured at the micro level. This is a perfect new experience for the game system.
Of course, adding a few pieces of particle effects to some simple cartoon games and card games can also bring a pleasant and exciting visual experience to players. Make your game more refined and cherished.
Composition of particle systems
An example system usually consists of the following key components:
LParticle)
Each particle is a graphical object that enables a single color point or an image. The following is the definition of cocosnoded particles: (File ccparticlesystem. h)
Typedef struct sparticle
{
Cgpoint Pos; // A location
Cgpoint startpos; // Start position of
Cgpoint Dir; // A direction of motion
FloatRadialaccel; // Radial Acceleration. Escape from the origin
FloatTangentialaccel;// Tangent acceleration. Rotate speed around the origin
Cccolor4fColor; // A color
Cccolor4fDeltacolor; // B color change
FloatSize; // A size
FloatDeltasize; // B Size Change
FloatAngle; // A Angle
FloatDeltaangle; // The number of changes in the B Angle
FloatLife; // Time when a exists
} Particle;
Each particle has its own independent attributes. According to the previous definition of the particle system, there are two categories:
Class A describes the main attributes of particles, that is, the macro characteristics required by the macro system.
Class B represents the rule-free attribute change in each example, that is, the microscopic attribute.
It should be noted that when a transmitter object is generated, the personality of each particle is set based on the macro characteristics, class B variables Save the amount of one-step transformation of this change in the lifecycle.
LEmitter)
The transmitter object is the whole of a particle system. A cloud, fog, lightning, and smoke are all simulated by an independent particle system, the definition of the Particle System in the Cocos2d-iPhone is as follows: (File ccparticlesystem. h ):
@ Interface ccparticlesystem: ccnode <cctextureprotocol>
{
Int ID;
// Is the particle system active?
Bool active;
// Duration in seconds of the system.-1 is infinity
Float duration;
// Time elapsed since the start of the system (in seconds)
Float elapsed;
/// Gravity of the participant
Cgpoint gravity; // The vertical and horizontal velocity of all particles of.
// Position is from "superclass" cocosnode
// Emitter centerofgravity position
Cgpoint centerofgravity; // particle gravity origin.
// Position variance
Cgpoint posvar; // The wide and high variation range of the B particle eruption origin.
// The angle (Direction) of the participant measured in degrees
Float angle; // A particle eruption Angle
// Angle variance measured in degrees;
Float anglevar; // Change range of B particle eruption Angle
// The speed the participates will have.
Float speed; // A particle motion speed
// The speed variance
Float speedvar; // The variation range of the Motion Velocity of particle B
// Tangential acceleration
Float tangentialaccel; // Tangent acceleration of particle
// Tangential acceleration variance
Float tangentialaccelvar; // Change range of the tangent acceleration of particle B
// Radial Acceleration
Float radialaccel; // Radial Acceleration of particle
// Radial Acceleration variance
Float radialaccelvar; // Change range of Radial Acceleration of particle B
// Start ize of the participant
Float startsize; // Starting size of a particle
// Start size variance
Float startsizevar; // Size range of the starting part of particle B
// End size of the particle
Float endsize; // A particle size after completion
// End size of variance
Float endsizevar; // The size change range of particle B after completion
// How many seconds will the particle live
Float life; // A particle Lifecycle
// Life variance
Float lifevar; // Lifecycle change range of particle B
// Start color of the participant
Cccolor4f startcolor; // Start color of particle
// Start color variance
Cccolor4f startcolorvar; // The range in which the color of the starting B particle changes
// End color of the participant
Cccolor4f endcolor; // End COLOR OF PARTICLE
// End color variance
Cccolor4f endcolorvar; // The change range of the ending color of particle B
// Start angle of the participant
Float startspin; // Start spin angle of particle
// Start angle variance
Float startspinvar; // The change range of the starting spin angle of particle B
// End angle of the particle
Float endspin; // A particle ends the spin Angle
// End angle ariance
Float endspinvar; // The variation range of the end spin angle of particle B
// Array of Particles
Particle * participant;
// Maximum participant
Int totalparticipant;
// Count of active participant
Int particle count;
// Additive color or blend
Bool blendadditive;
// Color modulate
Bool colormodulate;
// How many particles can be emitted per second
Float emissionrate; // Particle eruption Rate
Float emitcounter;
// Texture of the participant
Cctexture2d * texture _;
// Blend function
Ccblendfunc Blendfunc _;
// Movment type: free or grouped
Tpositiontype positiontype _;
// Whether or not the node will be auto-removed when there are not participant
Bool Autoremoveonfinish _;
//Particle idx
Int particle idx;
}
Through the above definition, we can see that Class A attributes define the macroscopic characteristics of the entire particle system, and Class B attributes define the microscopic characteristics of particles, class B attributes are randomly assigned by the particle system by the random function ccrandom_minus1_1 when each microscopic particle is generated. Once activated, the random and chaotic effects of the entire system can be simulated.
Particle and transmitter objects describe all the attributes of a particle system's complexity. We can also add the above complexity by adding new A and B attributes based on our game design: flickering, random disturbance, and so on.
...
If you are interested in this article, you can download the complete PDF here. Download the sample source code.