Cocos2d Example – Collision
In my last article Cocos2d Example – Bouncing Ball, a little ball bounces up and down on the floor. Continue with that article, some effects will be made when the ball hits the floor. In order to achieve this, we first need to detect collisions between the ball and floor. Fotunately, ChipMunk can do this for us. What we need to do here is just using function cpSpaceAddCollisionPairFunc to register our callback function which can response the collision event.
Step 1: add the codes below to the end of method setupChipmunk.
cpSpaceAddCollisionPairFunc(space, 1, 0, &ballCollision, self);
The function above means callback function ballCollision will be invoked when two physical objects collide, whose collision_type are 1 and 0. Do you remember collision_type? It is the parameter of Shape. In the example of Bouncing Ball, the collision_types of ball and floor are set to 1 and 0 respectively. (Note: When two physical objects whose collision_type are not 1 and 0 collide, ballCollision will not be called.). self is the address of ballLayer. It will be passed to ballCollision as the data.
Step 2: Define the callback function ballCollision.
int ballCollision(cpShape *a, cpShape *b, cpContact *contacts, int numContacts, cpFloat normal_coef, void *data)
{
BallLayer *layer = (BallLayer*) data;
ParticleSystem* emitter = [ParticleExplosion node];
emitter.position = cpv(contacts->p.x, contacts->p.y);
emitter.life = 0.1f;
emitter.duration = 0.1f;
emitter.lifeVar = 0.1f;
emitter.totalParticles = abs(a->body->v.y)*0.05;
[layer addChild:emitter];
return 1;
}
The return value of callback function plays an important role here. If the return value is 0, the collision will be discarded (Note: but the callback function will be called). You will see the ball pass through the floor. The effects of collision we want to make is emitting particles, so an ParticleExplosion object is created. For the details of properties (e.g., life, duration, etc) of ParticleSystem object, please refer to the Cocos2d documentation. It should be noted that the totalParticles is determined by the falling speed. Therefore, when the ball bounces up and down on the floor, the explosion will become more and more weak.
After the two steps above, the explosion effect is made when the ball collide with floor.
—————————————
Update: You can download the sourcecode from here. Note: ParticleExplosion class will use fire.png to initialize. This file is in Resources/images fold of Cocos2d library. Please add fire.png to the project, otherwise, an exception will happen when the ball hits the floor.

Can you post some source code because this isn’t working for me. I build with no errors but as soon as the ball hits the floor the TERMINATING DUE TO UNCAUGHT EXCEPTION message shows.
to Austin,
The post was updated. Now you can download the source code.
Thanks! With the fire.png everything works fine.
Very helpful, thank you
This is very helpful, but the performance hits the floor after (I assume) several particle systems have been created. What is the proper way to share/cleanup emitters?
-Thanks!