How to remove vehicle/rigidbody from dynamics worlds without deleting memory

Post Reply
tomson0119
Posts: 5
Joined: Tue Feb 08, 2022 10:18 am

How to remove vehicle/rigidbody from dynamics worlds without deleting memory

Post by tomson0119 »

I want to remove btRaycastVehicle and btRigidBody from btDiscretDynamicsWorld,
so that any other vehicle wouldn't respond to collision with removed vehicle.

Here's what I did.

Code: Select all

if (mRigidBody)
{
	auto motionState = mRigidBody->getMotionState();
	if (motionState) delete motionState;
	physicsWorld->removeRigidBody(mRigidBody);
	delete mRigidBody;
	mRigidBody = nullptr;
}
if (mVehicle) 
{
	physicsWorld->removeVehicle(mVehicle.get());
	mVehicle.reset();	// unique_ptr, this is bad, no point in using smart pointer..
	mVehicleRayCaster.reset();  // unique_ptr, this is bad, no point in using smart pointer..
}
Which works totally fine.(Let me know this may cause problems)
But since rigidbody can be created and destroyed several times, I want to optimize it by minimizing new/delete calls.
I tried to use only physicsWorld->removeRigidBody and physicsWorld->removeVehicle, and delete memory when program terminated or in specific condition.
But vehicle still respond to removed vehicle..
How to correctly remove vehicle/rigidbody from dynamics world without deleting rigidbody/motion state..
tomson0119
Posts: 5
Joined: Tue Feb 08, 2022 10:18 am

Re: How to remove vehicle/rigidbody from dynamics worlds without deleting memory

Post by tomson0119 »

Hmm... seems like my game logic was wrong.. Sorry
I can prevent collision just by using removeRigidBody/removeVehicle..

EDIT: One more question. Is removing rigidbody from dynamics world also stop detecting collision?
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: How to remove vehicle/rigidbody from dynamics worlds without deleting memory

Post by drleviathan »

Yes, if you remove a body from the dynamics world then it will not collide.

There are ways to leave a body in the dynamics world while disabling all or selective collisions. One way to do this is to assign each object to one or more collision groups and to specify which groups it collides with. This collision group bits and mask bits are arguments in the btDiscreteDynamicsWorld::addRigidBody() method:

Code: Select all

void addRigidBody(btRigidBody* body, int group, int mask);
The group argument has a bit set for each group to which the body belongs and the mask argument has a bit set for each group against which the body collides. Two objects only collide if both group&mask pairs are non-zero. That is:

body_A and body_B collide iff (group_A & mask_B) && (group_B & mask_A) != 0.

The group and mask are not stored at the body but at the broadphase proxy, which means (a) the check is performed in the broadphase, before overlapping pairs are submitted to the narrowphase and (b) if you want to dynamically change the group/mask of an object then you must update its broadphase proxy, which typically means removing/re-adding the object to the world.
tomson0119
Posts: 5
Joined: Tue Feb 08, 2022 10:18 am

Re: How to remove vehicle/rigidbody from dynamics worlds without deleting memory

Post by tomson0119 »

Thank you so much, That's exactly what I was looking for!
Post Reply