mirror of
https://github.com/overte-org/overte.git
synced 2025-08-06 20:58:38 +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);
|
connect(_myAvatar, &MyAvatar::transformChanged, this, &Application::updateMyAvatarTransform);
|
||||||
|
|
||||||
#ifdef USE_BULLET_PHYSICS
|
#ifdef USE_BULLET_PHYSICS
|
||||||
_physicsWorld.init();
|
_physicsWorld.initSafe(_entities.getTree());
|
||||||
#endif // USE_BULLET_PHYSICS
|
#endif // USE_BULLET_PHYSICS
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -35,7 +35,7 @@
|
||||||
#include <NetworkPacket.h>
|
#include <NetworkPacket.h>
|
||||||
#include <NodeList.h>
|
#include <NodeList.h>
|
||||||
#include <PacketHeaders.h>
|
#include <PacketHeaders.h>
|
||||||
#include <PhysicsWorld.h>
|
//#include <PhysicsWorld.h>
|
||||||
#include <ScriptEngine.h>
|
#include <ScriptEngine.h>
|
||||||
#include <OctreeQuery.h>
|
#include <OctreeQuery.h>
|
||||||
#include <ViewFrustum.h>
|
#include <ViewFrustum.h>
|
||||||
|
@ -53,6 +53,7 @@
|
||||||
#include "Menu.h"
|
#include "Menu.h"
|
||||||
#include "MetavoxelSystem.h"
|
#include "MetavoxelSystem.h"
|
||||||
#include "PacketHeaders.h"
|
#include "PacketHeaders.h"
|
||||||
|
#include "Physics.h"
|
||||||
#include "Stars.h"
|
#include "Stars.h"
|
||||||
#include "avatar/Avatar.h"
|
#include "avatar/Avatar.h"
|
||||||
#include "avatar/AvatarManager.h"
|
#include "avatar/AvatarManager.h"
|
||||||
|
@ -504,7 +505,7 @@ private:
|
||||||
MetavoxelSystem _metavoxels;
|
MetavoxelSystem _metavoxels;
|
||||||
|
|
||||||
#ifdef USE_BULLET_PHYSICS
|
#ifdef USE_BULLET_PHYSICS
|
||||||
PhysicsWorld _physicsWorld;
|
ThreadSafePhysicsWorld _physicsWorld;
|
||||||
#endif // USE_BULLET_PHYSICS
|
#endif // USE_BULLET_PHYSICS
|
||||||
|
|
||||||
ViewFrustum _viewFrustum; // current state of view frustum, perspective, orientation, etc.
|
ViewFrustum _viewFrustum; // current state of view frustum, perspective, orientation, etc.
|
||||||
|
|
|
@ -9,12 +9,55 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
#include <EntityTree.h>
|
||||||
#include <SharedUtil.h>
|
#include <SharedUtil.h>
|
||||||
|
#include <ThreadSafeDynamicsWorld.h>
|
||||||
|
|
||||||
#include "Util.h"
|
#include "Util.h"
|
||||||
#include "world.h"
|
#include "world.h"
|
||||||
#include "Physics.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
|
// 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
|
// 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) {
|
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
|
#ifndef hifi_Physics_h
|
||||||
#define 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 applyStaticFriction(float deltaTime, glm::vec3& velocity, float maxVelocity, float strength);
|
||||||
void applyDamping(float deltaTime, glm::vec3& velocity, float linearStrength, float squaredStrength);
|
void applyDamping(float deltaTime, glm::vec3& velocity, float linearStrength, float squaredStrength);
|
||||||
|
|
||||||
|
|
|
@ -15,12 +15,15 @@
|
||||||
PhysicsWorld::~PhysicsWorld() {
|
PhysicsWorld::~PhysicsWorld() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// virtual
|
||||||
void PhysicsWorld::init() {
|
void PhysicsWorld::init() {
|
||||||
_collisionConfig = new btDefaultCollisionConfiguration();
|
if (!_dynamicsWorld) {
|
||||||
_collisionDispatcher = new btCollisionDispatcher(_collisionConfig);
|
_collisionConfig = new btDefaultCollisionConfiguration();
|
||||||
_broadphaseFilter = new btDbvtBroadphase();
|
_collisionDispatcher = new btCollisionDispatcher(_collisionConfig);
|
||||||
_constraintSolver = new btSequentialImpulseConstraintSolver;
|
_broadphaseFilter = new btDbvtBroadphase();
|
||||||
_dynamicsWorld = new btDiscreteDynamicsWorld(_collisionDispatcher, _broadphaseFilter, _constraintSolver, _collisionConfig);
|
_constraintSolver = new btSequentialImpulseConstraintSolver;
|
||||||
|
_dynamicsWorld = new btDiscreteDynamicsWorld(_collisionDispatcher, _broadphaseFilter, _constraintSolver, _collisionConfig);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicsWorld::stepSimulation() {
|
void PhysicsWorld::stepSimulation() {
|
||||||
|
|
|
@ -39,7 +39,7 @@ public:
|
||||||
|
|
||||||
~PhysicsWorld();
|
~PhysicsWorld();
|
||||||
|
|
||||||
void init();
|
virtual void init();
|
||||||
|
|
||||||
void stepSimulation();
|
void stepSimulation();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue