Yes, create a post-solve callback between the blocks and check the impact force. I have an Objective-C example on the downloads page called Angry Chipmunks that is vaguely similar:
http://chipmunk-physics.net/downloads.phpBasically it uses
cpArbiterIsFirstContact() to check if it's the first frame two objects have hit each other. If it is, then it calculates the impact force and uses that to apply damage.
- Code: Select all
static inline void BreakableApplyDamage(BreakableBox *box, cpArbiter *arb, GameWorld *self){
if(cpArbiterIsFirstContact(arb)){
// Divide the impulse by the timestep to get the collision force.
cpFloat impact = cpvlength(cpArbiterTotalImpulse(arb))/FIXED_TIMESTEP;
if(impact > 1.0f){
box.strength -= impact/500.0f;
if(box.strength < 0.0f){
// Create the broken chunks and add them to the GameWorld.
BrokenBox *broken = [[BrokenBox alloc] initFromBox:box];
[self add:broken];
[broken release];
// If you broke the goal box you lose!
if([box isKindOfClass:[GoalBox class]]) [self scheduleRestart];
[self remove:box];
}
}
}
}
// This is an Objective Chipmunk callback.
- (void)PostSolve_Default_BreakableBox:(cpArbiter *)arbiter space:(ChipmunkSpace*)space
{
CHIPMUNK_ARBITER_GET_SHAPES(arbiter, unused, box);
BreakableApplyDamage(box.data, arbiter, self);
}
Don't worry about calling collision functions to much. Realize that Chipmunk runs a
lot of math for each collision pair it finds. Yeah, you might run a few hundred or maybe a few thousand of them per frame, just don't do anything really expensive in them like loading data from disk or calculate pi to a million digits.