问题
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