How to detect collision but do not collide in box2

2019-01-18 11:56发布

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楼-- · 2019-01-18 12:24

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

查看更多
闹够了就滚
3楼-- · 2019-01-18 12:26

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.

查看更多
登录 后发表回答