Search code examples
cocos2d-iphonecollisionparticles

How to get particle position in Cocos2d (iphone)


I am using CCParticleSystemQuad to create a particle effect in Cocos2d. Now I would like to test each particle for collisions with a CCRect. How do I get the postions of each particle in the particle engine so I can do this?

Any help or examples would be appreciated. I've looked for hours on the net expecting to find tutorials on this. I am surprised I can't find much as I would expect collisions with particles to be essential; Perhaps I wasn't looking in the right place :)


Solution

  • Create a subclass of CCParticleSystemQuad and override update: method or updateQuadWithParticle:newPosition: method.

    @interface MyParticleSystem : CCParticleSystemQuad
    @end
    
    @implementation MyParticleSystem
    - (void)updateQuadWithParticle:(tCCParticle*)particle newPosition:(CGPoint)pos
    {
        /* use pos */
        [super updateQuadWithParticle:particle newPosition:pos];
    }
    @end
    

    EDITED:

    You can set any data (position, color, or so on) to the particles as the following.

    @interface MyParticleSystem : CCParticleSystemQuad
    @end
    
    @implementation MyParticleSystem
    - (void)update:(ccTime)dt
    {
        /* implement as cocos2d/CCParticleSystem.m -update: */
    }
    @end