Bullet Physics: How to change body mass

By on January 28, 2014

We’ve run into a problem with updates of a mass of the body in our game Burnt Islands. We had some flying robots on the platforms of their factory. These robots were static objects in the beginning with no mass (mass = 0).
Rotor monster factory with monsters

After the factory was destroyed the robots would have to fly up and begin to attack. In order to do that they got mass set so that static objects became dynamic. According to Bullet Physics specification it should work with just this code:

btVector3 inertia;
body->getCollisionShape()->calculateLocalInertia( mass, inertia );
body->setMassProps(mass, inertia);

But that just didn’t work. Robots were “hanging” in the air. They wanted to attack, but couldn’t move.

The result was many robots “hanging” in the air. They wanted to attack, but couldn’t move.
Rotor robots hang in the air

The solution was to reactivate the body: remove the rigid body from the dynamics world, make the changes to the mass, and re-add the body to the dynamics world.

//Remove the rigid body from the dynamics world
m_PhysicsWorld->removeRigidBody( bt->body() );

btVector3 inertia;
body->getCollisionShape()->calculateLocalInertia( mass, inertia );
body->setMassProps(mass, inertia);

//Add the rigid body to the dynamics world
m_PhysicsWorld->addRigidBody( bt->body() );

Here is how the robots should have behaved:
Rotor robots attack the player

Comments

  1. Di Giorgi
    April 6, 2014

    Leave a Reply

    Thanks for the very easy explain. i was thinking in recreate the object. i Will test your code, thanks!

Leave a Reply

*



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>