2
votes

What's the best way to get the point of collision in box2d. I'm using it with cocos2d and Objective C, but I imagine the API is similar in other languages. Using the b2ContactListener class will produce b2Contact objects, but I can't find any information on the contact position.

2
looks like I killed the partyAram Kocharyan
i think the Demo would work for you!Marine
Thanks, I've gone through this a while back. I'm using b2Body's so detecting the collisions is still easy.Aram Kocharyan

2 Answers

0
votes

You can use the following code to get the point of collision

b2Body *bodyA = contact->GetFixtureA()->GetBody();
b2Body *bodyB = contact->GetFixtureB()->GetBody();

if ((bodyA->GetFixtureList()->GetFilterData().categoryBits == Categorybits1 || bodyA->GetFixtureList()->GetFilterData().categoryBits == categoryBits2) && (bodyB->GetFixtureList()->GetFilterData().categoryBits == categoryBits2 || bodyB->GetFixtureList()->GetFilterData().categoryBits == Categorybits1)) 

You can get body positions through this code.....

even i am searching how to get point of collision

0
votes
try this method

OBJECT1_CATEGORY_BITS = 0x00000001;
OBJECT2_CATEGORY_BITS = 0x00000002;

void MyContactListener::PreSolve(b2Contact *contact, const b2Manifold
*oldManifold) 
{
    b2Fixture *fixtureA = contact->GetFixtureA();
    b2Fixture *fixtureB = contact->GetFixtureB();

    b2Filter filterA = fixtureA->GetFilterData();
    b2Filter filterB = fixtureB->GetFilterData();

    if ((filterB.categoryBits == OBJECT1_CATEGORY_BITS) && (filterA.categoryBits == OBJECT2_CATEGORY_BITS))
    {
        b2Vec2 normal = contact->GetManifold()->localNormal;

        NSLog(@"pointX : %f",normal.x);
        NSLog(@"pointY : %f",normal.y);
    } 
}