Vixen
02b45cc4
csharp
public sealed class FixedStepAccumulator

Turns a variable frame delta into a whole number of fixed simulation steps.

No guide page documents this yet — the page shows what the code says about itself.

Remarks

Simulation that is a function of the frame delta is not reproducible, is not stable at low frame rates, and cannot be replayed — three properties physics, netcode and the determinism tests all need. So the simulation advances in fixed steps and the renderer interpolates between the last two, which is what Alpha is for.

The catch-up clamp is the part that matters. A frame that took a second — a breakpoint, a shader compile, a laptop lid — owes sixty steps. Running all of them makes the next frame take a second too, which owes sixty more: the spiral of death. Clamping discards the debt instead, so the simulation runs slow for one frame and then recovers. Time that is dropped is dropped visibly, via DroppedSteps, rather than quietly.

Counted in ticks, not in seconds. A TimeSpan is exact in ticks and approximate in double seconds, and the difference shows up at once: a hundred milliseconds against a sixtieth-of-a-second step divides to 5.999… in floating point and owes five steps instead of six. Integer division of ticks owes six, and a loop fed the same delta for an hour banks exactly the remainder it should rather than drifting.

Fields and properties (6)

  • public TimeSpan Step

    How long one step is. Sixty a second by default.

  • public int MaxStepsPerFrame

    The most steps one frame may run before the rest of the debt is discarded.

  • public TimeSpan Pending

    How much time is banked and not yet stepped.

  • public float Alpha

    How far the frame is between the last completed step and the next, from 0 to 1 — what a renderer interpolates with so a 60 Hz simulation looks smooth at 144 Hz.

  • public long DroppedSteps

    How many steps have been discarded to the clamp since the accumulator was made.

  • public long TotalSteps

    How many steps have been run since the accumulator was made.

Methods (4)

  • public FixedStepAccumulator(TimeSpan step = default(TimeSpan), int maxStepsPerFrame = 5)

    Creates an accumulator.

  • public int Advance(TimeSpan elapsed)

    Banks a frame's elapsed time and reports how many steps to run.

  • public GameTime StepTime(GameTime frame)

    The clock one fixed step sees.

  • public void Reset()

    Throws away the banked time, without counting it as dropped.

Used by (5)

  • EngineLoopVixen.Engine
  • EngineLoopTestsVixen.Engine.Tests
  • PhysicsInterpolationSystemVixen.Physics
  • PhysicsSceneTestsVixen.Physics.Tests
  • HostedEngineTestsVixen.App.Tests