0
votes

I am using libGDX with box2D, and I have two bodies who has collision filtering so they cant collide each other.

I am using the groupIndex filter for those bodies, so you can imagine.

But i want to detect the body collision with the contact listener even if they cant collide to each other.

There is a way to do it?.

I did not put any code because this is a question and i thought is not necessary, at least for now...

If you can help me, thank you!.

1
Thanks for your reply, but i cant set it to sensor because i just want to not collide just between that 2 bodies. If i put to sensor the body wont collide with no bodies and i dont want that. As i said i just want to avoid the collision just between these two bodies. Nothing else.Luis Bermúdez

1 Answers

1
votes

Use Contact setEnabled method.

In ContactListener:

@Override // In preSolve method. Not beginContact, it matters
public void preSolve(Contact contact, Manifold manifold) {
       short firstBit = contact.getFixtureA().getFilterData().categoryBits;
       short secondBit = contact.getFixtureB().getFilterData().categoryBits;

       if ((firstBit | secondBit) == (BOX_BIT | GROUND_BIT)) {
            System.out.println("Contact " + firstBit + " " + secondBit);
            contact.setEnabled(false);
       }
  }

These bits BOX_BIT and GROUND_BIT are bits of your bodies that shouldn't collide but ContactListener will catch contact between them, set it like:

fixturedef.filter.categoryBits = GROUND_BIT;

Set some other bit for bodies that they should collide.

Hope helps.