How to detect collision but do not collide in box2d?

前端 未结 2 1945
攒了一身酷
攒了一身酷 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

    0 讨论(0)
  • 2020-12-09 12:07

    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.

    0 讨论(0)
提交回复
热议问题