Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Box2d - Changing contact filter on the fly

Im using cocos2d (iOS) and box2d to create a game.
I have come to the point where I need to change the contact filter mid simulation and wondering how to go about this.
I need to use maskbits and categorybits, which is fine, im just unsure how to apply them to a b2body mid game.

I'm thinking I may need to retrieve the original b2fixture or b2fixturedef of the b2body on initialization, alter the values accordingly then call a method to refresh - world.Refilter()?

Does this sound somewhat accurate?

Any advice is surely appreciated
Oliver.

like image 376
Ospho Avatar asked Jan 31 '26 12:01

Ospho


1 Answers

b2Filter filter;

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

Obviously this would change the filter settings for all fixtures on a body - if you want to be more selective you will have to be able to tell which fixture is which somehow. Eg. if you know it's the only circle fixture you could just look at the type of the fixture to decide, otherwise you would have to make use of the fixture's user data I guess.

like image 74
iforce2d Avatar answered Feb 03 '26 07:02

iforce2d