How to remove all objects from world quickly

Post Reply
kore3d
Posts: 1
Joined: Tue Jul 31, 2018 2:16 pm

How to remove all objects from world quickly

Post by kore3d »

Profiler says that removeCollisionObject per object is slow. I want to make it faster.

Code: Select all

  for (auto& node : m_nodes)
    m_pCollisionWorld->removeCollisionObject(node.second.get());
I saw solution here https://pybullet.org/Bullet/phpBB3/viewtopic.php?t=7062 But I'm not sure that it's correct way to do it.
Could anybody help me?
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: How to remove all objects from world quickly

Post by drleviathan »

If you look at the implementation of btCollisionWorld::removeCollisionObject() you can see what it is doing. My guess would be the cost of removing from the broadphase is the most expensive part. If I wanted to remove ALL objects at once then I would derive MyDynamicsWorld from btDiscreteDynamicsWorld and give it a clearAllCollisionObjects() method that does the following:

(a) delete the broadphase and then add a new empty one
(b) clear the m_collisionObjects array, and
(c) clear m_nonStaticRigidBodies.

Note: that would leave dangling "worldArrayIndices" on the btCollisionObjects so if I was going to recycle those I would need to go through and clear those indices, else delete 'em.
Post Reply