views:

137

answers:

1

If I have two objects in chipmunk (I'm using cocos2d-iphone), once I've detected that they collided, how can I tell how hard they are hitting each other?

I want their force (vs. the velocity) to know the damage of the collision.

I've seen discussion about how to do this, but never concrete working code (and I couldn't get it work, even though I thought I knew what I was doing.) For completeness, here is the code that I am using to determine the Velocity of the collision, so, how, precisely, would I go about detecting the Force of a collision?

- (BOOL) handleCollisionBetweenBalls:(CollisionMoment)moment arbiter:(cpArbiter*)arb space:(cpSpace*)space {
  JjrFootprint(@"handleCollisionBetweenBalls");
    if (moment == COLLISION_BEGIN) {
    JjrLog(@"Balls Collide");
    CP_ARBITER_GET_SHAPES(arb, a, b);
    cpVect va;
    cpVect vb;
    va = a->body->v;
    vb = b->body->v;
    cpVect vNet = cpvadd(va,vb);
    cpFloat Length = cpvlength(vNet);
    // 7 = barely touching
    // 1000 = hard
    if (Length > 100) {
      [[SimpleAudioEngine sharedEngine] playEffect:@"board.wav"];
    }
  }
    return YES;
}

Thanks, JJ

+1  A: 

http://code.google.com/p/chipmunk-physics/wiki/CallbackSystem#Collision_Handlers

You can only get the collision impulse from inside the post solve callback.

slembcke