How to detect collision but do not collide in box2d?

血红的双手。 提交于 2019-11-28 09:13:09

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!