Box2d - Change contact filter on the fly

I use cocos2d (iOS) and box2d to create a game.
I came to the point where I need to change the average simulation of the contact filter and wonder how to do it.
I need to use mascubits and category bits, and that’s fine, I just don’t know how to apply them to the average b2body game.

I think that I may need to restore the original b2fixture or b2fixturedef for b2body when initializing, change the values, and then call the refresh method - world.Refilter ()?

Does this sound somewhat accurate?

Any advice is definitely appreciated by
Oliver.

+3
source share
3 answers
b2Filter filter;

for ( b2Fixture* f = body->GetFixtureList(); f; f = f->GetNext() ) {
    f->GetFilterData( &filter );
    filter.categoryBits = ...;
    filter.maskBits = ...;
    filter.groupIndex = ...;
    f->SetFilterData( &filter );
}

, - , , - . . , , , , , , .

+9

b2World. . , userData.

0

iforce2d . , box2d v2.2.1, cocos2D v2.0.0, Xcode v4.5.2 ( , b2Body "" , .. ):

b2Fixture *fix = body->GetFixtureList();
b2Filter filter = fix->GetFilterData();   
filter.groupIndex = -1*kPlayerGroupIndex; 
fix->SetFilterData(filter);

In the above code, I prevent the "body" from matching with the body of my player, which also has the same groupIndex value, that is -1 * kPlayerGroupIndex, where kPlayerGroupIndex is a positive integer constant. Any bodies with this negative index group never collide with each other. You can also update category bits or maskBits to prevent conflicts.

GetFilterData (& filter) and SetFilterData (& filter) return errors for me with the above version numbers.

0
source

All Articles