How can I get sprites to detect collisions with the cocos2d particle systems? I wan't to be able to use the fire particles as a sort of top down flamethrower and whatever it touches it burns.
Particle System Collision Detection?
(6 posts) (4 voices)-
Posted 1 year ago #
-
A CCParticle[Type] inherits CCNode so you could check for its boundingBox intersecting with the boundingBox of your CCSprite i.e. you can basically check for collision in exactly the same way you would check for a collision with another CCSprite.
And also this forum thread
http://www.cocos2d-iphone.org/forum/topic/2878#post-17772
Regards,
Derek.Posted 1 year ago # -
I know how to map cgrect onto a ccsprite but I'm unsure how to go about the particles cgrect? Can you point me to some example code? Thanks.
Posted 1 year ago # -
There is (not surprisingly) a bit of jiggery-pokery required. (Please Note: referencing a superclass ivar is only allowed when compiling for the iPhone Simulator and will fail with a compiler error if attempting to compile for the iPhone Device. The @property below will need to be added to a future version of CCParticleSystem.h (or the cocos2d code to be patched as required) for this to work with the iPhone Device).
First we need to subclass the particle system to expose the array of particles in the super class CCParticleSystem e.g.
in GameScene.h
// // MyParticleFireworks // @interface MyParticleFireworks: CCParticleFireworks { } @property (nonatomic,readonly) Particle *particles; // :dbolli:100305 20:21:25 Expose array of particles @endand in GameScene.m
// // MyParticleFireworks // @implementation MyParticleFireworks @synthesize particles; // :dbolli:100305 20:21:25 Expose array of particles @endMake sure this goes before code which uses MyParticleFireworks e.g.
CCParticleSystem *emitter = [MyParticleFireworks node]; [emitter setPosition:ccp( s.width/2, s.height/6 )]; [self addChild:emitter z:kGameLayerZOrder tag:kParticleEmitterTag];Then in your -step: function iterate through your particles and see if their positions intersect with your sprite
CCSprite *esp = (CCSprite *)[self getChildByTag:kEarthSpriteTag]; MyParticleFireworks *em = (MyParticleFireworks *)[self getChildByTag:kParticleEmitterTag]; int pindex = 0; while ( pindex < em.particleCount ) { if ( CGRectContainsPoint( esp.boundingBox, em.particles[pindex].pos ) ) // :dbolli:100305 20:22:07 If particle collides with sprite, increment highscore sl.highscore++; pindex++; }I have created a demo to illustrate this (also features examples of Lam's brilliant ProgressTimer Class)
See http://public.me.com/dbolli/iphone%20projects/dosh-demo-cocos2d-iphone.zip
You will need to have cocos2d-iphone-0.99.0 at the same dir level as dosh-demo-cocos2d-iphone
and COCOS2D_SRC defined in Xcode Preferences->Source Trees with Path ../cocos2d-iphone-0.99.0 and Display Name cocos2d-iphone srcEnjoy :)
Posted 1 year ago # -
Is there a recent update to this? The property particles is now protected and it won't let me get to it.
Many thanks
Oliver
Posted 6 months ago #
Reply
You must log in to post.