2
votes

I'm writing simple game for iOS using Cocos2d with Box2d. I've already collision detection working, but not in the way I want to. I've extended b2ContactListener, and detecting objects collisions this way:

void ContactListener::BeginContact(b2Contact* contact)
{
    b2Body *aBody = contact->GetFixtureA()->GetBody();
    b2Body *bBody = contact->GetFixtureB()->GetBody();

    // collision between aBody and bBody occurred

}

I can't find out how to get the point of collision, and if possible, the collision impact force. There are several tutorials how to get collision points, but none of them is working for me. I've found some examples that used b2CollisionPoint, but It looks like this is not working in the current version of Box2d. Other examples implements "Result" method of b2CollisionPoint, but it doesn't work neither - looks like it's applies to the previous version of Box2d or versions for other platform (Java or Flash).

To be more specific about my problem, here is an example of what I want to achieve: I need to detect kind of collision between game objects. I'm already able to detect if player fixture is colliding with ground fixture, but I need to know if the player is just "standing" on the ground or colliding with it from the bottom (imagine platform game and a player that hits a platform after jump) or other side (player jumps into the vertical wall). I'll be much easier for me to do this if I would be able to get the point of collision. This would be useful in many other cases too.

1

1 Answers

3
votes

this worked for me: http://www.cocos2d-iphone.org/forum/topic/6024

void ContactListener::PostSolve(b2Contact* contact, 
                                  const b2ContactImpulse* impulse) {

    // find the strength of the impulse..
    int32 count = contact->GetManifold()->pointCount;
    float32 maxImpulse = 0.0f;
    for (int32 i = 0; i < count; ++i) {
        maxImpulse = b2Max(maxImpulse, impulse->normalImpulses[i]);
    }

    NSLog(@"maxImpulse: %f", maxImpulse);
}