I'm trying to remove a collision object after a collision occurred. To do this in my collision callback, I add the object to a removal queue that addresses the removal of the collision object before calling stepSimulation. Is there something I am doing wrong with my remove code or approach?
Remove Function:
Code: Select all
void BulletPhysics::removeFromPhysicsWorld(STRigidBody *rigidBody) {
for(int i = m_dynamicsWorld->getNumCollisionObjects() - 1; i>=0; i--){
auto obj = m_dynamicsWorld->getCollisionObjectArray()[i];
auto r = btRigidBody::upcast(obj);
if(obj->getUserPointer() == ((BulletRigidBody*)rigidBody)->getRigidBody()->getUserPointer()){
for(int j = (r->getNumConstraintRefs() - 1); j > 0; j--){
m_dynamicsWorld->removeConstraint(r->getConstraintRef(j));
}
m_broadphase->getOverlappingPairCache()->cleanProxyFromPairs(r->getBroadphaseHandle(), m_dynamicsWorld->getDispatcher());
if(r->getMotionState()){
delete r->getMotionState();
}
m_dynamicsWorld->removeCollisionObject(obj);
delete r->getCollisionShape();
delete obj;
return;
}
}
}
Code: Select all
for(auto rigidBody : m_removeQueue){
removeFromPhysicsWorld(rigidBody);
}
m_removeQueue.clear();
m_dynamicsWorld->stepSimulation(delta, 1, (1.0f / (stReal)STGame::Get()->getTargetFPS()));