FixedTimeStep configuration for variable hardware performance
0 reputation · 07 Nov 2020, 21:05 UTC
0 reputation · 07 Nov 2020, 21:05 UTC
The IsFixedTimeStep property in MonoGame ensures that the Update method is called at a consistent interval defined by TargetElapsedTime. This mechanism is designed to keep game logic speed independent of the hardware's rendering capabilities by allowing multiple updates per single Draw call.
When IsFixedTimeStep is enabled, the internal loop accumulates elapsed time to trigger updates. However, a performance bottleneck where the Update logic consistently exceeds the TargetElapsedTime can lead to a cumulative delay in the game loop.
There is uncertainty regarding the optimal configuration to prevent the "spiral of death" without sacrificing the deterministic physics benefits of a fixed time step.
TargetElapsedTime to balance physics stability and CPU overhead on low-end hardware?32775 reputation · 07 Nov 2020, 22:56 UTC
MonoGame’s default TargetElapsedTime is 1/60 s (≈16.67 ms). On machines that struggle to keep up with that cadence, you can safely double the interval to 1/30 s (≈33 ms). This keeps the physics integration stable while reducing the number of Update calls per second and thus the CPU load.
MonoGame exposes a MaxElapsedTime property (inherited from XNA). It limits the amount of accumulated time that the game loop will process in a single frame. When the accumulated time exceeds this value, the loop discards the excess and waits for the next Draw cycle. A common setting is 250 ms (0.25 s), which allows at least one full physics step per frame while preventing runaway update bursts.
public class Game1 : Game
{
public Game1()
{
// Enable a deterministic update cadence
this.IsFixedTimeStep = true;
// 30 Hz update rate for low‑end hardware
this.TargetElapsedTime = TimeSpan.FromSeconds(1.0 / 30.0);
// Cap the accumulated time to 250 ms
this.MaxElapsedTime = TimeSpan.FromMilliseconds(250);
}
protected override void Update(GameTime gameTime)
{
// Game logic here
base.Update(gameTime);
}
}
MonoGame does not provide a per‑frame update counter. If you require a strict maximum number of Update calls per frame, you will need to implement your own counter inside the Update method and break the loop when the threshold is reached. However, in most cases MaxElapsedTime is sufficient to avoid the spiral of death while preserving deterministic physics.
Try the configuration above on your target hardware and monitor the frame rate and physics responsiveness. If you observe that the physics feels sluggish, consider lowering TargetElapsedTime back toward 1/60 s or tightening MaxElapsedTime slightly. Conversely, if the game still stalls, increase MaxElapsedTime or profile the Update workload for bottlenecks.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.