Page 1 of 1

Correctly delete Rigid Bodies and Constraints

Posted: Wed Jun 01, 2016 2:10 pm
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

Re: Correctly delete Rigid Bodies and Constraints

Posted: Wed Jun 01, 2016 5:32 pm
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

Re: Correctly delete Rigid Bodies and Constraints

Posted: Wed Jun 01, 2016 8:00 pm
by MRENOU
Thanks a lot ! It works perfectly :)

Re: Correctly delete Rigid Bodies and Constraints

Posted: Wed Jun 29, 2016 10:38 pm
by benelot
Can you edit the first post's title and add the [SOLVED] tag to the beginning of it?