Cocos2d-x 3.0 tip (02)-PositionType)

Source: Internet
Author: User

The cocos2dx particle system has three types of positions. For me, the most common is FREE, because once such particles are emitted, the position will no longer follow the Node of the particle system (including its parent Node). Moving the particle system can form a so-called "tail", which looks more natural.


The position of the free particle is still the local space coordinate under the Particle System (although theoretically, once the free particle is emitted, it should become one dollar of the world space, its coordinate transformation is no longer related to the nodes and transmitters of the particle system. Because the parameter of the influencer is set in the local space of the particle system.

When rendering a particle, you need to change the position of the particle from the local space to the world space. If the position of the particle system node changes, it will be reflected in this transformation process. The result is that the particle will move along with the node and cannot form a "tail" effect.

To realize the free particle free Motion in the world space, cocos2dx modifies the coordinates after each frame updates the particle coordinates (in the local space, then, the corrected coordinates are used for rendering calculation. The purpose of this correction is to offset the changes in the position of the particle system node or its parent node, making the particle look like a free movement in the world space.


For example, if a particle system P is directly attached to scene, the coordinate is (100,200), and a particle is emitted. the coordinate of the particle under P is ), then the coordinates of the particle in the entire scene are (100,200 ).

Assuming that the particle moves 10 horizontally to the right, the coordinates of the particle under P are (110,200), and the coordinates under scene are ).

If at the same time, P moves 50 to the left and the coordinate changes to (50,200), then the coordinate of the particle under P remains unchanged ), but in scene, it is (60,200 ).

For free particles, it cannot be affected by the movement of P to the left of 50, and must be kept at the position of (110,200) under scene.

The correction made by cocos2dx is to calculate and write down the world coordinates of the particle system (in the above example (100,200) during particle emission )). Then, when each frame updates the particle coordinate, it calculates the global coordinate of the Particle System (50,200 in the above example), the difference between subtraction, and adds it to the particle, in this way, the coordinate of the particle under P is (60, 0), and after the transformation to scene is (110,200 ). The corrected coordinates (60, 0) will not be recorded, and the next frame will be corrected again based on the new coordinates of P.


If there is no special requirement, this is OK. However, if the particle system or its parent node scales, rotates, and tilts, it will find that when the particle system is moving, the particles are messy. Cocos2dx uses the Coordinate Difference of the world space to correct the coordinates of a local space. Once the two spaces have different sizes of the referenced coordinates, confusion may occur.

In the preceding example, we assume that P is scaled to (0.5, 0.5 ). Since the coordinates of P itself are (50,200) after moving, the difference value is still (50, 0) when the particle is corrected, after the particle is corrected, the coordinates under P are still (60, 0 ). Then, when the particle is transformed to the world space, because of the scale ratio of P (0.5, 0.5), the world coordinates of the particle are actually x = 60*0.5 + 50 = 80, y = 0*0.5 + 200 = 200. The result is (80,200), not the expected (110,200 ).


At present, my solution does not correct free particles. Instead, it calculates the world coordinates of the particles for each frame in a theoretical way. Specifically:

1. When initializing a particle, record the transformation matrix of the particle system relative to the world. This matrix can convert the local coordinates of a particle system into the world coordinates. In addition, this matrix is unique to each particle;

2. Each frame still calculates the local coordinates of the particle in the particle system, but when it is used to calculate the coordinates of four vertices, multiply the matrix obtained in step 1;

3. the vertex coordinates obtained in step 2 are actually coordinates in the world space. Therefore, you must disable visit (or draw) to transform the particle coordinates. Ignore the transform passed in as the parameter and replace it with a unit matrix.

In this way, we are done.

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.