Chipmunk collision callback not triggered

Chipmunk collision callback not triggered
0.0 0


Hi everyone,

So, I recently included the chipmunk in my iPhone/Android project for a mini-games which uses a little bit of physic.
The game is very simple, you grab objects and throw them so they fall into containers, with a couple of obstacles in the way
However, I’m having a problem where the callback for the collision isn’t called.

This is how I create my space and add my collision handler:

// CREATE SPACE cpInitChipmunk(); space = cpSpaceNew(); space->elasticIterations = 10; cpSpaceAddCollisionHandler(space, 1, 1, &spriteCollision, NULL, NULL, NULL, this); // Listen for collisions

Here the declaration and definition of the spriteCollision function:

static int spriteCollision(cpArbiter *arb, cpSpace *space, void *data) { if(DEBUG_FLAG) cocos2d::CCLog("#DBG sprite collision"); }

The initialisation occurs into the GameScene class, which is a subclass of CCScene, init function,
while the spriteCollision function is just a static function that doesn’t belong to the class but is declared at the top of the GameScene.cpp file.

I tried to make the spriteCollision function a static member of the class, pass it as “spriteCollision” instead of “&spriteCollision”, both made no difference.

Did anyone have the same problem ? Am I missing something?

void cpSpaceAddCollisionHandler(
    cpSpace *space,
    cpCollisionType a, cpCollisionType b,
    cpCollisionBeginFunc begin,
    cpCollisionPreSolveFunc preSolve,
    cpCollisionPostSolveFunc postSolve,
    cpCollisionSeparateFunc separate,
    void *data

typedef cpBool(*cpCollisionBeginFunc)(cpArbiter *arb, cpSpace *space, void *data);

The function prototypes look almost correct (int instead of cpBool?). Put a breakpoint in your game loop and double check that your bodies(and/or shapes) have the correct settings, cpCollisionType for instance.