Game Development Reference
In-Depth Information
cpArbiterGetBodies(arbiter, &bodyA, &bodyB);
PhysicsSprite* spriteA = (__bridge PhysicsSprite*)bodyA-> data;
PhysicsSprite* spriteB = (__bridge PhysicsSprite*)bodyB-> data;
if (spriteA ! = nil && spriteB ! = nil)
spriteA.color = ccWHITE;
spriteB.color = ccWHITE;
The contactBegin method should return YES if the collision should be processed
normally. By returning NO or 0 from this method, you can also ignore collisions. To get
to the sprites, you first have to get the shapes from the cpArbiter , which just like
b2Contact holds the contact information. Via the cpArbiterGetShapes method
and passing two shapes as out parameters, you get the colliding shapes from which
you can then retrieve the individual PhysicsSprite pointers. If they're both valid,
you can change their color.
As with Box2D, these callbacks don't get called by themselves. In the Hel-
loWorldLayer initPhysics method, right after the space is created, you must
add the collision handlers using the cpSpaceAddCollisionHandler method:
-(void) initPhysics
space = cpSpaceNew();
space-> gravity = CGPointMake(0, -100);
unsigned int defaultCollisionType = 0;
cpSpaceAddCollisionHandler(space, defaultCollisionType, defaultCollisionType,
&contactBegin, NULL, NULL, &contactEnd, NULL);
The default collision type for shapes is 0, and because I don't care about filtering colli-
sions, both collision type parameters are set to 0 . You can assign each body's shape an
integer value to its collision_type property and then add collision handlers that
are called only if bodies of matching collision types collide. This is called filtering col-
lisions and is described in the Chipmunk manual, at ht-
Search Nedrilad ::

Custom Search