CollisionHandlers

Official forum for the Chipmunk2D Physics Library.
marciokoko
Posts: 86
Joined: Thu Jan 12, 2012 11:50 am
Contact:

CollisionHandlers

Post by marciokoko »

I have a layer searching for many collision types. I already have one collision handler set:

in createSpace:

Code: Select all

    cpSpaceAddCollisionHandler(space, kCollisionTypeA, kCollisionTypeB, (cpCollisionBeginFunc)begin, NULL, NULL, NULL, NULL);
Then above it all, in the layer class:

Code: Select all

static void
postStepRemove(cpSpace *space, cpShape *shape, void *unused)
{

    cpSpaceRemoveBody(space, shape->body);
    cpBodyFree(shape->body);
    
    cpSpaceRemoveShape(space, shape);
    cpShapeFree(shape);

}
static cpBool begin(cpArbiter *arb, cpSpace *space, void *ignore){
    CP_ARBITER_GET_SHAPES(arb, aShape, bShape);
    cpSpaceAddPostStepCallback(space, (cpPostStepFunc)postStepRemove, bShape, NULL);
    return cpTrue;
}
but I want to add another one:

Code: Select all

// TO BE IGNORED
    cpSpaceAddCollisionHandler(space, kCollisionTypeC, kCollisionTypeD, begin, NULL, NULL, NULL, NULL);
// TO BE postStepRemove like A-B
    cpSpaceAddCollisionHandler(space, kCollisionTypeE, kCollisionTypeA, begin, NULL, NULL, NULL, NULL);
My question is, how to I add a second or n-th collision handler?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: CollisionHandlers

Post by slembcke »

I'm not sure I understand the question, but you can register as many collision handlers as you want. Call cpSpaceAddCollisonHandler() with the callbacks you want for each one.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
marciokoko
Posts: 86
Joined: Thu Jan 12, 2012 11:50 am
Contact:

Re: CollisionHandlers

Post by marciokoko »

Is there any situation in which you might imagine all groundShapes loading their ability to hold up GameObjects? All of a sudden after adding the collision handler for the shield and the one for the new meteor, after the player touches the shield, everything falls down and thru the bottom ground even!

For that to happen I would have to somehow cancel all groundShapes collision type, right?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: CollisionHandlers

Post by slembcke »

Yes, it's not an uncommon issue. When you allow two infinite mass objects to collide for instance, it will cause divide by zero issues that will basically corrupt the physics state of both bodies. For the ground, this will cause the collision response to fail against it.

If compile and run Chipmunk in debug mode, there should be assertions that catch the error that cause it though.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
marciokoko
Posts: 86
Joined: Thu Jan 12, 2012 11:50 am
Contact:

Re: CollisionHandlers

Post by marciokoko »

Im creating them using mass of 1.0f.


[self addBoxBodyAndShapeWithLocation:location
size:size space:theSpace mass:1.0 e:0.0 u:0.5
collisionType:kCollisionTypeShield canRotate:TRUE];

oh...i just noticed one of the bodies isn't created with this method at all!? Does that mean its created by default with infinite mass?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: CollisionHandlers

Post by slembcke »

There is no default mass in Chipmunk. Are you using SpaceManager? I'm not very familiar with that. How are you creating the "other" body? Is it using SpaceManager code? What does that do?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
marciokoko
Posts: 86
Joined: Thu Jan 12, 2012 11:50 am
Contact:

Re: CollisionHandlers

Post by marciokoko »

No, Im basing off of RW code for the Space Viking app, but anyway, here is the code:

In the actionLayer I call:

Code: Select all

hopper = [[[CPHopper alloc] initWithLocation:ccp(100,100) space:space groundBody:groundBody] autorelease];
The hopper & shield are a subclass of CPSprite and have this in their init method to create a body associated with a sprite:

Code: Select all

        [self addBoxBodyAndShapeWithLocation:location size:size space:theSpace mass:1.0 e:0.0 u:0.5 collisionType:kCollisionTypeViking canRotate:TRUE];
Where CPSprite has the respective method to create cpBody:

Code: Select all

- (void)addBoxBodyAndShapeWithLocation:(CGPoint)location
                                  size:(CGSize)size
                                 space:(cpSpace *)theSpace
                                  mass:(cpFloat)mass
                                     e:(cpFloat)e
                                     u:(cpFloat)u
                         collisionType:(cpCollisionType)collisionType
                             canRotate:(BOOL)canRotate {
    space = theSpace;
    
    float moment = INFINITY;
    if (canRotate) {
        moment = cpMomentForBox(mass, size.width, size.height);
    }
    
    body = cpBodyNew(mass, moment);
    body->p = location;
    cpSpaceAddBody(space, body);
    
    shape = cpBoxShapeNew(body, size.width, size.height);
    shape->e = e;
    shape->u = u;
    shape->collision_type = collisionType;
    shape->data = self;
    cpSpaceAddShape(space, shape);
}
So is mass = 1.0 mean infinity? Meaning no mass is 0 and infinity is 1?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: CollisionHandlers

Post by slembcke »

No, 1.0 mass means 1.0 mass (of whatever units you decided you are using). You need to use the INFINITY constant to set it to infinity.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
marciokoko
Posts: 86
Joined: Thu Jan 12, 2012 11:50 am
Contact:

Re: CollisionHandlers

Post by marciokoko »

Ok could you tell me how to put chipmunk in debug mode so i can figure out what's going on?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: CollisionHandlers

Post by slembcke »

Using the debug build configuration from Xcode should be sufficient.

It will print out something like the following when in debug mode:
Initializing cpSpace - Chipmunk v6.0.3 (Debug Enabled)
Compile with -DNDEBUG defined to disable debug mode and runtime assertion checks
So as long as you don't define NDEBUG (the same symbol used to disable other system provided assert() statements), Chipmunk will be running in debug mode.
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: Heise IT-Markt [Crawler] and 39 guests