Change density after the creation of a body

Big Stink wrote:
Hello,

Is it possible to change attributes like friction, restitution or density on the fly?


EDIT:: Just found this, seems to work fine 🙂
Code:
// Change property of body, in this case friction (there are other methods for other properties)
this.b2body.GetFixtureList().SetFriction(friction);


Hi,

the above doesn't work for me. I would like to change density after the creation of the bodies like this:
Code:
b2Def.fixture.friction = 0.5;
         b2Def.fixture.restitution = 1.0;
         b2Def.fixture.density = 0.2;
         b2Def.fixture.shape = new b2CircleShape();
         b2CircleShape(b2Def.fixture.shape).m_radius = 10 /Constants.RATIO;
         b2Def.body.type = b2Body.b2_dynamicBody;
         body11 = Constants._myWorld.CreateBody(b2Def.body);
         body11.CreateFixture(b2Def.fixture);
         
         b2Def.body.type = b2Body.b2_dynamicBody;
         body22 = Constants._myWorld.CreateBody(b2Def.body);
         body22.CreateFixture(b2Def.fixture);
         
         body11.SetTransform(new V2(100 / Constants.RATIO, 0 / Constants.RATIO), 0);
         body22.SetTransform(new V2(300 / Constants.RATIO, 0 / Constants.RATIO), 0);
         
         body22.GetFixtureList().SetDensity(0.9); //<-- DOESN'T WORK
         //body22.m_fixtureList.SetDensity(0.9);   //<-- DOESN'T WORK EITHER
         
         addEventListener(Event.ENTER_FRAME,goWorld);
         
         body11.ApplyForce(new V2(0,5), body11.GetWorldCenter());
         body22.ApplyForce(new V2(0,5), body22.GetWorldCenter());


The density keeps being the one defined on the creation of the bodies.
Anyone knows why or how to do it work??

Thanks you in advance and happy holidays.
ok, I found out that I can destroy the fixture of a body and re-create it with a new density. However, isn't there a way to alter the density value of a current fixture of a body without destroying and re-creating it ?
OK, I found that I have to recalculate the mass data after a change of density.

So after, for example, do this: body22.m_fixtureList.SetDensity(0.9);
I have also to do this: body22.ResetMassData();
in order to apply the change.

Original thread: https://box2d.org/forum/viewtopic.php?t=6089