How to disbale simulation for a rigidbody?

User avatar
Neo
Posts: 10
Joined: Fri Feb 19, 2016 2:04 am

How to disbale simulation for a rigidbody?

Post by Neo »

I'm trying to make an object pool for objects that add and remove frequently, such as arrows and machine gun bullets.
I tried setActivationState() and forceActivationState(), but neither of them worked, other rigidbody still collide with them when their state
has been set to DISABLE_SIMULATION.
Any suggestions?
User avatar
Typhontaur
Posts: 135
Joined: Fri Nov 04, 2005 2:22 pm

Re: How to disbale simulation for a rigidbody?

Post by Typhontaur »

you try like this?
rigidBody->setCollisionFlags(rigidBody->getCollisionFlags() | btCollisionObject::CF_NO_CONTACT_RESPONSE);
...
rigidBody->setCollisionFlags(rigidBody->getCollisionFlags() & ~btCollisionObject::CF_NO_CONTACT_RESPONSE);

here an example
https://www.youtube.com/watch?v=JhG-_PxwlsQ
...
https://www.youtube.com/watch?v=E-HPPsEUa4I
User avatar
Neo
Posts: 10
Joined: Fri Feb 19, 2016 2:04 am

Re: How to disbale simulation for a rigidbody?

Post by Neo »

Typhontaur wrote:you try like this?
rigidBody->setCollisionFlags(rigidBody->getCollisionFlags() | btCollisionObject::CF_NO_CONTACT_RESPONSE);
...
rigidBody->setCollisionFlags(rigidBody->getCollisionFlags() & ~btCollisionObject::CF_NO_CONTACT_RESPONSE);

here an example
https://www.youtube.com/watch?v=JhG-_PxwlsQ
...
https://www.youtube.com/watch?v=E-HPPsEUa4I
Thank you, it works, though I still get collisions in getManifoldByIndexInternal(),
would that influence the performance by doing collision detection on disabled object? How to disable it from collision detection as well?
User avatar
Typhontaur
Posts: 135
Joined: Fri Nov 04, 2005 2:22 pm

Re: How to disbale simulation for a rigidbody?

Post by Typhontaur »

I dont know much about getManifoldByIndexInternal(), I never used it...
In my engine, I use also :
for disable:
body->setActivationState(ISLAND_SLEEPING);
body->setCollisionFlags(body->getCollisionFlags() | btCollisionObject::CF_STATIC_OBJECT);
for enable:
body->setActivationState(ACTIVE_TAG);
body->setCollisionFlags(body->getCollisionFlags() & ~btCollisionObject::CF_STATIC_OBJECT);

I hope to help ;)
User avatar
Neo
Posts: 10
Joined: Fri Feb 19, 2016 2:04 am

Re: How to disbale simulation for a rigidbody?

Post by Neo »

Thank you.
Basically, I use getManifoldByIndexInternal() to implement custom collision callback.
From my test,your code indeed disable the physics simulation but did not disable the collision detection.
I can still get the information when two objects overlapping each other( one of them is set disabled).
My question is, does this have any influence upon the overall performance?
User avatar
Neo
Posts: 10
Joined: Fri Feb 19, 2016 2:04 am

Re: How to disbale simulation for a rigidbody?

Post by Neo »

OK, after hours of search and try, I got a solution.
Use collision filter group to filter out the disabled object.

Set the filter group at the begining

Code: Select all

int groupActive=1;
int groupDisable=2;
Then write a custom filter callback

Code: Select all

struct filterCallback : public btOverlapFilterCallback
{
	virtual bool needBroadphaseCollision(btBroadphaseProxy*proxy0,btBroadphaseProxy*proxy1)const
	{
		bool collides=(proxy0->m_collisionFilterGroup&groupActive)&&(proxy1->m_collisionFilterGroup&groupActive);
		return collides;
	}
};
register it,

Code: Select all

btOverlapFilterCallback*filtercbk=new filterCallback();
dynamicsWorld->getPairCache()->setOverlapFilterCallback(filtercbk);
When adding rigid body, set then in active group

Code: Select all

dynamicsWorld->addRigidBody(body,groupActive,groupActive);
When you want to disable a object,first stop simulate the rigid body, then change
the filter group to disabled group(it takes me a long time to find out how to change filter group without removing and re-adding object)

Code: Select all

body->setActivationState(DISABLE_SIMULATION );
body->getBroadphaseProxy()->m_collisionFilterGroup=groupDisable;
Then you won't get any simulation and collision callback on this object any more.

The drawback of this method is, aabb match still take place before filtering, to stop physics COMPLETELY on disabled object requires modification
on the broadphase. I'll appreciate it if anyone knows how to achieve this goal without change API :)