Particles - using bare sprite rather than colours


#1

Hi,

I’ve been playing around with particle systems and I’m wondering how to use sprites as they are rather than applying arbitrary colours as shown below:

ccColor4F startColor = {0, 0, 0, 1};
_emitter~~>setStartColor;
ccColor4F startColorVar = ;
*emitter~~>setStartColorVar;
ccColor4F endColor = ;
_emitter~~>setEndColor;
ccColor4F endColorVar = ;
*emitter~~>setEndColorVar(endColorVar);

Does anyone know how to use sprites as-is?


#2

I haven’t tried this out but this route looks promising.

Look at the emitter’s setTexture() function. It wants a CCTexture2D to be set so you will need to create one (or obtain one). There appears to be a few different ways you can do this, assuming you loaded a sprite sheet in to the
sprite frame cache, it looks like you do this:

CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
CCSpriteFrame* frame = cache->spriteFrameByName("MyImage.png");
_emitter->setTexture(frame->getTexture());

If you’re not using CCSpriteFrameCache then it looks like you can create an image (using CCImage) and use that to create a new CCTexture2D to set for the emitter.


#3

Cheers for your response, I’m already using the following:

_emitter->setTexture(TextureCache::sharedTextureCache()->addImage("particle_grass.png"));

The problem is that although the sprite is used for the particles, all you really get is the shape of the sprite in an arbitrary colour, rather than the natural colours of the sprite.

Without the following:

ccColor4F startColor = {0, 0, 0, 1};
_emitter~~>setStartColor;
ccColor4F startColorVar = ;
*emitter~~>setStartColorVar;
ccColor4F endColor = ;
_emitter~~>setEndColor;
ccColor4F endColorVar = ;
*emitter~~>setEndColorVar(endColorVar);

No particles will even appear


#4

What happens if you have both startColor and endColor be (0, 0, 0, 1)? Don’t set any variance on color.


#5

Aha! It kind of works… The particles start off fine but once they’re recycled and used again they’re just black. They seem to be fading out but I can’t see any setting for this. Any tips?

    ParticleSystemQuad *_emitter = new ParticleSystemQuad();
    _emitter->initWithTotalParticles(200);
    addChild(_emitter, 10);
    _emitter->setTexture(TextureCache::sharedTextureCache()->addImage("particle.png"));

    // duration
    _emitter->setDuration(kParticleDurationInfinity);

    // radius mode
    _emitter->setEmitterMode(kParticleModeRadius);

    // radius mode: start and end radius in pixels
    _emitter->setStartRadius(0);
    _emitter->setStartRadiusVar(0);
    _emitter->setEndRadius(160);
    _emitter->setEndRadiusVar(0);

    // radius mode: degrees per second
    _emitter->setRotatePerSecond(180);
    _emitter->setRotatePerSecondVar(0);


    // angle
    _emitter->setAngle(90);
    _emitter->setAngleVar(0);

    // emitter position
    Size size = Director::sharedDirector()->getWinSize();
    _emitter->setPosition(ccp(size.width/2, size.height/2));
    _emitter->setPosVar(PointZero);

    // life of particles
    _emitter->setLife(5);
    _emitter->setLifeVar(0);

    // spin of particles
    _emitter->setStartSpin(0);
    _emitter->setStartSpinVar(0);
    _emitter->setEndSpin(0);
    _emitter->setEndSpinVar(0);

    // color of particles
    ccColor4F startColor = {0, 0, 0, 1};
    _emitter->setStartColor(startColor);

    ccColor4F endColor = {0, 0, 0, 1};
    _emitter->setEndColor(endColor);

    // size, in pixels
    _emitter->setStartSize(32);
    _emitter->setStartSizeVar(0);
    _emitter->setEndSize(kParticleStartSizeEqualToEndSize);

    // emits per second
    _emitter->setEmissionRate(1);

    // additive
    _emitter->setBlendAdditive(false);

#6

Maybe someone else will know more about this than I do but I can only guess that the underlying cached texture is being modified (which sounds like a bug). You could try creating a whole new texture every time to see if it would work (though a less appealing solution).


#7

A wild guess : have you tried setting the color variances explicitly?


#8

Pawel Lopusinski wrote:

A wild guess : have you tried setting the color variances explicitly?

Yeah I haven’t been able to make any difference yet though. Currently the sprites spawn and weirdly change to black, or spawn black. And their alpha seems to change, for what reason I have no clue.

    ccColor4F pcolor = {0, 0, 0, 1};
    _emitter->setStartColor(pcolor);
    _emitter->setEndColor(pcolor);

#9

Well, I think I know what is wrong. I checked the params in Particle Designer and the obvious struck me : the {0, 0, 0, 1} color is BLACK! :smiley:
So in order to not change the color, the setting should like like that :

startColor endColor {1, 1, 1, 1}
sColorVar eColorVar {0, 0, 0, 0}

Hope this helps!


#10

Give it a shot but I think that’ll set the sprite to white ;).

While yes, {0, 0, 0. 1} is black, I think it might be additive in this case? If so then {0, 0, 0, 1} should be what he wants.

Also your variance has alpha being 0, which may not give the intended effect.

Though, it’s cheap to try, so worth a shot :).


#11

As I said : I checked with ParticleDesigner : setting the color to { 1, 1, 1, 1 } on a red particle sprite resulted in this sprite having unchanged color.
Secondly : alpha variance should be equal to 0, because the variance parameters specify how much they can change, so if you put 0 there, such parameter cannot change.

Edit: I’ve added screenshots with examples. On the second one you can see, that with ColorVar.alpha = 1 the particles get some transparency so you can see ones beneath them (the lighter spots).


#12

Pawel Lopusinski wrote:

Well, I think I know what is wrong. I checked the params in Particle Designer and the obvious struck me : the {0, 0, 0, 1} color is BLACK! :smiley:
So in order to not change the color, the setting should like like that :
>
startColor endColor {1, 1, 1, 1}
sColorVar eColorVar {0, 0, 0, 0}
>
Hope this helps!

I found this out after some tinkering late last night. I too thought this would make them white, but oh well.

Cheers!


#13

Cool, will remember :).


#14

Fore me it’s just black