From 14f49df44d3ded809fba81bd2b2cbf1b6618b616 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Thu, 13 Nov 2014 10:02:36 -0800 Subject: [PATCH] Application gets a ThreadSafePhysicsWorld --- interface/src/Application.cpp | 2 +- interface/src/Application.h | 5 +-- interface/src/Physics.cpp | 45 +++++++++++++++++++++++++- interface/src/Physics.h | 14 ++++++++ libraries/physics/src/PhysicsWorld.cpp | 13 +++++--- libraries/physics/src/PhysicsWorld.h | 2 +- 6 files changed, 71 insertions(+), 10 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index f0b4afe90a..50de20bbed 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2011,7 +2011,7 @@ void Application::init() { connect(_myAvatar, &MyAvatar::transformChanged, this, &Application::updateMyAvatarTransform); #ifdef USE_BULLET_PHYSICS - _physicsWorld.init(); + _physicsWorld.initSafe(_entities.getTree()); #endif // USE_BULLET_PHYSICS } diff --git a/interface/src/Application.h b/interface/src/Application.h index fb8518b794..49773ee96e 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -35,7 +35,7 @@ #include #include #include -#include +//#include #include #include #include @@ -53,6 +53,7 @@ #include "Menu.h" #include "MetavoxelSystem.h" #include "PacketHeaders.h" +#include "Physics.h" #include "Stars.h" #include "avatar/Avatar.h" #include "avatar/AvatarManager.h" @@ -504,7 +505,7 @@ private: MetavoxelSystem _metavoxels; #ifdef USE_BULLET_PHYSICS - PhysicsWorld _physicsWorld; + ThreadSafePhysicsWorld _physicsWorld; #endif // USE_BULLET_PHYSICS ViewFrustum _viewFrustum; // current state of view frustum, perspective, orientation, etc. diff --git a/interface/src/Physics.cpp b/interface/src/Physics.cpp index 30ea829c4e..63100ef504 100644 --- a/interface/src/Physics.cpp +++ b/interface/src/Physics.cpp @@ -9,12 +9,55 @@ // #include + +#include #include +#include #include "Util.h" #include "world.h" #include "Physics.h" +// DynamicsImpl is an implementation of ThreadSafeDynamicsWorld that knows how to lock an EntityTree +class DynamicsImpl : public ThreadSafeDynamicsWorld { +public: + DynamicsImpl( + btDispatcher* dispatcher, + btBroadphaseInterface* pairCache, + btConstraintSolver* constraintSolver, + btCollisionConfiguration* collisionConfiguration, + EntityTree* entities) + : ThreadSafeDynamicsWorld(dispatcher, pairCache, constraintSolver, collisionConfiguration), _entities(entities) { + assert(entities); + } + + bool tryLock() { + // wait for lock + _entities->lockForRead(); + return true; + } + + void unlock() { + _entities->unlock(); + } +private: + EntityTree* _entities; +}; + +ThreadSafePhysicsWorld::ThreadSafePhysicsWorld(const glm::vec3& offset) : PhysicsWorld(offset) { +} + +void ThreadSafePhysicsWorld::initSafe(EntityTree* entities) { + assert(!_dynamicsWorld); // only call this once + assert(entities); + _collisionConfig = new btDefaultCollisionConfiguration(); + _collisionDispatcher = new btCollisionDispatcher(_collisionConfig); + _broadphaseFilter = new btDbvtBroadphase(); + _constraintSolver = new btSequentialImpulseConstraintSolver; + // ThreadSafePhysicsWorld gets a DynamicsImpl, which derives from ThreadSafeDynamicsWorld + _dynamicsWorld = new DynamicsImpl(_collisionDispatcher, _broadphaseFilter, _constraintSolver, _collisionConfig, entities); +} + // // Applies static friction: maxVelocity is the largest velocity for which there // there is friction, and strength is the amount of friction force applied to reduce @@ -41,4 +84,4 @@ void applyDamping(float deltaTime, glm::vec3& velocity, float linearStrength, fl void applyDampedSpring(float deltaTime, glm::vec3& velocity, glm::vec3& position, glm::vec3& targetPosition, float k, float damping) { -} \ No newline at end of file +} diff --git a/interface/src/Physics.h b/interface/src/Physics.h index 97e873d920..36bf46a180 100644 --- a/interface/src/Physics.h +++ b/interface/src/Physics.h @@ -12,6 +12,20 @@ #ifndef hifi_Physics_h #define hifi_Physics_h +#include + +class EntityTree; + +class ThreadSafePhysicsWorld : public PhysicsWorld { +public: + ThreadSafePhysicsWorld(const glm::vec3& offset); + + // virtual override from PhysicsWorld + void init() { assert(false); } // call initSafe() instead + + void initSafe(EntityTree* entities); +}; + void applyStaticFriction(float deltaTime, glm::vec3& velocity, float maxVelocity, float strength); void applyDamping(float deltaTime, glm::vec3& velocity, float linearStrength, float squaredStrength); diff --git a/libraries/physics/src/PhysicsWorld.cpp b/libraries/physics/src/PhysicsWorld.cpp index 5541622a66..b112110da8 100644 --- a/libraries/physics/src/PhysicsWorld.cpp +++ b/libraries/physics/src/PhysicsWorld.cpp @@ -15,12 +15,15 @@ PhysicsWorld::~PhysicsWorld() { } +// virtual void PhysicsWorld::init() { - _collisionConfig = new btDefaultCollisionConfiguration(); - _collisionDispatcher = new btCollisionDispatcher(_collisionConfig); - _broadphaseFilter = new btDbvtBroadphase(); - _constraintSolver = new btSequentialImpulseConstraintSolver; - _dynamicsWorld = new btDiscreteDynamicsWorld(_collisionDispatcher, _broadphaseFilter, _constraintSolver, _collisionConfig); + if (!_dynamicsWorld) { + _collisionConfig = new btDefaultCollisionConfiguration(); + _collisionDispatcher = new btCollisionDispatcher(_collisionConfig); + _broadphaseFilter = new btDbvtBroadphase(); + _constraintSolver = new btSequentialImpulseConstraintSolver; + _dynamicsWorld = new btDiscreteDynamicsWorld(_collisionDispatcher, _broadphaseFilter, _constraintSolver, _collisionConfig); + } } void PhysicsWorld::stepSimulation() { diff --git a/libraries/physics/src/PhysicsWorld.h b/libraries/physics/src/PhysicsWorld.h index 0c508af2da..04d0563f84 100644 --- a/libraries/physics/src/PhysicsWorld.h +++ b/libraries/physics/src/PhysicsWorld.h @@ -39,7 +39,7 @@ public: ~PhysicsWorld(); - void init(); + virtual void init(); void stepSimulation();