Removing a rigid body

nomad
Posts: 32
Joined: Sun Jun 18, 2006 10:22 pm

Removing a rigid body

Post by nomad »

What is the correct way to remove a rigid body from btDynamicsWorld? Unless I am missing something simple, there is nothing like a btDynamicsWorld::removeRigidBody() function.
Just deleting the rigid body causes btDynamicsWorld::stepSimulation() to crash.
Thanks.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Removing a rigid body

Post by Erwin Coumans »

nomad wrote:What is the correct way to remove a rigid body from btDynamicsWorld? Unless I am missing something simple, there is nothing like a btDynamicsWorld::removeRigidBody() function.
Just deleting the rigid body causes btDynamicsWorld::stepSimulation() to crash.
Thanks.
Pressing 'END' key should remove objects in most Bullet demos. Which version of Bullet are you using? There should be
virtual void removeRigidBody(btRigidBody* body);

Please check out the BasicDemo for the right order to remove things:

Code: Select all

void    BasicDemo::exitPhysics()
{


        //cleanup in the reverse order of creation/initialization

        //remove the rigidbodies from the dynamics world and delete them
        int i;
        for (i=m_dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--)
        {
                btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[i];
                m_dynamicsWorld->removeCollisionObject( obj );
                delete obj;
        }

        //delete collision shapes
        for (unsigned int j=0;j<m_collisionShapes.size();j++)
        {
                btCollisionShape* shape = m_collisionShapes[j];
                delete shape;
        }

        //delete dynamics world
        delete m_dynamicsWorld;

        //delete collision algorithms creation functions
        delete m_sphereSphereCF;


        delete m_sphereBoxCF;
        delete m_boxSphereCF;

        //delete solver
        delete m_solver;

        //delete broadphase
        delete m_overlappingPairCache;

        //delete dispatcher
        delete m_dispatcher;

}
There might be functionaly missing for objects with constraints involved, I have to check that out. Notice that collision shapes can be re-used among several rigidbodies.

This should be really in the Bullet User Manual.

Thanks for the feedback,
Erwin
nomad
Posts: 32
Joined: Sun Jun 18, 2006 10:22 pm

Post by nomad »

Thanks for that ... removeRigidBody() isn't referenced in the docs, and I didn't think to search all the source for removeRigidBody :oops:
nomad
Posts: 32
Joined: Sun Jun 18, 2006 10:22 pm

Re: Removing a rigid body

Post by nomad »

Erwin Coumans wrote:
Please check out the BasicDemo for the right order to remove things:
What about the btDefaultMotionState pointer (created in DemoApplication::localCreateRigidBody() )?
Shouldn't this also be explicitly deleted?
Thanks.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Removing a rigid body

Post by Erwin Coumans »

nomad wrote:
Erwin Coumans wrote:
Please check out the BasicDemo for the right order to remove things:
What about the btDefaultMotionState pointer (created in DemoApplication::localCreateRigidBody() )?
Shouldn't this also be explicitly deleted?
Thanks.
Indeed, that needs to be explicitly deleted indeed. If you already have a graphics object, its destructor would be a good place to delete the motionstate. In the simple Bullet demos, there are no graphics 'objects'.
I'll add some cleanup code in DemoApplication.

Thanks for the feedback,
Erwin