1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-09-15 15:02:34 +02:00

Interpolating dynamic objects

How?
Calling stepsimulation each frame,
bullet interpolate vehicles and characters for us.

Removed unneeded code:
"float alpha = fmod(dt, GAME_TIMESTEP) / GAME_TIMESTEP;"
It looks like alpha shift is

I also removed unneeded variable clock
and refactored names to match bullet's example.

We should alse think about problem of
crossing the range of float.
This commit is contained in:
Filip Gawin 2017-09-02 17:51:06 +02:00 committed by Daniel Evans
parent c5ade22377
commit bc8652baba
2 changed files with 21 additions and 27 deletions

View File

@ -26,6 +26,11 @@ std::map<GameRenderer::SpecialModel, std::string> kSpecialModels = {
{GameRenderer::ZoneCylinderB, "zonecylb.dff"}, {GameRenderer::ZoneCylinderB, "zonecylb.dff"},
{GameRenderer::Arrow, "arrow.dff"}}; {GameRenderer::Arrow, "arrow.dff"}};
namespace {
constexpr float kPhysicsTimeStep = 1.0f/30.0f;
constexpr float kMaxPhysicsSubSteps = 4;
}
#define MOUSE_SENSITIVITY_SCALE 2.5f #define MOUSE_SENSITIVITY_SCALE 2.5f
RWGame::RWGame(Logger& log, int argc, char* argv[]) RWGame::RWGame(Logger& log, int argc, char* argv[])
@ -353,7 +358,9 @@ void RWGame::handleCheatInput(char symbol) {
} }
int RWGame::run() { int RWGame::run() {
last_clock_time = clock.now(); namespace chrono = std::chrono;
auto lastFrame = chrono::steady_clock::now();
float accumulatedTime = 0.0f;
// Loop until we run out of states. // Loop until we run out of states.
bool running = true; bool running = true;
@ -400,45 +407,37 @@ int RWGame::run() {
} }
RW_PROFILE_END(); RW_PROFILE_END();
auto now = clock.now(); auto now = chrono::steady_clock::now();
float timer = auto deltaTime = chrono::duration<float>(now - lastFrame).count();
std::chrono::duration<float>(now - last_clock_time).count(); lastFrame = now;
last_clock_time = now; accumulatedTime += deltaTime;
accum += timer * timescale;
if(!world->isPaused()) {
world->dynamicsWorld->stepSimulation(deltaTime * timescale, kMaxPhysicsSubSteps, kPhysicsTimeStep);
}
RW_PROFILE_BEGIN("Update"); RW_PROFILE_BEGIN("Update");
if (accum >= GAME_TIMESTEP) { while (accumulatedTime >= GAME_TIMESTEP && !world->isPaused()) {
if (!StateManager::currentState()) { if (!StateManager::currentState()) {
break; break;
} }
accumulatedTime -= GAME_TIMESTEP;
RW_PROFILE_BEGIN("state"); RW_PROFILE_BEGIN("state");
StateManager::get().tick(GAME_TIMESTEP); StateManager::get().tick(GAME_TIMESTEP * timescale);
RW_PROFILE_END(); RW_PROFILE_END();
RW_PROFILE_BEGIN("engine"); RW_PROFILE_BEGIN("engine");
tick(GAME_TIMESTEP); tick(GAME_TIMESTEP * timescale);
RW_PROFILE_END(); RW_PROFILE_END();
getState()->swapInputState(); getState()->swapInputState();
accum -= GAME_TIMESTEP;
// Throw away time if the accumulator reaches too high.
if (accum > GAME_TIMESTEP * 5.f) {
accum = 0.f;
}
} }
RW_PROFILE_END(); RW_PROFILE_END();
float alpha = fmod(accum, GAME_TIMESTEP) / GAME_TIMESTEP;
if (!StateManager::currentState()->shouldWorldUpdate()) {
alpha = 1.f;
}
RW_PROFILE_BEGIN("Render"); RW_PROFILE_BEGIN("Render");
RW_PROFILE_BEGIN("engine"); RW_PROFILE_BEGIN("engine");
render(alpha, timer); render(1, deltaTime);
RW_PROFILE_END(); RW_PROFILE_END();
RW_PROFILE_BEGIN("state"); RW_PROFILE_BEGIN("state");
@ -508,7 +507,6 @@ void RWGame::tick(float dt) {
state.text.tick(dt); state.text.tick(dt);
world->dynamicsWorld->stepSimulation(dt, 2, dt);
if (vm) { if (vm) {
try { try {

View File

@ -27,9 +27,6 @@ class RWGame : public GameBase {
std::unique_ptr<ScriptMachine> vm; std::unique_ptr<ScriptMachine> vm;
std::unique_ptr<SCMFile> script; std::unique_ptr<SCMFile> script;
std::chrono::steady_clock clock;
std::chrono::steady_clock::time_point last_clock_time;
bool inFocus = true; bool inFocus = true;
ViewCamera currentCam; ViewCamera currentCam;
@ -46,7 +43,6 @@ class RWGame : public GameBase {
std::string cheatInputWindow = std::string(32, ' '); std::string cheatInputWindow = std::string(32, ' ');
float accum = 0.f;
float timescale = 1.f; float timescale = 1.f;
public: public: