CCPhysicsCollisionDelegate Protocol Reference
| Declared in | CCPhysicsNode.h |
Overview
Delegate type called when two physics bodies collide.
The final two parameter names (typeA/typeB) should be replaced with names used with CCPhysicsBody.collisionType or CCPhysicsShape.collisionType. If both final parameter names are “default” then the collision method is called when a more specific method isn’t found.
Wildcard Collisions:
“wildcard” can be used as the final parameter name to mean “collides with anything”. When using wildcard methods, it’s possible for up to three methods to be called (ex: bullet/monster, bullet/wildcard, monster/wildcard). The most specific method (bullet/monster) will be called first, and then the wildcard handlers will be called in a somewhat random order. Begin and PreSolve methods return a boolean. If multiple methods are called, their return values are combined with a logical AND.
Collision Sequence:
On the first fixed time step (not necessarily frame) that two physics shapes collide for the first time, a ccPhysicsCollisionBegin method will be called. As long as those objects stay in contact, a ccPhysicsCollisionPreSolve and ccPhysicsCollisionPostSolve method will be called before and after the physics solver runs. The pre-solve method gives you a chance to modify the outcome of the collision, and the post-solve method lets you find out what the outcome was. This means you cannot use the outcome of the collision to modify itself. (ex: discard or undo a collision only if it will cause enough damage) Finally, when two physics objects stop touching for the very first fixed time step, the ccPhysicsCollisionSeparate method is called.
CCPhysicsShape vs. CCPhysicsBody:
Though CCPhysics concentrates mostly on physics bodies, the underlying physics engine (Chipmunk2D) operates collision callbacks on the level of individual shapes. CCPhysicsBody objects can have multiple shapes such as polyline bodies or those created with [CCPhysicsBody bodyWithShapes:]. In those cases, its possible to have have multiple active collisions between two CCPhysicsBody objects. This is something to keep in mind.
Instance Methods
ccPhysicsCollisionBegin:typeA:typeB:
Begin methods are called on the first fixed time step when two bodies begin colliding. If you return NO from a begin method, the collision between the two bodies will be ignored.
- (BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair typeA:(CCNode *)nodeA typeB:(CCNode *)nodeBParameters
- pair
Collision pair.
- nodeA
Node A.
- nodeB
Node B.
Return Value
BOOL
Declared In
CCPhysicsNode.hccPhysicsCollisionPostSolve:typeA:typeB:
Post-solve methods are called every fixed time step when two bodies are in contact after the physics solver runs. You can call use properties such as totalKineticEnergy and totalImpulse on CCPhysicsCollisionPair from a post-solve method.
- (void)ccPhysicsCollisionPostSolve:(CCPhysicsCollisionPair *)pair typeA:(CCNode *)nodeA typeB:(CCNode *)nodeBParameters
- pair
Collision pair.
- nodeA
Node A.
- nodeB
Node B.
Declared In
CCPhysicsNode.hccPhysicsCollisionPreSolve:typeA:typeB:
Pre-solve methods are called every fixed time step when two bodies are in contact before the physics solver runs. This gives you a chance to modify the collision’s outcome by setting properties such as friction, restitution, or surfaceVelocity on CCPhysicsCollisionPair Since the collisions have not been solved yet, you cannot access the outcome by using properties such as totalKineticEnergy or totalImpulse. If you return NO from a pre-solve method, the collision will be ignored for the current time step.
- (BOOL)ccPhysicsCollisionPreSolve:(CCPhysicsCollisionPair *)pair typeA:(CCNode *)nodeA typeB:(CCNode *)nodeBParameters
- pair
Collision pair.
- nodeA
Node A.
- nodeB
Node B.
Return Value
BOOL
Declared In
CCPhysicsNode.hccPhysicsCollisionSeparate:typeA:typeB:
Separate methods are called the first fixed time step after two bodies stop colliding.
- (void)ccPhysicsCollisionSeparate:(CCPhysicsCollisionPair *)pair typeA:(CCNode *)nodeA typeB:(CCNode *)nodeBParameters
- pair
Collision pair.
- nodeA
Node A.
- nodeB
Node B.
Declared In
CCPhysicsNode.h