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:
@@ -94,7 +94,7 @@ bool Time::TickData::OnTickBegin(double time, float targetFps, float maxDeltaTim
|
||||
|
||||
if (targetFps > ZeroTolerance)
|
||||
{
|
||||
int skip = (int)(1 + (time - NextBegin) * targetFps);
|
||||
const int skip = (int)(1 + (time - NextBegin) * targetFps);
|
||||
NextBegin += (1.0 / targetFps) * skip;
|
||||
}
|
||||
}
|
||||
@@ -132,14 +132,12 @@ bool Time::FixedStepTickData::OnTickBegin(double time, float targetFps, float ma
|
||||
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 +147,10 @@ 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;
|
||||
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);
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user