Fixed timestep
Run simulation at a steady rate with FixedUpdate and Time<Fixed>, handle input correctly around fixed steps, and interpolate rendering so movement stays smooth.
On this page
Systems in Update run once per frame, and frames vary in length. Physics and other simulation behave better with steps of the same size every time. The FixedUpdate schedule gives you that: it runs at a fixed rate, 64 times a second by default, however fast or slow the frame rate is.
Add a fixed system#
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(FixedUpdate, apply_velocity)
.run();
}
fn apply_velocity(time: Res<Time>, mut q: Query<(&mut Transform, &Velocity)>) {
for (mut transform, velocity) in &mut q {
// inside FixedUpdate, Time is the fixed clock: delta is always one step
transform.translation += velocity.0 * time.delta_secs();
}
}Inside FixedUpdate, Res<Time> is the fixed clock, so delta_secs() is always the step size. The same system works in either schedule.
How it runs#
Every frame, the elapsed virtual time is added to an accumulator. FixedUpdate then runs once for each whole step in the accumulator, between PreUpdate and Update:
| Frame time | FixedUpdate runs, at 64 Hz (15.6 ms steps) |
|---|---|
| 8 ms (120 fps) | 0 or 1 times |
| 16.7 ms (60 fps) | Usually once, sometimes twice |
| 33 ms (30 fps) | About twice |
What's left over is the overstep. Because the fixed clock follows virtual time, pausing virtual time stops FixedUpdate, and slow motion slows it down. See Time.
The fixed loop has its own schedules, run in order each step: FixedFirst, FixedPreUpdate, FixedUpdate, FixedPostUpdate and FixedLast.
Change the rate#
app.insert_resource(Time::<Fixed>::from_hz(60.0));
// or Time::<Fixed>::from_seconds(0.5), Time::<Fixed>::from_duration(d)Method on Time<Fixed> | What it does |
|---|---|
timestep(), set_timestep(d), set_timestep_hz(hz), set_timestep_seconds(s) | The step size. A change applies from the next step. |
overstep() | Time accumulated toward the next step. |
overstep_fraction() | The overstep as a fraction of a step, from 0 to 1. Use it to interpolate. |
The default of 64 Hz is chosen on purpose: 60 Hz can alternate between zero and two steps a frame on a 60 Hz display, and a power of two converts exactly to f32.
Input and fixed steps#
Input is read once per frame, in PreUpdate, but FixedUpdate may run zero or several times in a frame. So:
just_pressedinFixedUpdatecan be missed (on a frame with no step) or seen twice (on a frame with two).- Read input every frame and store it, then consume it in the fixed step.
The RunFixedMainLoop schedule has sets for exactly this. Systems in RunFixedMainLoopSystems::BeforeFixedMainLoop run every frame before the fixed steps; systems in AfterFixedMainLoop run every frame after them.
App::new()
.add_plugins(DefaultPlugins)
.add_systems(
RunFixedMainLoop,
(
accumulate_input.in_set(RunFixedMainLoopSystems::BeforeFixedMainLoop),
interpolate_rendered_transform.in_set(RunFixedMainLoopSystems::AfterFixedMainLoop),
),
)
.add_systems(FixedUpdate, advance_physics)
.run();Messages work across the boundary: messages written in Update are kept until fixed systems have had a chance to read them.
Smooth rendering#
If you move a Transform only in fixed steps, an object can move twice in one frame and not at all in the next, which looks like stutter. Keep the simulated position in its own component, and set the Transform every frame by interpolating between the previous and current step:
#[derive(Component, Default)]
struct PhysicalTranslation(Vec3);
#[derive(Component, Default)]
struct PreviousPhysicalTranslation(Vec3);
fn advance_physics(
fixed_time: Res<Time<Fixed>>,
mut q: Query<(&mut PhysicalTranslation, &mut PreviousPhysicalTranslation, &Velocity)>,
) {
for (mut current, mut previous, velocity) in &mut q {
previous.0 = current.0;
current.0 += velocity.0 * fixed_time.delta_secs();
}
}
fn interpolate_rendered_transform(
fixed_time: Res<Time<Fixed>>,
mut q: Query<(&mut Transform, &PhysicalTranslation, &PreviousPhysicalTranslation)>,
) {
let alpha = fixed_time.overstep_fraction();
for (mut transform, current, previous) in &mut q {
transform.translation = previous.0.lerp(current.0, alpha);
}
}What you see is up to one step behind the simulation, in return for smooth motion. Cameras that follow the player should also update every frame, in AfterFixedMainLoop, from the interpolated position.
Example: cargo run --example physics_in_fixed_timestep (and fixed_timestep for the basics).
Testing#
TimeUpdateStrategy::FixedTimesteps(n) makes every app.update() run exactly n fixed steps, which keeps tests independent of the machine's speed. See Time.