Please help me with cpSpaceAddCollisionPairFunc

Official forum for the Chipmunk2D Physics Library.
Post Reply
mbue
Posts: 7
Joined: Tue Sep 30, 2008 8:45 am
Contact:

Please help me with cpSpaceAddCollisionPairFunc

Post by mbue »

Hi all,

I try to use chipmunk collisions to tell me if a car collided with a powerup in my game.
Unfortunately I'm having problems with cpSpaceAddCollisionPairFunc right now.

My stripped down code looks like this:

Code: Select all

int Phys_CollectPowerup (cpShape *a, cpShape *b, cpContact *contacts, int numContacts, cpFloat normal_coef, void *data)
{
[...]
}

static void Phys_Init (void)
{
	cpInitChipmunk();
	e.physics = cpSpaceNew();
[...]
	cpSpaceAddCollisionPairFunc(e.physics, COLL_CAR, COLL_POWERUP, &Phys_CollectPowerup, NULL); //&some_value);
}
EDIT Full code here
COLL_CAR and COLL_POWERUP are 1 and 2 respectively.

They are set for cars and powerups like this

Code: Select all

shape->collision_type = COLL_CAR;
and
shape->collision_type = COLL_POWERUP;
before adding the shapes and bodies to the space.

My problem is that Phys_CollectPowerup is never called.
Any ideas what I could've missed?
Is the order of adding shapes/bodies and the collision pair function important?

Thanks in advance,
Martin
supertommy
Posts: 56
Joined: Tue Sep 11, 2007 2:30 pm
Contact:

Re: Please help me with cpSpaceAddCollisionPairFunc

Post by supertommy »

I think the code you posted looks ok.

Do the cars and powerups collide if you remove the function?

If, for instance, the car and the powerups are on different layers or in the same group, the collision function won't be called (as far as I can remember.)
mbue
Posts: 7
Joined: Tue Sep 30, 2008 8:45 am
Contact:

Re: Please help me with cpSpaceAddCollisionPairFunc

Post by mbue »

supertommy many thanks for the answer.
I did rule out all your suggestions and realized that I didn't draw the powerup at the correct place (i.e. body->p was not set correctly, but it was draw at the right place anyway) :)

Martin
Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests