Skip to content

Commit fc19e3b

Browse files
committed
Fixed the RestoreMaxPhysicsDT patch not being 100% reliable
The previous implementation was attempting to restore `Time.maximumDeltaTime` in a prefix, before stock was setting `Time.fixedDeltaTime`. Since the minimum value of `maximumDeltaTime` was clamped to the (not yet changed) `fixedDeltaTime` value, it was possible for a higher value than `GameSettings.PHYSICS_FRAME_DT_LIMIT` to end up being set. In fact, the previous implementation only worked because `fixedDeltaTime` changes are (usually but not always) lerped over multiple frames. The issue was notably visible when having `PHYSICS_FRAME_DT_LIMIT` set to 0.02, in which case the value was always set to the lerped value from the last frame. Implementing the proper logic in a postfix fixes that issue.
1 parent 4bc36bb commit fc19e3b

1 file changed

Lines changed: 27 additions & 12 deletions

File tree

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
using HarmonyLib;
1+
//#define DEBUG_PHYSICS_DT
2+
23
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
74
using UnityEngine;
85

96
namespace KSPCommunityFixes.BugFixes
@@ -14,16 +11,34 @@ public class RestoreMaxPhysicsDT : BasePatch
1411

1512
protected override void ApplyPatches()
1613
{
17-
AddPatch(PatchType.Prefix, typeof(TimeWarp), "updateRate");
14+
AddPatch(PatchType.Postfix, typeof(TimeWarp), nameof(TimeWarp.updateRate));
15+
}
16+
17+
// when you use physics warp, it increases Time.fixedDeltaTime
18+
// Unity will internally increase maximumDeltaTime to be at least as high as fixedDeltaTime
19+
// But nothing in the KSP code will ever return maximumDeltaTime to the value from the settings when time warp is over
20+
static void TimeWarp_updateRate_Postfix()
21+
{
22+
float fixedDT = Time.fixedDeltaTime;
23+
if (Time.maximumDeltaTime > fixedDT)
24+
{
25+
if (fixedDT > GameSettings.PHYSICS_FRAME_DT_LIMIT)
26+
Time.maximumDeltaTime = fixedDT;
27+
else
28+
Time.maximumDeltaTime = GameSettings.PHYSICS_FRAME_DT_LIMIT;
29+
}
1830
}
31+
}
1932

20-
static void TimeWarp_updateRate_Prefix()
33+
#if DEBUG_PHYSICS_DT
34+
[KSPAddon(KSPAddon.Startup.Flight, false)]
35+
public class PhysicsDTDisplay : MonoBehaviour
36+
{
37+
void OnGUI()
2138
{
22-
// when you use physics warp, it increases Time.fixedDeltaTime
23-
// Unity will internally increase maximumDeltaTime to be at least as high as fixedDeltaTime
24-
// But nothing in the KSP code will ever return maximumDeltaTime to the value from the settings when time warp is over
25-
// Setting it just before TimeWarp sets fixedDeltaTime guarantees that all points of code will have the correct value
26-
Time.maximumDeltaTime = GameSettings.PHYSICS_FRAME_DT_LIMIT;
39+
GUI.Label(new Rect(0, 100, 200, 20), $"DT : {Time.fixedDeltaTime}");
40+
GUI.Label(new Rect(0, 120, 200, 20), $"MaxDT : {Time.maximumDeltaTime}");
2741
}
2842
}
43+
#endif
2944
}

0 commit comments

Comments
 (0)