Set rigidbody mass at any time

Petwoip
Posts: 5
Joined: Sun Jan 15, 2012 10:53 pm

Set rigidbody mass at any time

Post by Petwoip »

Hey, so far I have static and dynamic objects working by passing a mass value into the rigidbody constructor.

However, I'm trying to set an object's mass after the rigidbody has been constructed. For example, I have a dynamic object with mass 1 that I want to become static. My goal is to be able to say something like: object->setMass(0) and have bullet update the rigidbody mass appropriately (and this should work after the rigidbody has been added to the world).

Here is what I have tried:
  • rigidBody->setMassProps(0,btVector3(0,0,0));
    This successfully made the object static, but then the collision detection for that object started to mess up (objects started to slide along the top of the object and dip into its side).

    rigidBody->setCollisionFlags(rigidBody->getCollisionFlags() | btCollisionObject::CF_STATIC_OBJECT);
    This successfully made the object static, but like my other attempt the collisions started to mess up. Objects would partially go through and get stuck in the middle.
I've tried putting these two lines together but nothing meaningful came out.

So, is there a way to set an object's mass (or make it static) after the rigidbody has been constructed and after the object has been added to the world? Is there something about mass in bullet that I don't understand?
User avatar
jarno
Posts: 57
Joined: Tue Mar 16, 2010 1:42 am

Re: Set rigidbody mass at any time

Post by jarno »

As well as setting the mass to 0 and enabling the static collision flag, also set the linear and angular velocities to 0 (with setLinearVelocity() and setAngularVelocity()), and recompute the inertia tensor after setting the mass properties (with updateInertiaTensor()).

If you don't set the velocities to 0 then anything colliding with it will collide as if the object is still moving instead of stationary.

---JvdL---