Overte C++ Documentation
ThreadSafeDynamicsWorld.h
1 /*
2  * Bullet Continuous Collision Detection and Physics Library
3  * Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org
4  *
5  * This software is provided 'as-is', without any express or implied warranty.
6  * In no event will the authors be held liable for any damages arising from the use of this software.
7  * Permission is granted to anyone to use this software for any purpose,
8  * including commercial applications, and to alter it and redistribute it freely,
9  * subject to the following restrictions:
10  *
11  * 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12  * 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13  * 3. This notice may not be removed or altered from any source distribution.
14  *
15  * Copied and modified from btDiscreteDynamicsWorld.h by AndrewMeadows on 2014.11.12.
16  * */
17 
18 #ifndef hifi_ThreadSafeDynamicsWorld_h
19 #define hifi_ThreadSafeDynamicsWorld_h
20 
21 #include <BulletDynamics/Dynamics/btRigidBody.h>
22 #include <BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h>
23 
24 #include "ObjectMotionState.h"
25 
26 #include <functional>
27 
28 using SubStepCallback = std::function<void()>;
29 
30 ATTRIBUTE_ALIGNED16(class) ThreadSafeDynamicsWorld : public btDiscreteDynamicsWorld {
31 public:
32  BT_DECLARE_ALIGNED_ALLOCATOR();
33 
34  ThreadSafeDynamicsWorld(
35  btDispatcher* dispatcher,
36  btBroadphaseInterface* pairCache,
37  btConstraintSolver* constraintSolver,
38  btCollisionConfiguration* collisionConfiguration);
39 
40  int getNumSubsteps() const { return _numSubsteps; }
41  int stepSimulationWithSubstepCallback(btScalar timeStep, int maxSubSteps = 1,
42  btScalar fixedTimeStep = btScalar(1.)/btScalar(60.),
43  SubStepCallback onSubStep = []() { });
44  virtual void synchronizeMotionStates() override;
45  virtual void saveKinematicState(btScalar timeStep) override;
46 
47  // btDiscreteDynamicsWorld::m_localTime is the portion of real-time that has not yet been simulated
48  // but is used for MotionState::setWorldTransform() extrapolation (a feature that Bullet uses to provide
49  // smoother rendering of objects when the physics simulation loop is ansynchronous to the render loop).
50  float getLocalTimeAccumulation() const { return m_localTime; }
51 
52  const VectorOfMotionStates& getChangedMotionStates() const { return _changedMotionStates; }
53  const VectorOfMotionStates& getDeactivatedMotionStates() const { return _deactivatedStates; }
54 
55  void addChangedMotionState(ObjectMotionState* motionState) { _changedMotionStates.push_back(motionState); }
56  virtual void debugDrawObject(const btTransform& worldTransform, const btCollisionShape* shape, const btVector3& color) override;
57 
58 private:
59  // call this instead of non-virtual btDiscreteDynamicsWorld::synchronizeSingleMotionState()
60  void synchronizeMotionState(btRigidBody* body);
61  void drawConnectedSpheres(btIDebugDraw* drawer, btScalar radius1, btScalar radius2, const btVector3& position1,
62  const btVector3& position2, const btVector3& color);
63 
64  VectorOfMotionStates _changedMotionStates;
65  VectorOfMotionStates _deactivatedStates;
66  SetOfMotionStates _activeStates;
67  SetOfMotionStates _lastActiveStates;
68  int _numSubsteps { 0 };
69 };
70 
71 #endif // hifi_ThreadSafeDynamicsWorld_h