开发者

Collision detection with particles in Cocos2d

I am using CCParticleSystemQuad to create a particle effect. Now I would like to test for collisions with a CGRect from my Cocos2d scene. I listed another subject similar to this one and got a little closer however I still do not have the full solution so I have re-listed with a slightly different subject title.

I have half of the solution. I can get the position of each particle and can test for collisions, now I would l开发者_Python百科ike to set the positions of each when they collide. I am currently subclassing the CCParticleSystemQuad and then adding my own getter like so:

-(tCCParticle*)getQuadParticle:(int)quadIndex
{
    return &particles[quadIndex];
}

Then in my Cocos2d scene I can get the particle and the position:

tCCParticle *particle = [emitter getQuadParticle:i];
CGPoint pos = particle->pos;

This works but warns that CCParticleSystemQuad may not respond to getQuadParticle. Which is a concern, but what I would like to do now is set the position from the scene in a similar fashion such as:

[emitter setParticlePos:i newPosition:newPos];

However I am not sure how to make a setter that does this that works from my scene. I don't want to do collision detion inside the particle subclass if possible.

I started another topic of similar nature called "How to get particle position in Cocos2d (iphone)" and I was told to overide the "update" method or the "updateQuadWithParticle" method but I am unsure how to go about this exactly.

If someone could show me an example of how to do this I would be most grateful.


This works but warns that CCParticleSystemQuad may not respond to getQuadParticle.

For the warning, make sure your emitter is made from your subclass (and not a regular CCParticleSystemQuad), and that your getter method is declared in the interface (.h file) and defined in the implementation (.m file).

Looking at the API, I don't see a method for setParticlePos:newPosition: but there is something that looks similar: -(void) updateQuadWithParticle:(tCCParticle*)particle newPosition:(CGPoint)pos;

I have not used it, but a glance at the source suggests it does what you need.

So maybe try [emitter updateQuadWithParticle:particle newPosition:newPos];

Hope that's helpful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜