A
A
Andrey Andryushchenko2015-03-31 13:15:03
Java
Andrey Andryushchenko, 2015-03-31 13:15:03

How to change body mass in box2D?

I'm writing a Java game using libGDX which has the box2d physics engine built into it. Under certain circumstances, bodies must change their mass. How to correctly change the mass in box2D? I tried like this:

float newR = (float) Math.sqrt(newMass / Math.PI / this.body.getFixtureList().get(0).getDensity());// перерасчёт радиуса тела
        MassData newMassData = new MassData();
        newMassData.mass=newMass;
        newMassData.center.set(newR, newR);
        this.body.setMassData(newMassData);

The mass takes on a new value. And everything would be fine, but the body starts to act as if I set the fixedRotation value to true. Then I found a method
this.body.resetMassData();
After calling this method, the body starts to rotate as it should, but the mass becomes equal to the previous mass value. The strangest thing is that this does not always happen. That is, sometimes this chain of methods works as it should, despite the fact that the same values ​​were passed as newMass as before.
What to do? How to change body weight correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Georgiy Padalko, 2015-04-10
@kkray

Try changing the density of the fixtures first, and then let Box2D recalculate the mass itself.
fixture->SetDensity( newDensity );
body->ResetMassData();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question