Issue #225: Added cleanup of maps used in PhysicEngine.

This commit is contained in:
Lukasz Gromanowski 2012-03-25 16:12:23 +02:00
parent 7b3ecc290e
commit 633e80cded
2 changed files with 55 additions and 8 deletions

View file

@ -173,6 +173,7 @@ namespace Physic
mShapeLoader = shapeLoader; mShapeLoader = shapeLoader;
isDebugCreated = false; isDebugCreated = false;
mDebugDrawer = NULL;
} }
void PhysicEngine::createDebugRendering() void PhysicEngine::createDebugRendering()
@ -202,6 +203,35 @@ namespace Physic
PhysicEngine::~PhysicEngine() PhysicEngine::~PhysicEngine()
{ {
RigidBodyContainer::iterator rb_it = RigidBodyMap.begin();
for (; rb_it != RigidBodyMap.end(); ++rb_it)
{
if (rb_it->second != NULL)
{
dynamicsWorld->removeRigidBody(rb_it->second);
delete rb_it->second;
rb_it->second = NULL;
}
}
PhysicActorContainer::iterator pa_it = PhysicActorMap.begin();
for (; pa_it != PhysicActorMap.end(); ++pa_it)
{
if (pa_it->second != NULL)
{
dynamicsWorld->removeCollisionObject(pa_it->second->externalGhostObject);
dynamicsWorld->removeCollisionObject(pa_it->second->internalGhostObject);
dynamicsWorld->removeAction(pa_it->second->mCharacter);
delete pa_it->second;
pa_it->second = NULL;
}
}
delete mDebugDrawer;
delete dynamicsWorld; delete dynamicsWorld;
delete solver; delete solver;
delete collisionConfiguration; delete collisionConfiguration;
@ -239,32 +269,39 @@ namespace Physic
dynamicsWorld->addRigidBody(body,COL_WORLD,COL_NOTHING); dynamicsWorld->addRigidBody(body,COL_WORLD,COL_NOTHING);
} }
body->setActivationState(DISABLE_DEACTIVATION); body->setActivationState(DISABLE_DEACTIVATION);
RigidBody* oldBody = RigidBodyMap[body->mName];
if (oldBody != NULL)
{
dynamicsWorld->removeRigidBody(oldBody);
delete oldBody;
}
RigidBodyMap[body->mName] = body; RigidBodyMap[body->mName] = body;
} }
void PhysicEngine::removeRigidBody(std::string name) void PhysicEngine::removeRigidBody(std::string name)
{ {
std::map<std::string,RigidBody*>::iterator it = RigidBodyMap.find(name); RigidBodyContainer::iterator it = RigidBodyMap.find(name);
if (it != RigidBodyMap.end() ) if (it != RigidBodyMap.end() )
{ {
RigidBody* body = it->second; RigidBody* body = it->second;
if(body != NULL) if(body != NULL)
{ {
// broadphase->getOverlappingPairCache()->removeOverlappingPairsContainingProxy(body->getBroadphaseProxy(),dispatcher); // broadphase->getOverlappingPairCache()->removeOverlappingPairsContainingProxy(body->getBroadphaseProxy(),dispatcher);
/*std::map<std::string,PhysicActor*>::iterator it2 = PhysicActorMap.begin(); /*PhysicActorContainer::iterator it2 = PhysicActorMap.begin();
for(;it2!=PhysicActorMap.end();it++) for(;it2!=PhysicActorMap.end();it++)
{ {
it2->second->internalGhostObject->getOverlappingPairCache()->removeOverlappingPairsContainingProxy(body->getBroadphaseProxy(),dispatcher); it2->second->internalGhostObject->getOverlappingPairCache()->removeOverlappingPairsContainingProxy(body->getBroadphaseProxy(),dispatcher);
it2->second->externalGhostObject->getOverlappingPairCache()->removeOverlappingPairsContainingProxy(body->getBroadphaseProxy(),dispatcher); it2->second->externalGhostObject->getOverlappingPairCache()->removeOverlappingPairsContainingProxy(body->getBroadphaseProxy(),dispatcher);
}*/ }*/
dynamicsWorld->removeRigidBody(RigidBodyMap[name]); dynamicsWorld->removeRigidBody(body);
} }
} }
} }
void PhysicEngine::deleteRigidBody(std::string name) void PhysicEngine::deleteRigidBody(std::string name)
{ {
std::map<std::string,RigidBody*>::iterator it = RigidBodyMap.find(name); RigidBodyContainer::iterator it = RigidBodyMap.find(name);
if (it != RigidBodyMap.end() ) if (it != RigidBodyMap.end() )
{ {
RigidBody* body = it->second; RigidBody* body = it->second;
@ -293,6 +330,10 @@ namespace Physic
void PhysicEngine::addCharacter(std::string name) void PhysicEngine::addCharacter(std::string name)
{ {
// Remove character with given name, so we don't make memory
// leak when character would be added twice
removeCharacter(name);
PhysicActor* newActor = new PhysicActor(name); PhysicActor* newActor = new PhysicActor(name);
dynamicsWorld->addCollisionObject( newActor->externalGhostObject, COL_ACTOR_EXTERNAL, COL_WORLD |COL_ACTOR_EXTERNAL ); dynamicsWorld->addCollisionObject( newActor->externalGhostObject, COL_ACTOR_EXTERNAL, COL_WORLD |COL_ACTOR_EXTERNAL );
dynamicsWorld->addCollisionObject( newActor->internalGhostObject, COL_ACTOR_INTERNAL, COL_WORLD |COL_ACTOR_INTERNAL ); dynamicsWorld->addCollisionObject( newActor->internalGhostObject, COL_ACTOR_INTERNAL, COL_WORLD |COL_ACTOR_INTERNAL );
@ -303,7 +344,7 @@ namespace Physic
void PhysicEngine::removeCharacter(std::string name) void PhysicEngine::removeCharacter(std::string name)
{ {
//std::cout << "remove"; //std::cout << "remove";
std::map<std::string,PhysicActor*>::iterator it = PhysicActorMap.find(name); PhysicActorContainer::iterator it = PhysicActorMap.find(name);
if (it != PhysicActorMap.end() ) if (it != PhysicActorMap.end() )
{ {
PhysicActor* act = it->second; PhysicActor* act = it->second;
@ -311,7 +352,7 @@ namespace Physic
{ {
/*broadphase->getOverlappingPairCache()->removeOverlappingPairsContainingProxy(act->externalGhostObject->getBroadphaseHandle(),dispatcher); /*broadphase->getOverlappingPairCache()->removeOverlappingPairsContainingProxy(act->externalGhostObject->getBroadphaseHandle(),dispatcher);
broadphase->getOverlappingPairCache()->removeOverlappingPairsContainingProxy(act->internalGhostObject->getBroadphaseHandle(),dispatcher); broadphase->getOverlappingPairCache()->removeOverlappingPairsContainingProxy(act->internalGhostObject->getBroadphaseHandle(),dispatcher);
std::map<std::string,PhysicActor*>::iterator it2 = PhysicActorMap.begin(); PhysicActorContainer::iterator it2 = PhysicActorMap.begin();
for(;it2!=PhysicActorMap.end();it++) for(;it2!=PhysicActorMap.end();it++)
{ {
it->second->internalGhostObject->getOverlappingPairCache()->removeOverlappingPairsContainingProxy(act->externalGhostObject->getBroadphaseHandle(),dispatcher); it->second->internalGhostObject->getOverlappingPairCache()->removeOverlappingPairsContainingProxy(act->externalGhostObject->getBroadphaseHandle(),dispatcher);

View file

@ -42,6 +42,8 @@ namespace Physic
:btPairCachingGhostObject(),mName(name) :btPairCachingGhostObject(),mName(name)
{ {
} }
virtual ~PairCachingGhostObject(){}
std::string mName; std::string mName;
}; };
@ -106,6 +108,7 @@ namespace Physic
{ {
public: public:
RigidBody(btRigidBody::btRigidBodyConstructionInfo& CI,std::string name); RigidBody(btRigidBody::btRigidBodyConstructionInfo& CI,std::string name);
virtual ~RigidBody() {}
std::string mName; std::string mName;
//is this body used for raycasting only? //is this body used for raycasting only?
@ -217,8 +220,11 @@ namespace Physic
//the NIF file loader. //the NIF file loader.
BulletShapeLoader* mShapeLoader; BulletShapeLoader* mShapeLoader;
std::map<std::string,RigidBody*> RigidBodyMap; typedef std::map<std::string,RigidBody*> RigidBodyContainer;
std::map<std::string,PhysicActor*> PhysicActorMap; RigidBodyContainer RigidBodyMap;
typedef std::map<std::string, PhysicActor*> PhysicActorContainer;
PhysicActorContainer PhysicActorMap;
//debug rendering //debug rendering
BtOgre::DebugDrawer* mDebugDrawer; BtOgre::DebugDrawer* mDebugDrawer;