2

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.

Aram Kocharyan
  • 20,165
  • 11
  • 81
  • 96

2 Answers2

0

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

Aram Kocharyan
  • 20,165
  • 11
  • 81
  • 96
Umesh Sharma
  • 388
  • 5
  • 23
0
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);
    } 
}
Liolik
  • 791
  • 6
  • 12