richtilt
richtilt

Reputation: 1

attach multiple collision shapes to a single rigid body

I have created a single player game using iOS + Cocos2d + Chipmunk and I'm looking for a solution that demonstrates how to attach multiple collision shapes to a single rigid body. I have a target that has an irregular shape (a car) that I need to detect collisions for. The target (car) is seen by the player from a side view and other objects may impact the target from multiple directions, not just from the front or the rear. The shape is such that I am unable to use a single cpPolyShape and achieve a realistic collision effect. Two cpPolyShapes (rectangular) stacked on top of each other, with the bottom rectangle being larger should do the trick.

Can someone provide a example of how this can be achieved?

I read the Chipmunk docs about cpShape, http://code.google.com/p/chipmunk-physics/wiki/cpShape, and it states that 'You can attach multiple collision shapes to a rigid body' in the very bottom of the page in the notes section, but no example is provided.

I currently have a working, functional project and am trying to make some final adjustments to improve game play.

Upvotes: 0

Views: 642

Answers (2)

Marine
Marine

Reputation: 1097

You can add the method

In the .h file add the prototype

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

Now in the .m file add the code as

    cpSpaceAddCollisionHandler(<space name>, <cpCollisionType of  body a >, <cpCollisionType of body b>, <cpCollisionBeginFunc name>, <cpCollisionPreSolveFunc preSolve>, <cpCollisionPostSolveFunc postSolve>, <cpCollisionSeparateFunc separate>, <void *data>);

static int FunctionName(cpArbiter *arb, cpSpace *space, void *unused)
{
    cpShape *a, *b; cpArbiterGetShapes(arb, &a, &b);    
    printf("\n Collision Detected");    
    return 1;   
}

Note:- Don't Forget to give the collision type of both Body.

Upvotes: 0

slembcke
slembcke

Reputation: 1419

When you call cp*ShapeNew(), the first parameter is the body to attach it to. Simple make more than one shape that share the same body. There is no trick.

Upvotes: 1

Related Questions