Skip to content

Commit a6fc5bf

Browse files
authored
Fix physics being run with zero delta time (#622)
# Objective Fixes #619. When pausing physics, the `PhysicsSchedule` can still run even though the delta time is zero. This can cause simulation problems and sometimes even results in values becoming NaN or infinite due to division by zero. This is a regression from 0.1, which handled scheduling differently and did not run physics with a delta time of zero. ## Solution Don't run the `PhysicsSchedule` if the delta time is zero.
1 parent 0e72040 commit a6fc5bf

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/schedule/mod.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -248,32 +248,35 @@ fn run_physics_schedule(world: &mut World, mut is_first_run: Local<IsFirstRun>)
248248
.delta()
249249
.mul_f64(physics_clock.relative_speed_f64());
250250

251-
// Advance physics clock by timestep if not paused.
251+
// Advance the physics clock by the timestep if not paused.
252252
if !is_paused {
253253
world.resource_mut::<Time<Physics>>().advance_by(timestep);
254254

255-
// Advance substep clock already so that systems running before the substepping loop have the right delta.
255+
// Advance the substep clock already so that systems running
256+
// before the substepping loop have the right delta.
256257
let SubstepCount(substeps) = *world.resource::<SubstepCount>();
257258
let sub_delta = timestep.div_f64(substeps as f64);
258259
world.resource_mut::<Time<Substeps>>().advance_by(sub_delta);
259260
}
260261

261-
// Set generic `Time` resource to `Time<Physics>`.
262+
// Set the generic `Time` resource to `Time<Physics>`.
262263
*world.resource_mut::<Time>() = world.resource::<Time<Physics>>().as_generic();
263264

264-
// Advance simulation.
265-
trace!("running PhysicsSchedule");
266-
schedule.run(world);
265+
// Advance the simulation.
266+
if !world.resource::<Time>().delta().is_zero() {
267+
trace!("running PhysicsSchedule");
268+
schedule.run(world);
269+
}
267270

268-
// If physics is paused, reset delta time to stop simulation
271+
// If physics is paused, reset delta time to stop the simulation
269272
// unless users manually advance `Time<Physics>`.
270273
if is_paused {
271274
world
272275
.resource_mut::<Time<Physics>>()
273276
.advance_by(Duration::ZERO);
274277
}
275278

276-
// Set generic `Time` resource back to the clock that was active before physics.
279+
// Set the generic `Time` resource back to the clock that was active before physics.
277280
*world.resource_mut::<Time>() = old_clock;
278281
});
279282

0 commit comments

Comments
 (0)