From bc8652baba49c091149f5a752f5f1c227615adbc Mon Sep 17 00:00:00 2001 From: Filip Gawin Date: Sat, 2 Sep 2017 17:51:06 +0200 Subject: [PATCH] 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. --- rwgame/RWGame.cpp | 44 +++++++++++++++++++++----------------------- rwgame/RWGame.hpp | 4 ---- 2 files changed, 21 insertions(+), 27 deletions(-) diff --git a/rwgame/RWGame.cpp b/rwgame/RWGame.cpp index e38865ea..1ecd4f35 100644 --- a/rwgame/RWGame.cpp +++ b/rwgame/RWGame.cpp @@ -26,6 +26,11 @@ std::map kSpecialModels = { {GameRenderer::ZoneCylinderB, "zonecylb.dff"}, {GameRenderer::Arrow, "arrow.dff"}}; +namespace { + constexpr float kPhysicsTimeStep = 1.0f/30.0f; + constexpr float kMaxPhysicsSubSteps = 4; +} + #define MOUSE_SENSITIVITY_SCALE 2.5f RWGame::RWGame(Logger& log, int argc, char* argv[]) @@ -353,7 +358,9 @@ void RWGame::handleCheatInput(char symbol) { } 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. bool running = true; @@ -400,45 +407,37 @@ int RWGame::run() { } RW_PROFILE_END(); - auto now = clock.now(); - float timer = - std::chrono::duration(now - last_clock_time).count(); - last_clock_time = now; - accum += timer * timescale; + auto now = chrono::steady_clock::now(); + auto deltaTime = chrono::duration(now - lastFrame).count(); + lastFrame = now; + accumulatedTime += deltaTime; + + if(!world->isPaused()) { + world->dynamicsWorld->stepSimulation(deltaTime * timescale, kMaxPhysicsSubSteps, kPhysicsTimeStep); + } RW_PROFILE_BEGIN("Update"); - if (accum >= GAME_TIMESTEP) { + while (accumulatedTime >= GAME_TIMESTEP && !world->isPaused()) { if (!StateManager::currentState()) { break; } + accumulatedTime -= GAME_TIMESTEP; RW_PROFILE_BEGIN("state"); - StateManager::get().tick(GAME_TIMESTEP); + StateManager::get().tick(GAME_TIMESTEP * timescale); RW_PROFILE_END(); RW_PROFILE_BEGIN("engine"); - tick(GAME_TIMESTEP); + tick(GAME_TIMESTEP * timescale); RW_PROFILE_END(); 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(); - float alpha = fmod(accum, GAME_TIMESTEP) / GAME_TIMESTEP; - if (!StateManager::currentState()->shouldWorldUpdate()) { - alpha = 1.f; - } - RW_PROFILE_BEGIN("Render"); RW_PROFILE_BEGIN("engine"); - render(alpha, timer); + render(1, deltaTime); RW_PROFILE_END(); RW_PROFILE_BEGIN("state"); @@ -508,7 +507,6 @@ void RWGame::tick(float dt) { state.text.tick(dt); - world->dynamicsWorld->stepSimulation(dt, 2, dt); if (vm) { try { diff --git a/rwgame/RWGame.hpp b/rwgame/RWGame.hpp index 4c6120b1..959ca5ea 100644 --- a/rwgame/RWGame.hpp +++ b/rwgame/RWGame.hpp @@ -27,9 +27,6 @@ class RWGame : public GameBase { std::unique_ptr vm; std::unique_ptr script; - std::chrono::steady_clock clock; - std::chrono::steady_clock::time_point last_clock_time; - bool inFocus = true; ViewCamera currentCam; @@ -46,7 +43,6 @@ class RWGame : public GameBase { std::string cheatInputWindow = std::string(32, ' '); - float accum = 0.f; float timescale = 1.f; public: