Box2D – Reset Body and Fixture

Share Post:

Share on facebook
Share on linkedin
Share on twitter
Share on pinterest
Share on email

Some of you may re-create the body and just re-attach in world object. Did you know in JAVA if you re-create an object over and over this is bad for you performance if garbage collector run anytime. It would make your game lag for a few time which is bad for user experience.

Here some simple script to reset your body definition:

body.setTransform(pos.x, pos.y, 0);
body.setType(BodyDef.BodyType.DynamicBody);

Hare to reset FIxture definition

filter = new Filter();
filter.categoryBits = HookaHookaGame.ANIMAL_BIT;
filter.maskBits = HookaHookaGame.GROUND_BIT;

// Change fixture category maskbits
fixture = getFixture();
fixture.setSensor(false);
fixture.setFilterData(filter);

Basically you just need to recreate filter object and reattach it.  We don’t have another option to reset fixture by not recreate new Filter object, but I think this is the only way.

To get fixture, it is better if you save the fixture when you create set FixtureDef in your first place, and store it to a variable.

Leave a Reply

Your email address will not be published.

CAPTCHA Image

*