I'm using Chipmunk for my new project and seem to be having some inconsistencies in detecting collision impact.
The collision appears to happen, and the collision handler is invoked, but when I try to calculate the impact it often reports back as zero.
Here's the code for the collision detectors (much based on the AngryChipmunk example, but not in the Pro version).
Code: Select all
static void ApplyWoodDamage(cpSpace *space, wood *woodenBlock, cpArbiter *arb, GameLayerSimple *self) {
if(cpArbiterIsFirstContact(arb)){
cpFloat impact = cpvlength(cpArbiterTotalImpulse(arb));
NSLog(@"Original impact %f", impact);
impact = impact/FIXED_TIMESTAMP;
NSLog(@"Divided impact %f", impact);
NSLog(@"wood strength = %f", woodenBlock.wStrength);
if(impact > 1.0f){
NSLog(@"impact/500 = %f", impact/500.0f);
woodenBlock.wStrength -= impact/500.0f;
NSLog(@"wood strength after = %f", woodenBlock.wStrength);
if(woodenBlock.wStrength < 0.0f){
cpSpaceAddPostStepCallback(space, (cpPostStepFunc)postStepRemoveWood, woodenBlock, self);
NSLog(@"Need postStepCallback to remove wood");
}
}
}
}
static void postsolve_wood(cpArbiter *arb, cpSpace *space, void *unused) {
cpShape *a, *b; cpArbiterGetShapes(arb, &a, &b);
ApplyWoodDamage(space, b->data, arb, unused);
}
Am I missing something when it comes to the impulse/impact? Do I need to reset something after the first collision is detected?
Here's some output from the log.
Code: Select all
2011-11-16 21:07:39.674 bloksnroks[12342:1be03] Original impact 201.118423
2011-11-16 21:07:39.675 bloksnroks[12342:1be03] Divided impact 12067.104492
2011-11-16 21:07:39.676 bloksnroks[12342:1be03] wood strength = 4.000000
2011-11-16 21:07:39.677 bloksnroks[12342:1be03] impact/500 = 24.134209
2011-11-16 21:07:39.677 bloksnroks[12342:1be03] wood strength after = -20.134209
2011-11-16 21:07:39.680 bloksnroks[12342:1be03] Need postStepCallback to remove wood
2011-11-16 21:08:09.174 bloksnroks[12342:1be03] Original impact 0.000000
2011-11-16 21:08:09.175 bloksnroks[12342:1be03] Divided impact 0.000000
2011-11-16 21:08:09.175 bloksnroks[12342:1be03] wood strength = 24.000000
2011-11-16 21:08:09.623 bloksnroks[12342:1be03] Original impact 0.000000
2011-11-16 21:08:09.624 bloksnroks[12342:1be03] Divided impact 0.000000
2011-11-16 21:08:09.625 bloksnroks[12342:1be03] wood strength = 24.000000
Any help please?
Thanks,
Tony B
EDIT: Further log of inconsistent impact.
2011-11-16 21:21:03.676 bloksnroks[12553:1be03] Original impact 0.000000
2011-11-16 21:21:03.677 bloksnroks[12553:1be03] Divided impact 0.000000
2011-11-16 21:21:03.678 bloksnroks[12553:1be03] wood strength = 48.000000
2011-11-16 21:21:04.361 bloksnroks[12553:1be03] Original impact 0.000000
2011-11-16 21:21:04.362 bloksnroks[12553:1be03] Divided impact 0.000000
2011-11-16 21:21:04.362 bloksnroks[12553:1be03] wood strength = 48.000000
2011-11-16 21:21:04.643 bloksnroks[12553:1be03] Original impact 0.000000
2011-11-16 21:21:04.644 bloksnroks[12553:1be03] Divided impact 0.000000
2011-11-16 21:21:04.645 bloksnroks[12553:1be03] wood strength = 48.000000
2011-11-16 21:21:04.710 bloksnroks[12553:1be03] Original impact 106.263054
2011-11-16 21:21:04.710 bloksnroks[12553:1be03] Divided impact 6375.782715
2011-11-16 21:21:04.712 bloksnroks[12553:1be03] wood strength = 48.000000
2011-11-16 21:21:04.713 bloksnroks[12553:1be03] impact/500 = 12.751565
2011-11-16 21:21:04.714 bloksnroks[12553:1be03] wood strength after = 35.248436