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:
parent
c5ade22377
commit
bc8652baba
@ -26,6 +26,11 @@ std::map<GameRenderer::SpecialModel, std::string> 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<float>(now - last_clock_time).count();
|
||||
last_clock_time = now;
|
||||
accum += timer * timescale;
|
||||
auto now = chrono::steady_clock::now();
|
||||
auto deltaTime = chrono::duration<float>(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 {
|
||||
|
@ -27,9 +27,6 @@ class RWGame : public GameBase {
|
||||
std::unique_ptr<ScriptMachine> vm;
|
||||
std::unique_ptr<SCMFile> 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:
|
||||
|
Loading…
Reference in New Issue
Block a user