Search code examples
javalibgdxbox2dcontacts

Box2d sensors don't detect contacts


My problem:

Weapon body stoped detecting contacts when i make it's fixture sensor.

ContactListener code:

void preSolve(contact: Contact, oldManifold:Manifold){

        switch (fixA.getFilterData().categoryBits | fixB.getFilterData().categoryBits) {
            case Game.WEAPON_BIT | Game.ENEMY_BIT: bodyDeleteList.add(contact.getFixtureA().getBody());
            System.out.println("Contact!");  // this never prints
            break;
        }
}

When i make body fixture regular(commenting fixtureDef.setFixtute(true)) everything becomes great and contacts occur and detecting.

Documentation says:

/** A sensor shape collects contact information but never generates a collision response. */

But it doesn't. So maybe i forgot something.


Solution

  • Solved it by adding this code to beginContact(..) method.

    It's strange, but sensors only collect contact information in beginContact(..) method.

    void beginContact(contact: Contact){
    
         switch (fixA.getFilterData().categoryBits | fixB.getFilterData().categoryBits) {
                case Game.WEAPON_BIT | Game.ENEMY_BIT: bodyDeleteList.add(contact.getFixtureA().getBody());
                System.out.println("Contact!");  // this prints
                break;
         }
    }