How to detect collision but do not collide in box2d?

前端 未结 2 1950
攒了一身酷
攒了一身酷 2020-12-09 11:15

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 fixture

2条回答
  •  借酒劲吻你
    2020-12-09 12:01

    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

提交回复
热议问题