How to get collision point or points with Box2d?

3.5k views Asked by At

I created my own contact listener, implementing usual 4 methods: BeginContact, EndContact, PreSolve, PostSolve.

Where from what parameters of these methods can i current contact points?

I tried something like this, but nothing helped

void CListener::PreSolve(b2Contact* contact, const b2Manifold* oldManifold) {
    for(int i = 0 ; i < oldManifold->pointCount; ++i) {
        b2ManifoldPoint p = oldManifold->points[i];   
    }
}
1

There are 1 answers

0
Louis Langholtz On

In Box2D version 2.3.2, the "contact" points are obtained from the contact parameter (the first parameter) of the b2ContactListener methods. I put contact in parenthesis as these include points obtained in overlapped situations as well (where the shapes have moved beyond touching).

The base contact point information is obtained from the contacts' b2Manifold structure returned from the b2Contact::GetManifold() method. These are in coordinates local to the two shapes that are associated with that manifold however.

If you want the contact points in world coordinates (which I'd guess is really what you'd want), you can instead call the contacts' b2Contact::GetWorldManifold method to fill a b2WorldManifold instance for you. Under-the-hood, this method is essentially a convenience method that gathers the information needed from the contact including the b2Manifold and then converts the points to world coordinates.

Note that to know how many of the world coordinate contact points are actually valid (which could be 0, 1, or 2) or to know the kind of manifold that's representing the contact, you'll still want access the b2Manifold information - specifically its pointCount and type fields.

Lastly, the webpage that @iforce2d linked for you, I still find to be helpful and is more verbally and visually comprehensive than what I've stated here.