Search code examples
androidbox2dcollision-detection

Box2d contact listener doesnt work right at presolve


i am creating a game using box2d. I have balls and when a ball passes through a sensor i need to increase score... Now when i resolve the contact in beginContact everything is ok

public ContactListener createContactListener() {
        return new ContactListener() {
            @Override
            public void beginContact(Contact contact) {

                if(contact.getFixtureA().getUserData() == "ball" && contact.getFixtureB().getUserData() == "cup_sensor"){
                    //contact.getFixtureA().setUserData("ball_inactive");
                    Constants.LC.score++;

                    //contact.getFixtureA().getFilterData().maskBits = 0xffff & 0x0002;
                    //contact.setEnabled(false);
                }

                if(contact.getFixtureB().getUserData() == "ball" && contact.getFixtureA().getUserData() == "cup_sensor"){
                    //contact.getFixtureB().setUserData("ball_inactive");
                    Constants.LC.score++;
                    //contact.getFixtureB().getFilterData().maskBits = 0xffff & 0x0002;
                    //contact.setEnabled(false);
                }
            }

            @Override
            public void endContact(Contact contact) {

            }

            @Override
            public void preSolve(Contact contact, Manifold oldManifold) {

            }

            @Override
            public void postSolve(Contact contact, ContactImpulse impulse) {
            }
        };
    }

every thing works like a charm

But for some reason i want to do it in presolve

return new ContactListener() {
    @Override
    public void beginContact(Contact contact) {


    }

    @Override
    public void endContact(Contact contact) {

    }

    @Override
    public void preSolve(Contact contact, Manifold oldManifold) {
        if(contact.getFixtureA().getUserData() == "ball" && contact.getFixtureB().getUserData() == "cup_sensor"){
            //contact.getFixtureA().setUserData("ball_inactive");
            Constants.LC.score++;

            //contact.getFixtureA().getFilterData().maskBits = 0xffff & 0x0002;
            //contact.setEnabled(false);
        }

        if(contact.getFixtureB().getUserData() == "ball" && contact.getFixtureA().getUserData() == "cup_sensor"){
            //contact.getFixtureB().setUserData("ball_inactive");
            Constants.LC.score++;
            //contact.getFixtureB().getFilterData().maskBits = 0xffff & 0x0002;
            //contact.setEnabled(false);
        }
    }

    @Override
    public void postSolve(Contact contact, ContactImpulse impulse) {
    }
};

}

now it does not work... it does not passes the if condition... so score does not increase...

Please support me with that problem

Edit: i did some debugging. seems like on presolve contact.getFixtureA().getUserData() for sensor is returning null. I have no idea why


Solution

  • The PreSolve callback lets you do something right before the collision response impulses are calculated. Sensors have no collision response, so PreSolve will not be called for them.