// 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();
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?
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
// 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: