Correctly delete Rigid Bodies and Constraints

Post Reply
MRENOU
Posts: 9
Joined: Thu May 26, 2016 11:50 am

Correctly delete Rigid Bodies and Constraints

Post by MRENOU »

Hi everyone !

I have a vector of rigid bodies r_body with a certain number of constraints r_constraints that I would like to delete in order to reinitialise the scene. But it seems like it's not working...

Here is the code where I delete everything in my scene:

Code: Select all

for (unsigned int i(r_constraints.size() - 1) ; i > 0 ; i--)
{
	world->removeConstraint(r_constraints.at(i));
	delete r_constraints.at(i);
}

//remove the rigidbodies from the dynamics world and delete them
for (int i(world->getNumCollisionObjects() - 1) ; i >= 0 ; i--)
{
	btCollisionObject* obj = world->getCollisionObjectArray()[i];
	btRigidBody* body = btRigidBody::upcast(obj);
	if (body && body->getMotionState())
	{
		delete body->getMotionState();
	}
	world->removeCollisionObject( obj );
	delete obj;
}
	//delete collision shapes
for (unsigned int j(0) ; j < r_shapes.size() ; j++)
{
	btCollisionShape* shape = r_shapes[j];
	r_shapes[j] = 0;
	delete shape;
}

r_body.clear();
r_shapes.clear();
r_motionStates.clear();
r_constraints.clear();
world->clearForces();
There is certainly some errors..

I would be very happy if you could help !
Martin
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: Correctly delete Rigid Bodies and Constraints

Post by benelot »

MRENOU wrote:for (unsigned int i(r_constraints.size() - 1) ; i > 0 ; i--)
you never remove constraint 0. That is what I found on first sight. Probably there is more.

In any case, check the CommonRigidbodyBase.h of the bullet example browser starting at line 93 (exitPhysics), that is exactly what you need.

https://github.com/bulletphysics/bullet ... BodyBase.h
MRENOU
Posts: 9
Joined: Thu May 26, 2016 11:50 am

Re: Correctly delete Rigid Bodies and Constraints

Post by MRENOU »

Thanks a lot ! It works perfectly :)
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: Correctly delete Rigid Bodies and Constraints

Post by benelot »

Can you edit the first post's title and add the [SOLVED] tag to the beginning of it?
Post Reply