diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 5753449075..549f05a905 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -608,6 +608,7 @@ Application::~Application() { Menu::getInstance()->deleteLater(); + _physicsEngine.setCharacterController(NULL); _myAvatar = NULL; ModelEntityItem::cleanupLoadedAnimations(); diff --git a/libraries/physics/src/CharacterController.cpp b/libraries/physics/src/CharacterController.cpp index 148746a76e..626a1ea3a9 100644 --- a/libraries/physics/src/CharacterController.cpp +++ b/libraries/physics/src/CharacterController.cpp @@ -241,6 +241,12 @@ CharacterController::CharacterController(AvatarData* avatarData) { } CharacterController::~CharacterController() { + delete _ghostObject; + _ghostObject = NULL; + delete _convexShape; + _convexShape = NULL; + // make sure you remove this Character from its DynamicsWorld before reaching this spot + assert(_dynamicsWorld == NULL); } btPairCachingGhostObject* CharacterController::getGhostObject() { diff --git a/libraries/physics/src/PhysicsEngine.cpp b/libraries/physics/src/PhysicsEngine.cpp index aabf3dc331..139a954b22 100644 --- a/libraries/physics/src/PhysicsEngine.cpp +++ b/libraries/physics/src/PhysicsEngine.cpp @@ -28,6 +28,9 @@ PhysicsEngine::PhysicsEngine(const glm::vec3& offset) } PhysicsEngine::~PhysicsEngine() { + if (_characterController) { + _characterController->setDynamicsWorld(NULL); + } // TODO: delete engine components... if we ever plan to create more than one instance delete _collisionConfig; delete _collisionDispatcher; @@ -614,8 +617,14 @@ bool PhysicsEngine::updateObjectHard(btRigidBody* body, ObjectMotionState* motio } void PhysicsEngine::setCharacterController(CharacterController* character) { - if (!_characterController) { + if (_characterController != character) { lock(); + if (_characterController) { + // remove the character from the DynamicsWorld immediately + _characterController->setDynamicsWorld(NULL); + _characterController = NULL; + } + // the character will be added to the DynamicsWorld later _characterController = character; unlock(); }