How do I remove object from World more quicker?

Ushio
Posts: 4
Joined: Fri Jul 15, 2011 5:18 pm

How do I remove object from World more quicker?

Post by Ushio »

Hello,
I use bullet only to RayCast to many boxes without dynamics simulation.
This is initialize code.

Code: Select all

btCollisionConfiguration* pConfig = new btDefaultCollisionConfiguration();
btCollisionDispatcher* pDispatcher = new btCollisionDispatcher( pConfig );
btDbvtBroadphase* pDbvtBroadPhase = new btDbvtBroadphase();

btCollisionWorld world = new btCollisionWorld( pDispatcher, pDbvtBroadPhase, pConfig );

//add many objects
btCollisionObject obj;
obj.getWorldTransform().setBasis(...);
obj.getWorldTransform().setOrigin(...);
obj.setCollisionShape(...);
world ->addCollisionObject(&obj);
There is a problem at the speed of remove btCollisionObject.
I wrote following codes to remove all CollisionObjects from world.

Code: Select all

for (int i = m_collisionWorld->getNumCollisionObjects() - 1 ; i >= 0 ; i--)
{  
  btCollisionObject* obj = world->getCollisionObjectArray()[i];
  world ->removeCollisionObject( obj );
}
//memory release code is omitted.
but...this code is very slowly when the number of btCollisionObject is about 20,000 or more
How do I remove btCollisionObject from btCollisionWorld more quicker?
I'm looking for more faster method.

#note#
The speed of addCollisionObject is quickly enough.
The speed of RayCast is quickly enough too.

<ratio of speed (about)>
add 20,000 objects : remove 20,000 object = 1 : 25

My English might be strange, because it is Japanese. pardon me.
User avatar
dphil
Posts: 237
Joined: Tue Jun 29, 2010 10:27 pm

Re: How do I remove object from World more quicker?

Post by dphil »

Someone else posted about a similar issue recently. Bullet is presumably doing incremental removals and reprocessing data structures after each one, when in your case some sort of "batch add/remove" operation (ex. btCollisionWorld::removeCollisionObjects(vector<btCollisionObject*>)) would be more efficient. Maybe one exists, but I am unaware of it.
Ushio
Posts: 4
Joined: Fri Jul 15, 2011 5:18 pm

Re: How do I remove object from World more quicker?

Post by Ushio »

Thank you for your answer.
but ,I could'nt find "batch add/remove" operation method...
would you show me more some infomations or some hints about the operation?
User avatar
dphil
Posts: 237
Joined: Tue Jun 29, 2010 10:27 pm

Re: How do I remove object from World more quicker?

Post by dphil »

Ushio wrote:Thank you for your answer.
but ,I could'nt find "batch add/remove" operation method...
would you show me more some infomations or some hints about the operation?
I didn't mean that such an operation exists, just that if it did it could be a better alternative to removing one object at a time. In other words, I was not providing an answer, just adding some thought/speculation to the topic.
User avatar
majestik666
Posts: 66
Joined: Tue Mar 02, 2010 6:13 am

Re: How do I remove object from World more quicker?

Post by majestik666 »

I haven't spent too much time timing this in bullet, but I suspect
very much that the removeOverlappingPairsContainingProxy call in the destroyProxy
function will take a lot of time when destroying a lot of objects...
we have scene with 300,000+ objects and it's pretty long.

Will try and come up with some code to speed this up.

F

void btDbvtBroadphase::destroyProxy( btBroadphaseProxy* absproxy, btDispatcher* dispatcher)
{
btDbvtProxy* proxy=(btDbvtProxy*)absproxy;
if(proxy->stage==STAGECOUNT)
m_sets[1].remove(proxy->leaf);
else
m_sets[0].remove(proxy->leaf);
listremove(proxy,m_stageRoots[proxy->stage]);
m_paircache->removeOverlappingPairsContainingProxy(proxy,dispatcher);
btAlignedFree(proxy);
m_needcleanup=true;
}
Ushio
Posts: 4
Joined: Fri Jul 15, 2011 5:18 pm

Re: How do I remove object from World more quicker?

Post by Ushio »

Thank you for your advice.
I wrote following codes to remove all objects.

Code: Select all

class MyCollisionWorld : public btCollisionWorld
{
public:
	MyCollisionWorld(btDispatcher *dispatcher, btBroadphaseInterface *broadphasePairCache, btCollisionConfiguration *collisionConfiguration):
   btCollisionWorld(dispatcher,broadphasePairCache,collisionConfiguration)
	{
	}
	void removeAllCollisionObjects()
	{
		getBroadphase()->~btBroadphaseInterface();
		new(getBroadphase())btDbvtBroadphase();

		m_collisionObjects.clear();
	}
};
This code was quickly enough.
But I am not confident, becouse this code may have some probrems that I don't know.
Please tell me advice if this code has some probrems.
User avatar
majestik666
Posts: 66
Joined: Tue Mar 02, 2010 6:13 am

Re: How do I remove object from World more quicker?

Post by majestik666 »

Although this should work, bullet still needs faster code
to remove a lot of objects without removing them all....
Ushio
Posts: 4
Joined: Fri Jul 15, 2011 5:18 pm

Re: How do I remove object from World more quicker?

Post by Ushio »

Thank you for your reply.
if the btCollisionWorld has "batch add/remove" operation such as "removeCollisionObjects(vector<btCollisionObject*>))", It is very convenient.