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,8 @@ bool Time::TickData::OnTickBegin(double time, float targetFps, float maxDeltaTim
|
|||||||
|
|
||||||
if (targetFps > ZeroTolerance)
|
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;
|
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)
|
bool Time::FixedStepTickData::OnTickBegin(double time, float targetFps, float maxDeltaTime)
|
||||||
{
|
{
|
||||||
// Check if can perform a tick
|
// Check if can perform a tick
|
||||||
double deltaTime, minDeltaTime;
|
double deltaTime;
|
||||||
if (FixedDeltaTimeEnable)
|
if (FixedDeltaTimeEnable)
|
||||||
{
|
{
|
||||||
deltaTime = (double)FixedDeltaTimeValue;
|
deltaTime = (double)FixedDeltaTimeValue;
|
||||||
minDeltaTime = deltaTime;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (time < NextBegin)
|
if (time < NextBegin)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
minDeltaTime = targetFps > ZeroTolerance ? 1.0 / targetFps : 0.0;
|
|
||||||
deltaTime = Math::Max((time - LastBegin), 0.0);
|
deltaTime = Math::Max((time - LastBegin), 0.0);
|
||||||
if (deltaTime > maxDeltaTime)
|
if (deltaTime > maxDeltaTime)
|
||||||
{
|
{
|
||||||
@@ -149,21 +148,12 @@ bool Time::FixedStepTickData::OnTickBegin(double time, float targetFps, float ma
|
|||||||
|
|
||||||
if (targetFps > ZeroTolerance)
|
if (targetFps > ZeroTolerance)
|
||||||
{
|
{
|
||||||
int skip = (int)(1 + (time - NextBegin) * targetFps);
|
//int skip = (int)(1 + (time - NextBegin) * targetFps);
|
||||||
NextBegin += (1.0 / targetFps) * skip;
|
//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
|
// Update data
|
||||||
Advance(time, deltaTime);
|
Advance(time, deltaTime);
|
||||||
|
|||||||
@@ -5,7 +5,6 @@
|
|||||||
#include "Engine/Core/Types/TimeSpan.h"
|
#include "Engine/Core/Types/TimeSpan.h"
|
||||||
#include "Engine/Core/Types/DateTime.h"
|
#include "Engine/Core/Types/DateTime.h"
|
||||||
#include "Engine/Scripting/ScriptingType.h"
|
#include "Engine/Scripting/ScriptingType.h"
|
||||||
#include "Engine/Core/Collections/SamplesBuffer.h"
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Game ticking and timing system.
|
/// Game ticking and timing system.
|
||||||
@@ -89,12 +88,7 @@ public:
|
|||||||
class FixedStepTickData : public TickData
|
class FixedStepTickData : public TickData
|
||||||
{
|
{
|
||||||
public:
|
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]
|
// [TickData]
|
||||||
bool OnTickBegin(double time, float targetFps, float maxDeltaTime) override;
|
bool OnTickBegin(double time, float targetFps, float maxDeltaTime) override;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user