From 9b13f186e3158e92ed18da297c22142a98bf5bb1 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Mon, 30 Mar 2015 15:51:58 -0700 Subject: [PATCH 1/2] remove character from PhysicsEngine on shutdown --- interface/src/Application.cpp | 1 + libraries/physics/src/CharacterController.cpp | 6 ++++++ libraries/physics/src/PhysicsEngine.cpp | 15 +++++++++++++-- 3 files changed, 20 insertions(+), 2 deletions(-) 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 f7dc90e72f..c9fdbc4857 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,10 +617,18 @@ bool PhysicsEngine::updateObjectHard(btRigidBody* body, ObjectMotionState* motio } void PhysicsEngine::setCharacterController(CharacterController* character) { - if (!_characterController) { + if (_characterController != character) { lock(); - _characterController = character; + if (_characterController) { + // remove the character from the DynamicsWorld immediately + _characterController->setDynamicsWorld(NULL); + _characterController = NULL; + } unlock(); + if (character) { + // the character will be added to the DynamicsWorld later + _characterController = character; + } } } From c6b34df06cd2a48ba70b775d5480f730b83d6b9f Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Mon, 30 Mar 2015 16:07:56 -0700 Subject: [PATCH 2/2] unlock after character is properly updated --- libraries/physics/src/PhysicsEngine.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libraries/physics/src/PhysicsEngine.cpp b/libraries/physics/src/PhysicsEngine.cpp index c9fdbc4857..83fad9228d 100644 --- a/libraries/physics/src/PhysicsEngine.cpp +++ b/libraries/physics/src/PhysicsEngine.cpp @@ -624,11 +624,9 @@ void PhysicsEngine::setCharacterController(CharacterController* character) { _characterController->setDynamicsWorld(NULL); _characterController = NULL; } + // the character will be added to the DynamicsWorld later + _characterController = character; unlock(); - if (character) { - // the character will be added to the DynamicsWorld later - _characterController = character; - } } }