mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-06-26 14:50:44 +02:00
Merge pull request #12946 from AndrewMeadows/minor-physics-cleanup
minor cleanup in physics simulation
This commit is contained in:
commit
38916a3023
6 changed files with 29 additions and 53 deletions
|
@ -252,11 +252,9 @@ void EntityMotionState::getWorldTransform(btTransform& worldTrans) const {
|
||||||
|
|
||||||
uint32_t thisStep = ObjectMotionState::getWorldSimulationStep();
|
uint32_t thisStep = ObjectMotionState::getWorldSimulationStep();
|
||||||
float dt = (thisStep - _lastKinematicStep) * PHYSICS_ENGINE_FIXED_SUBSTEP;
|
float dt = (thisStep - _lastKinematicStep) * PHYSICS_ENGINE_FIXED_SUBSTEP;
|
||||||
|
_lastKinematicStep = thisStep;
|
||||||
_entity->stepKinematicMotion(dt);
|
_entity->stepKinematicMotion(dt);
|
||||||
|
|
||||||
// bypass const-ness so we can remember the step
|
|
||||||
const_cast<EntityMotionState*>(this)->_lastKinematicStep = thisStep;
|
|
||||||
|
|
||||||
// and set the acceleration-matches-gravity count high so that if we send an update
|
// and set the acceleration-matches-gravity count high so that if we send an update
|
||||||
// it will use the correct acceleration for remote simulations
|
// it will use the correct acceleration for remote simulations
|
||||||
_accelerationNearlyGravityCount = (uint8_t)(-1);
|
_accelerationNearlyGravityCount = (uint8_t)(-1);
|
||||||
|
|
|
@ -184,7 +184,7 @@ protected:
|
||||||
btRigidBody* _body { nullptr };
|
btRigidBody* _body { nullptr };
|
||||||
float _density { 1.0f };
|
float _density { 1.0f };
|
||||||
|
|
||||||
uint32_t _lastKinematicStep;
|
mutable uint32_t _lastKinematicStep;
|
||||||
bool _hasInternalKinematicChanges { false };
|
bool _hasInternalKinematicChanges { false };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -71,8 +71,8 @@ void PhysicsEngine::init() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t PhysicsEngine::getNumSubsteps() {
|
uint32_t PhysicsEngine::getNumSubsteps() const {
|
||||||
return _numSubsteps;
|
return _dynamicsWorld->getNumSubsteps();
|
||||||
}
|
}
|
||||||
|
|
||||||
// private
|
// private
|
||||||
|
@ -329,13 +329,9 @@ void PhysicsEngine::stepSimulation() {
|
||||||
PHYSICS_ENGINE_FIXED_SUBSTEP, onSubStep);
|
PHYSICS_ENGINE_FIXED_SUBSTEP, onSubStep);
|
||||||
if (numSubsteps > 0) {
|
if (numSubsteps > 0) {
|
||||||
BT_PROFILE("postSimulation");
|
BT_PROFILE("postSimulation");
|
||||||
_numSubsteps += (uint32_t)numSubsteps;
|
|
||||||
ObjectMotionState::setWorldSimulationStep(_numSubsteps);
|
|
||||||
|
|
||||||
if (_myAvatarController) {
|
if (_myAvatarController) {
|
||||||
_myAvatarController->postSimulation();
|
_myAvatarController->postSimulation();
|
||||||
}
|
}
|
||||||
|
|
||||||
_hasOutgoingChanges = true;
|
_hasOutgoingChanges = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -52,7 +52,7 @@ public:
|
||||||
~PhysicsEngine();
|
~PhysicsEngine();
|
||||||
void init();
|
void init();
|
||||||
|
|
||||||
uint32_t getNumSubsteps();
|
uint32_t getNumSubsteps() const;
|
||||||
|
|
||||||
void removeObjects(const VectorOfMotionStates& objects);
|
void removeObjects(const VectorOfMotionStates& objects);
|
||||||
void removeSetOfObjects(const SetOfMotionStates& objects); // only called during teardown
|
void removeSetOfObjects(const SetOfMotionStates& objects); // only called during teardown
|
||||||
|
@ -135,7 +135,6 @@ private:
|
||||||
CharacterController* _myAvatarController;
|
CharacterController* _myAvatarController;
|
||||||
|
|
||||||
uint32_t _numContactFrames = 0;
|
uint32_t _numContactFrames = 0;
|
||||||
uint32_t _numSubsteps;
|
|
||||||
|
|
||||||
bool _dumpNextStats { false };
|
bool _dumpNextStats { false };
|
||||||
bool _saveNextStats { false };
|
bool _saveNextStats { false };
|
||||||
|
|
|
@ -59,14 +59,11 @@ int ThreadSafeDynamicsWorld::stepSimulationWithSubstepCallback(btScalar timeStep
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*//process some debugging flags
|
|
||||||
if (getDebugDrawer()) {
|
|
||||||
btIDebugDraw* debugDrawer = getDebugDrawer();
|
|
||||||
gDisableDeactivation = (debugDrawer->getDebugMode() & btIDebugDraw::DBG_NoDeactivation) != 0;
|
|
||||||
}*/
|
|
||||||
if (subSteps) {
|
if (subSteps) {
|
||||||
//clamp the number of substeps, to prevent simulation grinding spiralling down to a halt
|
//clamp the number of substeps, to prevent simulation grinding spiralling down to a halt
|
||||||
int clampedSimulationSteps = (subSteps > maxSubSteps)? maxSubSteps : subSteps;
|
int clampedSimulationSteps = (subSteps > maxSubSteps)? maxSubSteps : subSteps;
|
||||||
|
_numSubsteps += clampedSimulationSteps;
|
||||||
|
ObjectMotionState::setWorldSimulationStep(_numSubsteps);
|
||||||
|
|
||||||
saveKinematicState(fixedTimeStep*clampedSimulationSteps);
|
saveKinematicState(fixedTimeStep*clampedSimulationSteps);
|
||||||
|
|
||||||
|
@ -98,28 +95,24 @@ int ThreadSafeDynamicsWorld::stepSimulationWithSubstepCallback(btScalar timeStep
|
||||||
// call this instead of non-virtual btDiscreteDynamicsWorld::synchronizeSingleMotionState()
|
// call this instead of non-virtual btDiscreteDynamicsWorld::synchronizeSingleMotionState()
|
||||||
void ThreadSafeDynamicsWorld::synchronizeMotionState(btRigidBody* body) {
|
void ThreadSafeDynamicsWorld::synchronizeMotionState(btRigidBody* body) {
|
||||||
btAssert(body);
|
btAssert(body);
|
||||||
if (body->getMotionState() && !body->isStaticObject()) {
|
btAssert(body->getMotionState());
|
||||||
//we need to call the update at least once, even for sleeping objects
|
|
||||||
//otherwise the 'graphics' transform never updates properly
|
if (body->isKinematicObject()) {
|
||||||
///@todo: add 'dirty' flag
|
ObjectMotionState* objectMotionState = static_cast<ObjectMotionState*>(body->getMotionState());
|
||||||
//if (body->getActivationState() != ISLAND_SLEEPING)
|
if (objectMotionState->hasInternalKinematicChanges()) {
|
||||||
{
|
// this is a special case where the kinematic motion has been updated by an Action
|
||||||
if (body->isKinematicObject()) {
|
// so we supply the body's current transform to the MotionState
|
||||||
ObjectMotionState* objectMotionState = static_cast<ObjectMotionState*>(body->getMotionState());
|
objectMotionState->clearInternalKinematicChanges();
|
||||||
if (objectMotionState->hasInternalKinematicChanges()) {
|
body->getMotionState()->setWorldTransform(body->getWorldTransform());
|
||||||
objectMotionState->clearInternalKinematicChanges();
|
|
||||||
body->getMotionState()->setWorldTransform(body->getWorldTransform());
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
btTransform interpolatedTransform;
|
|
||||||
btTransformUtil::integrateTransform(body->getInterpolationWorldTransform(),
|
|
||||||
body->getInterpolationLinearVelocity(),body->getInterpolationAngularVelocity(),
|
|
||||||
(m_latencyMotionStateInterpolation && m_fixedTimeStep) ? m_localTime - m_fixedTimeStep : m_localTime*body->getHitFraction(),
|
|
||||||
interpolatedTransform);
|
|
||||||
body->getMotionState()->setWorldTransform(interpolatedTransform);
|
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
btTransform interpolatedTransform;
|
||||||
|
btTransformUtil::integrateTransform(body->getInterpolationWorldTransform(),
|
||||||
|
body->getInterpolationLinearVelocity(),body->getInterpolationAngularVelocity(),
|
||||||
|
(m_latencyMotionStateInterpolation && m_fixedTimeStep) ? m_localTime - m_fixedTimeStep : m_localTime*body->getHitFraction(),
|
||||||
|
interpolatedTransform);
|
||||||
|
body->getMotionState()->setWorldTransform(interpolatedTransform);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ThreadSafeDynamicsWorld::synchronizeMotionStates() {
|
void ThreadSafeDynamicsWorld::synchronizeMotionStates() {
|
||||||
|
@ -164,24 +157,12 @@ void ThreadSafeDynamicsWorld::synchronizeMotionStates() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void ThreadSafeDynamicsWorld::saveKinematicState(btScalar timeStep) {
|
void ThreadSafeDynamicsWorld::saveKinematicState(btScalar timeStep) {
|
||||||
///would like to iterate over m_nonStaticRigidBodies, but unfortunately old API allows
|
|
||||||
///to switch status _after_ adding kinematic objects to the world
|
|
||||||
///fix it for Bullet 3.x release
|
|
||||||
DETAILED_PROFILE_RANGE(simulation_physics, "saveKinematicState");
|
DETAILED_PROFILE_RANGE(simulation_physics, "saveKinematicState");
|
||||||
BT_PROFILE("saveKinematicState");
|
BT_PROFILE("saveKinematicState");
|
||||||
for (int i=0;i<m_collisionObjects.size();i++)
|
for (int i=0;i<m_nonStaticRigidBodies.size();i++) {
|
||||||
{
|
btRigidBody* body = m_nonStaticRigidBodies[i];
|
||||||
btCollisionObject* colObj = m_collisionObjects[i];
|
if (body && body->isKinematicObject() && body->getActivationState() != ISLAND_SLEEPING) {
|
||||||
btRigidBody* body = btRigidBody::upcast(colObj);
|
body->saveKinematicState(timeStep);
|
||||||
if (body && body->getActivationState() != ISLAND_SLEEPING)
|
|
||||||
{
|
|
||||||
if (body->isKinematicObject())
|
|
||||||
{
|
|
||||||
//to calculate velocities next frame
|
|
||||||
body->saveKinematicState(timeStep);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -37,6 +37,7 @@ public:
|
||||||
btConstraintSolver* constraintSolver,
|
btConstraintSolver* constraintSolver,
|
||||||
btCollisionConfiguration* collisionConfiguration);
|
btCollisionConfiguration* collisionConfiguration);
|
||||||
|
|
||||||
|
int getNumSubsteps() const { return _numSubsteps; }
|
||||||
int stepSimulationWithSubstepCallback(btScalar timeStep, int maxSubSteps = 1,
|
int stepSimulationWithSubstepCallback(btScalar timeStep, int maxSubSteps = 1,
|
||||||
btScalar fixedTimeStep = btScalar(1.)/btScalar(60.),
|
btScalar fixedTimeStep = btScalar(1.)/btScalar(60.),
|
||||||
SubStepCallback onSubStep = []() { });
|
SubStepCallback onSubStep = []() { });
|
||||||
|
@ -61,6 +62,7 @@ private:
|
||||||
VectorOfMotionStates _deactivatedStates;
|
VectorOfMotionStates _deactivatedStates;
|
||||||
SetOfMotionStates _activeStates;
|
SetOfMotionStates _activeStates;
|
||||||
SetOfMotionStates _lastActiveStates;
|
SetOfMotionStates _lastActiveStates;
|
||||||
|
int _numSubsteps { 0 };
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // hifi_ThreadSafeDynamicsWorld_h
|
#endif // hifi_ThreadSafeDynamicsWorld_h
|
||||||
|
|
Loading…
Reference in a new issue