mirror of
https://github.com/overte-org/overte.git
synced 2025-04-19 13:23:36 +02:00
Application gets a ThreadSafePhysicsWorld
This commit is contained in:
parent
0117233ed4
commit
14f49df44d
6 changed files with 71 additions and 10 deletions
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
#include <NetworkPacket.h>
|
||||
#include <NodeList.h>
|
||||
#include <PacketHeaders.h>
|
||||
#include <PhysicsWorld.h>
|
||||
//#include <PhysicsWorld.h>
|
||||
#include <ScriptEngine.h>
|
||||
#include <OctreeQuery.h>
|
||||
#include <ViewFrustum.h>
|
||||
|
@ -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.
|
||||
|
|
|
@ -9,12 +9,55 @@
|
|||
//
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include <EntityTree.h>
|
||||
#include <SharedUtil.h>
|
||||
#include <ThreadSafeDynamicsWorld.h>
|
||||
|
||||
#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) {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,6 +12,20 @@
|
|||
#ifndef hifi_Physics_h
|
||||
#define hifi_Physics_h
|
||||
|
||||
#include <PhysicsWorld.h>
|
||||
|
||||
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);
|
||||
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -39,7 +39,7 @@ public:
|
|||
|
||||
~PhysicsWorld();
|
||||
|
||||
void init();
|
||||
virtual void init();
|
||||
|
||||
void stepSimulation();
|
||||
|
||||
|
|
Loading…
Reference in a new issue