0
votes

I am developing game using LibGDX and Box2D . I want to get the force or the impulse when two bodies are collided , But I cant find the right way to do that , how can I use contact listener for getting that impulse or force ?I cant get the impulse using ContactImpulse parameter in postSolve method . Who can help me ?

     new ContactListener() {

    @Override
    public void preSolve(Contact contact, Manifold oldManifold) {
        // TODO Auto-generated method stub

    }

    @Override
    public void postSolve(Contact contact, ContactImpulse impulse) {
        // TODO Auto-generated method stub

    }

    @Override
    public void endContact(Contact contact) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beginContact(Contact contact) {
        // TODO Auto-generated method stub

    }
};
1

1 Answers

2
votes

In the preSolve(Contact contact, Manifold manifold) method, you can query the velocities of the two bodies that have collided. Using contact.getFixtureA() and contact.getFixtureB(), you can get the bodies of each fixture, and the use body.getLinearVelocity().

The preSolve method is called before box2D actually resolves of the collisions, so at the instant of collision, the impact velocity will still be accessible.

As the manual states: "The pre-solve event is also a good place to determine the point state and the approach velocity of collisions"

You could use the information provided in the preSolve such as how heavy the bodies are and how fast they are moving to decide how it would affect your object.