14
votes

How to detect if body collides other body but do not react on this collision.

By default i - detect collision and bodies collide.

If i set fixtureDef filter - bodies do not collide but i can not detect collision.

Help please!

2

2 Answers

22
votes

If the fixture never needs to collide with anything you could make it a sensor. If you need it to collide with some things but not others you could do contact->SetEnabled(false) in the PreSolve of the collision listener, depending on what it collided with.

9
votes

What you want here is a sensor fixture on a body. From the box2d manual:

Sometimes game logic needs to know when two fixtures overlap yet there should be no collision response. This is done by using sensors. A sensor is a fixture that detects collision but does not produce a response.

You can flag any fixture as being a sensor. Sensors may be static or dynamic. Remember that you may have multiple fixtures per body and you can have any mix of sensors and solid fixtures.

Sensors do not generate contact points. There are two ways to get the state of a sensor:

  1. b2Contact::IsTouching
  2. b2ContactListener::BeginContact and EndContact

You can set a fixture as a sensor and then write it into your contact listener. If a fixture has its sensor flag set to true, it will provide collision data without physically simulating the collision (ie will allow you to test for overlap between it any any other colliding fixture.)

This is a helpful tutorial on how to get started using sensors Ray Wenderlich sensor tutorial