# Change density of the body dynamically

In my game, I want to change density of my body object when it collide with other objects. I found something like following to change density but further I could not able to find any hint for this. So someone please help.

Fixture fixture = goldenBoxArrayList.get(i)
.getGoldenBoxBody()
.getFixtureList().get(0);
fixture.setDensity(0.5f);


After setting fixture data I could not able to set it to the body.

EDIT : I want to change my object body density when physics world in simulation

• What are you trying to do? Why do you need to change the density of an object? – knight666 Jun 13 '12 at 12:22
• How to ask questions the smart way: ask the goal, not the step. – doppelgreener Jun 13 '12 at 12:25

I'm not sure about AndEngine but in Box2D you can call the following functions:

fixture->SetDensity(5.0f); //this fixture is attached to body below
body->ResetMassData(); //must call this after changing density


AndEngine's physics is just a JNI wrapper around Box2D so it should support all or a subset of Box2D features.

Also check 7.4 Using a Body of Box2D manual, its possible to override mass data calculated from fixtures on a body using:

void SetMassData(const b2MassData* data);

• Yes that I want. Your consideration work perfectly. – Siddharth Jun 14 '12 at 5:13