mirror of
https://github.com/OpenMW/openmw.git
synced 2025-06-23 16:11:33 +00:00
Merge pull request #1479 from akortunov/physics
Make physics framerate configurable
This commit is contained in:
commit
e5d1fd022f
2 changed files with 22 additions and 5 deletions
|
@ -1,5 +1,6 @@
|
||||||
#include "physicssystem.hpp"
|
#include "physicssystem.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
#include <osg/Group>
|
#include <osg/Group>
|
||||||
|
@ -683,6 +684,7 @@ namespace MWPhysics
|
||||||
, mWaterHeight(0)
|
, mWaterHeight(0)
|
||||||
, mWaterEnabled(false)
|
, mWaterEnabled(false)
|
||||||
, mParentNode(parentNode)
|
, mParentNode(parentNode)
|
||||||
|
, mPhysicsDt(1.f / 60.f)
|
||||||
{
|
{
|
||||||
mResourceSystem->addResourceManager(mShapeManager.get());
|
mResourceSystem->addResourceManager(mShapeManager.get());
|
||||||
|
|
||||||
|
@ -695,6 +697,20 @@ namespace MWPhysics
|
||||||
// Don't update AABBs of all objects every frame. Most objects in MW are static, so we don't need this.
|
// Don't update AABBs of all objects every frame. Most objects in MW are static, so we don't need this.
|
||||||
// Should a "static" object ever be moved, we have to update its AABB manually using DynamicsWorld::updateSingleAabb.
|
// Should a "static" object ever be moved, we have to update its AABB manually using DynamicsWorld::updateSingleAabb.
|
||||||
mCollisionWorld->setForceUpdateAllAabbs(false);
|
mCollisionWorld->setForceUpdateAllAabbs(false);
|
||||||
|
|
||||||
|
// Check if a user decided to override a physics system FPS
|
||||||
|
const char* env = getenv("OPENMW_PHYSICS_FPS");
|
||||||
|
if (env)
|
||||||
|
{
|
||||||
|
std::string str(env);
|
||||||
|
|
||||||
|
float physFramerate = std::atof(env);
|
||||||
|
if (physFramerate > 0)
|
||||||
|
{
|
||||||
|
mPhysicsDt = 1.f / physFramerate;
|
||||||
|
std::cerr << "Warning: physics framerate was overriden (a new value is " << physFramerate << ")." << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
PhysicsSystem::~PhysicsSystem()
|
PhysicsSystem::~PhysicsSystem()
|
||||||
|
@ -1357,13 +1373,12 @@ namespace MWPhysics
|
||||||
mMovementResults.clear();
|
mMovementResults.clear();
|
||||||
|
|
||||||
mTimeAccum += dt;
|
mTimeAccum += dt;
|
||||||
const float physicsDt = 1.f/60.0f;
|
|
||||||
|
|
||||||
const int maxAllowedSteps = 20;
|
const int maxAllowedSteps = 20;
|
||||||
int numSteps = mTimeAccum / (physicsDt);
|
int numSteps = mTimeAccum / (mPhysicsDt);
|
||||||
numSteps = std::min(numSteps, maxAllowedSteps);
|
numSteps = std::min(numSteps, maxAllowedSteps);
|
||||||
|
|
||||||
mTimeAccum -= numSteps * physicsDt;
|
mTimeAccum -= numSteps * mPhysicsDt;
|
||||||
|
|
||||||
if (numSteps)
|
if (numSteps)
|
||||||
{
|
{
|
||||||
|
@ -1412,7 +1427,7 @@ namespace MWPhysics
|
||||||
bool positionChanged = false;
|
bool positionChanged = false;
|
||||||
for (int i=0; i<numSteps; ++i)
|
for (int i=0; i<numSteps; ++i)
|
||||||
{
|
{
|
||||||
position = MovementSolver::move(position, physicActor->getPtr(), physicActor, iter->second, physicsDt,
|
position = MovementSolver::move(position, physicActor->getPtr(), physicActor, iter->second, mPhysicsDt,
|
||||||
flying, waterlevel, slowFall, mCollisionWorld, mStandingCollisions);
|
flying, waterlevel, slowFall, mCollisionWorld, mStandingCollisions);
|
||||||
if (position != physicActor->getPosition())
|
if (position != physicActor->getPosition())
|
||||||
positionChanged = true;
|
positionChanged = true;
|
||||||
|
@ -1421,7 +1436,7 @@ namespace MWPhysics
|
||||||
if (positionChanged)
|
if (positionChanged)
|
||||||
mCollisionWorld->updateSingleAabb(physicActor->getCollisionObject());
|
mCollisionWorld->updateSingleAabb(physicActor->getCollisionObject());
|
||||||
|
|
||||||
float interpolationFactor = mTimeAccum / physicsDt;
|
float interpolationFactor = mTimeAccum / mPhysicsDt;
|
||||||
osg::Vec3f interpolated = position * interpolationFactor + physicActor->getPreviousPosition() * (1.f - interpolationFactor);
|
osg::Vec3f interpolated = position * interpolationFactor + physicActor->getPreviousPosition() * (1.f - interpolationFactor);
|
||||||
|
|
||||||
float heightDiff = position.z() - oldHeight;
|
float heightDiff = position.z() - oldHeight;
|
||||||
|
|
|
@ -220,6 +220,8 @@ namespace MWPhysics
|
||||||
|
|
||||||
osg::ref_ptr<osg::Group> mParentNode;
|
osg::ref_ptr<osg::Group> mParentNode;
|
||||||
|
|
||||||
|
float mPhysicsDt;
|
||||||
|
|
||||||
PhysicsSystem (const PhysicsSystem&);
|
PhysicsSystem (const PhysicsSystem&);
|
||||||
PhysicsSystem& operator= (const PhysicsSystem&);
|
PhysicsSystem& operator= (const PhysicsSystem&);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue