I have a physics entity contained in an Objective-C class. I have many of these entities, and they're responsible for adding themselves to the physics space (through a singleton physics manager interface). This includes creating the callbacks to the entities collision methods.
Since I'm using Objective-C, I can't use any of the selectors as the callback function for cpSpaceAddCollisionHandler() ... IE, I am forced to do something akin to this:
Code: Select all
// C callbacks to invoke Objective-C method hooks >:(
int wall_begin(cpArbiter *arb, cpSpace *space, void *unused);
int wall_pre(cpArbiter *arb, cpSpace *space, void *unused);
void wall_post(cpArbiter *arb, cpSpace *space, void *unused);
void wall_end(cpArbiter *arb, cpSpace *space, void *unused);
@interface Polygon : DynamicEntity
{
cpVect polygonVerts[8];
int numSides;
... (other stuffs) ...
}
... (some methods) ...
// Obj-C collision method hooks so we have access to iVars and class data
-(int)wall_beginCollision:(cpArbiter *)arb :(cpSpace *)space :(void *)data;
-(void)wall_endCollision:(cpArbiter *)arb :(cpSpace *)space :(void *)data;
@end
Code: Select all
/* PHYSICS CALLBACKS */
int wall_begin(cpArbiter *arb, cpSpace *space, void *ptrToSelf)
{
int r = [(Polygon *)ptrToSelf wall_beginCollision :arb :space :ptrToSelf];
return r;
}
// Unused
int wall_pre(cpArbiter *arb, cpSpace *space, void *unused)
{
return 1;
}
// Unused
void wall_post(cpArbiter *arb, cpSpace *space, void *unused)
{
}
void wall_end(cpArbiter *arb, cpSpace *space, void *ptrToSelf)
{
[(Polygon *)ptrToSelf wall_endCollision:arb :space :ptrToSelf];
}
/* END PHYSICS CALLBACKS */
@implementation Polygon
... (stuff) ...
-(void)addCollisionHandlers
{
// Add collision callbacks for a wall
[_sharedPhysicsManager addCollisionHandler :TYPE_WALL
:TYPE_POLYGON
:wall_begin
:nil
:nil
:wall_end
:self];
}
-(int)wall_beginCollision:(cpArbiter *)arb :(cpSpace *)space :(void *)data
{
NSLog(@"Polygon / Wall collision begin: %x\n", self);
cpShape *a, *b; cpArbiterGetShapes(arb, &a, &b);
cpVect cp = cpArbiterGetPoint(arb, 0);
return 1;
}
-(void)wall_endCollision:(cpArbiter *)arb :(cpSpace *)space :(void *)data
{
NSLog("Polygon / Wall Seperate");
}
@end
(Shared Physics Manager)
Code: Select all
-(void)addCollisionHandler :(cpCollisionType)a
:(cpCollisionType)b
:(cpCollisionBeginFunc)begin
:(cpCollisionPreSolveFunc)pre
:(cpCollisionPostSolveFunc)post
:(cpCollisionSeparateFunc)sep
:(void *)data
{
cpSpaceAddCollisionHandler(physicsSpace, a, b, begin, pre, post, sep, data);
}
Yet, when the first collision happens, the data that is sent to the "int wall_begin(cpArbiter *arb, cpSpace *space, void *ptrToSelf)" method is the address of the same object, every time - no matter which object collides.
I feel as if Chipmunk might be hashing, or using some sort of "bookmark" method to keep track of the data, causing the same data reference to be passed back every time. I'm not sure, but its causing issues for me as it doesn't follow conventional callback standards.
Any help on this matter?