cpSpaceAddCollisionHandler objC -> C

Official forum for the Chipmunk2D Physics Library.
Post Reply
pats
Posts: 5
Joined: Mon Feb 21, 2011 8:50 pm
Contact:

cpSpaceAddCollisionHandler objC -> C

Post by pats »

I'm a relative noob and have been using chipmunk collision handlers on the iPhone in objC with no problems. I'm trying the same with C.

in my HelloWorldScene.cpp I add collision types

enum _CollisionTypes {
BALL_TYPE,
BLOCK_TYPE,
CATCH_TYPE,
} CollisionTypes;

Then I set the handlers as

cpSpaceAddCollisionHandler(space, BALL_TYPE, BLOCK_TYPE, blockerBegin, NULL, NULL, NULL, NULL);

And my blockerBegin function as

static int
blockerBegin(cpArbiter *arb, cpSpace *space, void *unused) {
//CP_ARBITER_GET_SHAPES(arb, a, b);
//Emitter *emitter = (Emitter *) a->data;
//emitter->blocked++;
return 0; // Return values from sensors callbacks are ignored,
}

And I get a 'blockerBegin' was not declared in this scope error.
I've tried adding "static int blockerBegin(cpArbiter *arb, cpSpace *space, void *unused);" to my HelloWorldScene.h and I still get errors.

My CCLayer is...
class HelloWorld : public cocos2d::CCLayer

and my CCScene is..
static cocos2d::CCScene* scene();

I'm just not too familar with this C format.

Two days fiddling with this and any help would be appreciated.

Thanks - PatS
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: cpSpaceAddCollisionHandler objC -> C

Post by slembcke »

You have to declare your function before you can use it. Either move the definition above the line where you want to use it or add a function prototype to the top of the file. You would have ran into the same issue in Obj-C actually.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
pats
Posts: 5
Joined: Mon Feb 21, 2011 8:50 pm
Contact:

Re: cpSpaceAddCollisionHandler objC -> C

Post by pats »

@slembcke, thanks for the reply

In objC I would normally add this to the top of my .m code and it would work.

- (BOOL) blockerBegin:(CollisionMoment)moment arbiter:(cpArbiter*)arb space:(cpSpace*)space;

In C I tried adding this to my .h code.

static int blockerBegin(cpArbiter *arb, cpSpace *space, void *unused);

and I get
"HelloWorld::blockerBegin(cpArbiter*, cpSpace*, void*)", referenced from:
__ZN10HelloWorld12blockerBeginEP9cpArbiterP7cpSpacePv$non_lazy_ptr in HelloWorldScene.o
(maybe you meant: __ZN10HelloWorld12blockerBeginEP9cpArbiterP7cpSpacePv$non_lazy_ptr)
ld: symbol(s) not found
collect2: ld returned 1 exit status

Thanks again

PatS
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: cpSpaceAddCollisionHandler objC -> C

Post by slembcke »

Oh, you can't use a C++ method as a function. They look almost identical, but they are not called the same way. Put the function outside of you HelloWorld class.

Also, were you using SpaceManager in Obj-C? Objective-C methods are called dynamically at runtime, so when you use @selector(myMethod:woo:) to identify a method the compiler doesn't actually check that the method exists. You would have had the same problem in Obj-C if you were using C function callbacks instead of methods.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
pats
Posts: 5
Joined: Mon Feb 21, 2011 8:50 pm
Contact:

Re: cpSpaceAddCollisionHandler objC -> C

Post by pats »

@slembcke, WOW you're amazing.

Outside the HelloWorld class did the trick.

Many thanks

PatS
pats
Posts: 5
Joined: Mon Feb 21, 2011 8:50 pm
Contact:

Re: cpSpaceAddCollisionHandler objC -> C

Post by pats »

That seemed to work as I was able to remove shapes from the space in postStepRemove. Now I need to add another ball. I call addNewBall and for some reason I cannot get the body or shape to add to the space. I don't see an error it just crashes on either cpSpaceAddBody or cpSpaceAddShape.

static void
postStepRemove(cpSpace *space, cpShape *shape, void *unused)
{
cpShape *thisShape = shape;
CCSprite *thisSprite = (CCSprite *)thisShape->data;
CCNode *parent = (CCSprite *)thisSprite->getParent();
parent->removeChild(thisSprite, false);
cpSpaceRemoveShape(space, thisShape);
cpShapeFree(thisShape);

HelloWorld::addNewBall();

}

void HelloWorld::addNewBall()
{
cpBody *ballBody = cpBodyNew(250.0f, INFINITY); //INFINITY
cpSpaceAddBody(space, ballBody);
cpVect ballVerts[] = { cpv(-8.0, -8.0), cpv(-8.0, 8.0), cpv(8.0, 8.0), cpv(8.0, -8.0) };
cpShape *ballShape = cpPolyShapeNew(ballBody, 4, ballVerts, cpv(0, 0));
CCSprite *ballSprite = CCSprite::spriteWithFile("ballSprite.png", CGRectMake(0,10, 16, 16));
ballShape->data = ballSprite;
addChild(ballSprite);
cpSpaceAddShape(space, ballShape);
}

Thanks in advance

PatS
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: cpSpaceAddCollisionHandler objC -> C

Post by slembcke »

What does the debugger say the issue is?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
pats
Posts: 5
Joined: Mon Feb 21, 2011 8:50 pm
Contact:

Re: cpSpaceAddCollisionHandler objC -> C

Post by pats »

Here's my backtrace
Program received signal: “EXC_BAD_ACCESS”.
(gdb) bt
#0 0x00021b2b in cpHashSetFind (set=0x1, hash=175, ptr=0x668c310) at cpHashSet.c:209
#1 0x0002742e in cpSpaceAddShape (space=0x78dc3e0, shape=0x668c310) at cpSpace.c:321
#2 0x0000bd94 in HelloWorld::addNewBall2 (this=0xbfffdb94) at /Users/pats/Projects/SS/KillMe/Classes/HelloWorldScene.cpp:715
#3 0x0000d81e in HelloWorld::addNewBall () at /Users/pats/Projects/SS/KillMe/Classes/HelloWorldScene.cpp:731
#4 0x0000d8d6 in postStepRemove (space=0x714bfb0, shape=0x714ccf0, unused=0x0) at /Users/pats/Projects/SS/KillMe/Classes/HelloWorldScene.cpp:856
#5 0x00028f34 in postStepCallbackSetFilter (callback=0x668c500, space=0x714bfb0) at cpSpace.c:791
#6 0x00021c93 in cpHashSetFilter (set=0x714c2e0, func=0x28f00 <postStepCallbackSetFilter>, data=0x714bfb0) at cpHashSet.c:241
#7 0x0002942b in cpSpaceStep (space=0x714bfb0, dt=0.0333374999) at cpSpace.c:890
#8 0x00009714 in HelloWorld::step (this=0x71254e0, delta=0.0666749999) at /Users/pats/Projects/SS/KillMe/Classes/HelloWorldScene.cpp:1205
#9 0x00056fc4 in cocos2d::CCTimer::update (this=0x716d010, dt=0.0666749999) at CCScheduler.cpp:119
#10 0x000595f3 in cocos2d::CCScheduler::tick (this=0x716b760, dt=0.0666749999) at CCScheduler.cpp:586
#11 0x00093b59 in cocos2d::CCDirector::drawScene (this=0x177680) at CCDirector_mobile.cpp:187
#12 0x00092d99 in cocos2d::CCDisplayLinkDirector::mainLoop (this=0x177680) at CCDirector_mobile.cpp:947
#13 0x000b6500 in -[CCDirectorCaller doCaller:] (self=0x716d140, _cmd=0x11e11a, sender=0x7161100) at CCDirectorCaller.mm:93
#14 0x00920ebe in CA::Display::DisplayLink::dispatch ()
#15 0x00921003 in CA::Display::EmulatorDisplayLink::callback ()
#16 0x02bcbd43 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ ()
#17 0x02bcd384 in __CFRunLoopDoTimer ()
#18 0x02b29d09 in __CFRunLoopRun ()
#19 0x02b29280 in CFRunLoopRunSpecific ()
#20 0x02b291a1 in CFRunLoopRunInMode ()
#21 0x03f652c8 in GSEventRunModal ()
#22 0x03f6538d in GSEventRun ()
#23 0x009c6b58 in UIApplicationMain ()
#24 0x00008694 in main (argc=1, argv=0xbfffefb4) at /Users/pats/Projects/SS/KillMe/main.m:13
(gdb)
Thanks

PatS
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: cpSpaceAddCollisionHandler objC -> C

Post by slembcke »

The hash set passed to that function is 0x1, definitely not a real pointer. Are you sure that both of those methods are using the same space, and that the space pointer in HelloWorld::addNewBall() is valid? The callback has a parameter that would be shadowing a global or other type of variable.
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 6 guests