How to add variables into the cpSpaceAddCollisionHandler

Official forum for the Chipmunk2D Physics Library.
Post Reply
lenny0929
Posts: 1
Joined: Sun Jun 13, 2010 2:57 pm
Contact:

How to add variables into the cpSpaceAddCollisionHandler

Post by lenny0929 »

I need to return my own class in the cpSpaceAddCollisionHandler that can help me use the function that l wrote

say, i have a class called "DemoLayer" in object-c

in the cpSpaceAddCollisionHandler(space, BUG_TYPE, VEGI_TYPE, begin , NULL, NULL, NULL,NULL);

i notice that after collision of type BUG_TYPE and VEGI_TYPE

it will trigger the event begin,

static void begin(cpArbiter * arb, cpSpace * space, DemoLayer *glayer)
{
cpShape *a, *b;
cpArbiterGetShapes(arb, &a, &b);
return 0;
}

i can't retrieve the variable glayer correctly

how can i do??
User avatar
Tam Toucan
Posts: 141
Joined: Tue Jun 23, 2009 4:26 pm
Contact:

Re: How to add variables into the cpSpaceAddCollisionHandler

Post by Tam Toucan »

The final parameter of the cpSpaceAddCollisonHalder is the data parameter (the third argument) passed to your function.

I am suprized you can register your begin function since it's signature does not match the required type (I would expect any decent compiler to at least give a warning). The final parameter should be a void* which you then need to cast to the actual type (DemogLayer* in your case)
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: How to add variables into the cpSpaceAddCollisionHandler

Post by slembcke »

Yup. The last parameter of the event callback is the last parameter to the call to register the handler.

You have two options for defining the function. Either make the last parameter of the begin)( function void * so it's type matches the definition. This means that you have to cast it to the type you want inside the function. The second option is to cast the value of begin that you pass to cpSpaceAddCollisionHandler() to cpCollisionBeginFunc. Somebody pointed this out to me on the forums and I actually prefer it now.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 26 guests