std::vector wont delete btRigidBody!!! [ HELP ]

Post Reply
johnsonalpha
Posts: 73
Joined: Fri May 01, 2015 8:23 pm

std::vector wont delete btRigidBody!!! [ HELP ]

Post by johnsonalpha »

this is my code so far i basically want to delete the btrigidbodies from the dynamics world and clean my scene this is my code so far

Code: Select all

// hold objects in vector
std::vector<btRigidBody*> bodies;

btRigidBody* fallRigidBody = new btRigidBody(fallRigidBodyCI);

bodies.push_back(fallRigidBody);	//to be easier to clean, I store them a vector

for(int i=0;i<bodies.size();i++) {

	    dynamicsWorld->removeCollisionObject(bodies[i]);
	    btMotionState* motionState = bodies[i]->getMotionState();
	    btCollisionShape* shape = bodies[i]->getCollisionShape();
	    delete bodies[i];
	    delete shape;
	    delete motionState;
	 }

	
	for(std::vector<btRigidBody*>::iterator it = bodies.begin() ; it != bodies.end();)
	{	
	 // erase the element
         bodies.erase(it);
	}
	 bodies.clear();
anthrax11
Posts: 72
Joined: Wed Feb 24, 2010 9:49 pm

Re: std::vector wont delete btRigidBody!!! [ HELP ]

Post by anthrax11 »

Looks fine to me, except you might be deleting shapes several times in case they are shared between bodies. This would cause a crash. Also, it's better to use removeRigidBody rather than removeCollisionObject for rigid bodies, but the latter will also work.

What is the problem anyway? Do the bodies not get removed from the world? Is the memory not freed? Does it crash?
trying
Posts: 15
Joined: Sat May 16, 2015 4:32 pm

Re: std::vector wont delete btRigidBody!!! [ HELP ]

Post by trying »

By the way, I have this problem too, after removing the body I can still get its position and rotation.
aviator
Posts: 13
Joined: Thu Apr 02, 2015 5:15 pm

Re: std::vector wont delete btRigidBody!!! [ HELP ]

Post by aviator »

johnsonalpha wrote:this is my code so far i basically want to delete the btrigidbodies from the dynamics world and clean my scene this is my code so far

Code: Select all

// hold objects in vector
std::vector<btRigidBody*> bodies;

btRigidBody* fallRigidBody = new btRigidBody(fallRigidBodyCI);

bodies.push_back(fallRigidBody);	//to be easier to clean, I store them a vector

for(int i=0;i<bodies.size();i++) {

	    dynamicsWorld->removeCollisionObject(bodies[i]);
	    btMotionState* motionState = bodies[i]->getMotionState();
	    btCollisionShape* shape = bodies[i]->getCollisionShape();
	    delete bodies[i];
	    delete shape;
	    delete motionState;
	 }

	
	for(std::vector<btRigidBody*>::iterator it = bodies.begin() ; it != bodies.end();)
	{	
	 // erase the element
         bodies.erase(it);
	}
	 bodies.clear();

In reality you don't need an iterator for a Vector, this should work for most purposes:
List Example:

Code: Select all

std::list<btRigidBody*>					btRBodyList;
std::list<btRigidBody*>::iterator		btRBodyListIterator;

void addRigidBody(btRigidBody* rBody)
{
	dynamicsWorld->addRigidBody(rBody);
	btRBodyList.push_back(rBody);
}

void cleanUpRigidBodies()
{
	std::cout << btRBodyList.size() << std::endl;
	while ( btRBodyList.size() != 0)
	{
		for (btRBodyListIterator = btRBodyList.begin();
			btRBodyListIterator != btRBodyList.end(); ++btRBodyListIterator) {
				
			dynamicsWorld->removeRigidBody(*btRBodyListIterator);
			delete (*btRBodyListIterator)->getMotionState();
			delete (*btRBodyListIterator)->getCollisionShape();
			delete (*btRBodyListIterator);
			btRBodyListIterator = btRBodyList.erase(btRBodyListIterator);
			if (btRBodyListIterator == btRBodyList.end() && btRBodyList.size() == 0) 
			{
				break;
			} 
		}
	}
	std::cout << btRBodyList.size() << std::endl;
}
For vector you would do something slightly different.
Vector Example:

Code: Select all

std::vector<btRigidBody*>               btRBodies;

void addRigidBody(btRigidBody* rBody)
{
   dynamicsWorld->addRigidBody(rBody);
   btRBodies.push_back(rBody);
}

void cleanUpRigidBodies() 
{
	while (!btRBodies.empty()) 
	{
		if (btRBodies.back()) 
		{
			delete btRBodies.back()->getMotionState();
			delete btRBodies.back()->getCollisionShape();
			delete btRBodies.back();
		}
		btRBodies.pop_back();
	}
}
Post Reply