how to change the group index of a body dynamically in box2d

走远了吗. 提交于 2019-12-11 01:35:13

问题


I am new to box2d. I have started a new game in box2d and created 5 dynamic bodies in the world. Initially I need to prevent collision between them. so i set group index of all bodies negative. After touching a body i want to allow the collision with that body. How to reset the group index. Please help..


回答1:


Sounds like you're looking for something like

b2Body *body = world->GetBodyList();
b2Filter filter = body->GetFilterData();
filter.maskBits = filter.maskBits | THE_RELEVANT_CATEGORY_BITS;
body->SetFilterData(&filter);

The idea here is that you can get the collision filtering data for any object, use some boolean logic to assign collision data to the maskBits field, and then use SetFilterData to apply that new collision data to the body. THE_RELEVANT_CATEGORY_BITS should be an element of an enumerated type in which each collision filtering category is represented by a unique binary integer.

Check out the box2D tutorial for more on this http://www.iforce2d.net/b2dtut/collision-filtering




回答2:


if you have more than 1 fixtures of a body then you can use this chunk of code

for (b2Fixture* fix = body->GetFixtureList(); fix; fix = fix->GetNext())
{
    b2Filter filter=  fix->GetFilterData();

    filter.groupIndex = 0;
    filter.categoryBits = 0x0004;
    filter.maskBits = 0x0002;
    fix->SetFilterData(filter);
}


来源:https://stackoverflow.com/questions/11257502/how-to-change-the-group-index-of-a-body-dynamically-in-box2d

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