How to remove a body from the simulation...

Zeal
Posts: 47
Joined: Thu Oct 18, 2007 6:49 am

How to remove a body from the simulation...

Post by Zeal »

I have a very high churn rate when it comes to adding/deleting objects in my simulation. I need a quick way to 'remove' a body from the simulation (so it wont collide with other objects, wont eat up cpu time, ect...) WITHOUT having to actually delete the body. In other words, I want to be able to quickly 'hide' the body, then 'show' it when a new object/bodies is needed.

Is this possible?

Thanks!
Zeal
Posts: 47
Joined: Thu Oct 18, 2007 6:49 am

Re: How to remove a body from the simulation...

Post by Zeal »

Hmm so is the consensus that there is no way to do this?

Is the only option to use something like boost::pool to help with the constant allocation/deallocation?
Oogst
Posts: 25
Joined: Thu Apr 10, 2008 11:19 am
Location: Utrecht, Netherlands

Re: How to remove a body from the simulation...

Post by Oogst »

Deleting and removing from the simulation are separate steps. Can't you remove an object from the simulation through dynamicsWorld->removeCollisionObject(banana); and then instead of deleting it, inserting it's pointer in a std::vector (i.e. a memory pool, indeed). Later on, you can grab it from the vector and put it back in.

This seems a simple and good solution to me, am I getting your problem right?
chunky
Posts: 145
Joined: Tue Oct 30, 2007 9:23 pm

Re: How to remove a body from the simulation...

Post by chunky »

Perhaps you could have an off-screen cache of your objects. Like, when an object is "removed", move it to (-1000, -1000+(objectwidth+4)*objectindex, -1000) and deactivate it.

Bit crappy, but it'd probably "work"...

Gary (-;
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: How to remove a body from the simulation...

Post by Erwin Coumans »

Most efficient is to set the to DISABLE_SIMULATION to disable collision detection and simulation, but this is untested. Some additional check should be added in btCollisionDispatcher::needsCollision(btCollisionObject* body0,btCollisionObject* body1)

Code: Select all

body->setActivationState(DISABLE_SIMULATION );
To enable it, you can use

Code: Select all

body->setActivationState(ACTIVE_TAG);
Removing and adding back to the btDynamicsWorld would be another option indeed. However, it still requires removing and inserting from the broadphase, which has a fairly high cost.

Instead of std::vector or boost::pool, you can use Bullet's native btAlignedObjectArray. It has a similar interface to std::vector, and it automatically uses Bullet's aligned memory allocator.

Hope this helps,
Erwin