Particles + movable/scalable layer.

Particles + movable/scalable layer.
0.0 0


Hi there. Thanks for cocos2dx. I’m new to it, but it seems to be quite promising.
I ran into a problem with the particles and the layer being moved and / or scaled. I’ve got a layer which is a main layer of the scene, it contains all visible game objects. I need to have it movable and scalable to imitate camera movement and zooming.
Also I’ve got some sprite, let’s assume it’s a rocket. Rocket has to have a long fire trail which is done with particle system. Since rocket can change its direction of movement I need those particles to be free (not attached to the emitter). And here comes the problem, if I move or scale the layer, the particles in the rocket’s trail are moved too, causing totally unrealistic effect.
The bad thing is that this behavior is correct from that point of view that the particle system is a child of the layer being moved. It seems that the root of the problem is that we have to move the layer to fake the movement of the camera. If we had a real camera, no problem would appear. But it seems cocos2dx doesn’t have such an object as camera (in traditional meaning, like in other engines like Unity etc)? Am I wrong?
How to deal with this case?


Setting the emitter’s position type may fix this for you.


Here’s the different enumerations:

/** @typedef tCCPositionType
possible types of particle positions
typedef enum {
    /** Living particles are attached to the world and are unaffected by emitter repositioning. */

    /** Living particles are attached to the world but will follow the emitter repositioning.
    Use case: Attach an emitter to an sprite, and you want that the emitter follows the sprite.

    /** Living particles are attached to the emitter and are translated along with it. */


Hi. Thanks for your reply.
But I’m already aware of different emitter’s position types and I’m using the free one.
Here is a video illustrating my problem The rocket flies with a small torque, causing the trail to curve. During the first 8 seconds I do not move the parent layer and everything seems ok. But then I start moving the layer and the particles stop forming a nice looking trail, which I don’t want to happen.
In other engines this situation can’t even happen since they have a ‘camera’ object which can be moved, zoomed etc. In cocos2d we have to manipulate the parent layer to imitate the camera movement.
How to fix this problem?


Oh I see, the issue you’re seeing is because the position type is free. You want the emitter to be relative to the layer but follow the sprite. So set the position type to relative, add it as a child to the layer and tell it to follow the sprite.

    CCSprite* sprite = CCSprite::create();

    CCParticleSun* emitter = CCParticleSun::create();
    CCFollow* followAction = CCFollow::create(sprite);

    addChild(emitter); // This is the layer!


Wow, that worked. Thanks a lot!
I was so close to solution. I’ve just replaced kCCPositionTypeFree with kCCPositionTypeRelative.

But that’s not the end of the story. Now the trail of the rocket looks slightly different: it seems like the particles are spawning at discrete positions. Well, it’s easier to show than to explain The left rocket has a ‘relative’ emitter, the right one has a ‘free’ emitter. All the other settings of the particle systems are the same.
Any ideas why this is happening and how to make the ‘relative’ particle system look better?


Without understanding the node tree that your game has structured and what all is moving about, this is a little tough to reason out. But I can give it a shot :).

The second rocket has a position type of free, so each particle spawned stays where it spawned in the world coordinates. The first rocket is relative to the parent layer it was added to. The particle stays spawned where it spawned relative to it’s parent. So we’re seeing longer tail (or more distance between particle spawns). Is the layer underneath moving as well? That might cause this behavior.

Not sure for sure. If you can describe the structure and what all is in motion, it might be easier to figure out.

EDIT Is the sprite and emitter children of the same layer?


Ah, you are right again. The parent layer follows one of the rockets (through CCFollow action).
The node tree looks as follows:

Space Layer
 ^  ^  ^  ^
 |  |  |  |
 |  |  |  |____ Background Sprite
 |  |  |
 |  |  |_______ Rocket Node
 |  |               ^
 |  |               |______________ Rocket Sprite
 |  |
 |  |__________ Rocket Particle System
 |_____________ Some other nodes

where <- means relationship: parent <- child


I haven’t dealt too much with the particle emitters so I’m figuring through this with you :).

The emitter on the right is following the sprite (which is moving x speed) and spawning a particle that stays in the world coordinate. So that particle spawned is appears to move x speed . The emitter on the left is also following the sprite but the particle spawned stays on that spot relative to the layer which is moving atx. This gives the appearance of each particle moving at –2x speed. This is why there’s a gap between each particle and why the tail is twice as long.

So to get tail to the same length, you should have half the life for each particle. To prevent the space between each particle, double the number of particles emitted?

See what that does :).


Hi. Thanks for your help.

Yeah, you are right again. Since the parent layer follows the rocket, the particles are moving at double speed. The problem is that I don’t need the parent layer to follow the rocket permanently: sometimes it may follow other objects or follow no objects at all. This overcomplicates the particle management.
Doubling the number of particles does not prevent the gap between particles.

Actually, it seems that these flaws don’t break the game’s appearance, since the art design should mask them. However, I’d still like to solve these problems, because it is possible to run into them in the future.


I meant double the emission rate, my bad :).

Or half the time you emit a particle, whichever makes sense for your particle emitter.

And since these particles are relative to the layer they are on. Once you get the tail looking how you want, it shouldn’t matter which node (if any) the layer is following.


Well, since I’ve changed the texture of the particles and the whole appearance changed too, it’s hard to say if doubling the number of particles or emission rate fixes the problem with the gap between particles or not.
Anyway, now I’m satisfied, unless I face some other tricky nuances of cocos2dx.


Thank you so much for this thread — very helpful!