Always run fixed update ticks at fixed deltatime

The random variance in fixed updates makes it impossible to do anything deterministic during physics ticks.
This commit is contained in:
2022-03-19 23:27:25 +02:00
committed by Ari Vuollet
parent 83c3201ef8
commit 43801d0824
2 changed files with 7 additions and 23 deletions

View File

@@ -94,7 +94,8 @@ bool Time::TickData::OnTickBegin(double time, float targetFps, float maxDeltaTim
if (targetFps > ZeroTolerance)
{
int skip = (int)(1 + (time - NextBegin) * targetFps);
//int skip = (int)(1 + (time - NextBegin) * targetFps);
const int skip = (int)(1 + (time - NextBegin) / (1.0 / targetFps));
NextBegin += (1.0 / targetFps) * skip;
}
}
@@ -128,18 +129,16 @@ void Time::TickData::Advance(double time, double deltaTime)
bool Time::FixedStepTickData::OnTickBegin(double time, float targetFps, float maxDeltaTime)
{
// Check if can perform a tick
double deltaTime, minDeltaTime;
double deltaTime;
if (FixedDeltaTimeEnable)
{
deltaTime = (double)FixedDeltaTimeValue;
minDeltaTime = deltaTime;
}
else
{
if (time < NextBegin)
return false;
minDeltaTime = targetFps > ZeroTolerance ? 1.0 / targetFps : 0.0;
deltaTime = Math::Max((time - LastBegin), 0.0);
if (deltaTime > maxDeltaTime)
{
@@ -149,21 +148,12 @@ bool Time::FixedStepTickData::OnTickBegin(double time, float targetFps, float ma
if (targetFps > ZeroTolerance)
{
int skip = (int)(1 + (time - NextBegin) * targetFps);
NextBegin += (1.0 / targetFps) * skip;
//int skip = (int)(1 + (time - NextBegin) * targetFps);
//NextBegin += (1.0 / targetFps) * skip;
deltaTime = 1.0 / targetFps;
NextBegin += 1.0 / targetFps;
}
}
Samples.Add(deltaTime);
// Check if last few ticks were not taking too long so it's running slowly
const bool isRunningSlowly = Samples.Average() > 1.5 * minDeltaTime;
if (!isRunningSlowly)
{
// Make steps fixed size
const double diff = deltaTime - minDeltaTime;
time -= diff;
deltaTime = minDeltaTime;
}
// Update data
Advance(time, deltaTime);

View File

@@ -5,7 +5,6 @@
#include "Engine/Core/Types/TimeSpan.h"
#include "Engine/Core/Types/DateTime.h"
#include "Engine/Scripting/ScriptingType.h"
#include "Engine/Core/Collections/SamplesBuffer.h"
/// <summary>
/// Game ticking and timing system.
@@ -89,12 +88,7 @@ public:
class FixedStepTickData : public TickData
{
public:
/// <summary>
/// The last few ticks delta times. Used to check if can use fixed steps or whenever is running slowly so should use normal stepping.
/// </summary>
SamplesBuffer<double, 4> Samples;
public:
// [TickData]
bool OnTickBegin(double time, float targetFps, float maxDeltaTime) override;
};