Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify physics time APIs with Time<Physics> and Time<Substeps> #214

Merged
merged 14 commits into from
Nov 3, 2023

Conversation

Jondolf
Copy link
Owner

@Jondolf Jondolf commented Oct 31, 2023

Objective

Currently, bevy_xpbd has the DeltaTime, SubDeltaTime, PhysicsTimestep, PhysicsTimescale and PhysicsLoop resources for managing logic related to time and scheduling. Having so many separate resources can be confusing and inconvenient for users.

The DeltaTime and SubDeltaTime resources also create a contextual divide into two system groups: those that run in the PhysicsSchedule and those that run in the SubstepSchedule. However, a lot of systems aren't necessarily tied to a specific schedule in functionality; what if you wanted to run position integration outside of the substepping loop? It would be better if all of these systems had a shared time resource that could just be interpreted differently based on the schedule.

Solution

Replace DeltaTime, SubDeltaTime, PhysicsTimestep, PhysicsTimescale and PhysicsLoop with Time<Physics> and Time<Substeps> using the new time API in Bevy 0.12.

The generic Time resource is replaced by Time<Physics> in the PhysicsSchedule and Time<Substeps> in the SubstepSchedule. This means that systems are now agnostic over the time resource used, which also reduces cognitive overhead for users, as they can just use Time instead of e.g. DeltaTime in the PhysicsSchedule.

The timestep is still configurable, as Physics contains an TimestepMode enum like the old PhysicsTimestep. To match Bevy's APIs, the timestep also uses a Duration instead of a single scalar value. Physics has helper methods like fixed_hz, fixed_once_hz and variable to reduce the boilerplate a bit.

// Before
app.insert_resource(PhysicsTimestep::Fixed(1.0 / 144.0));

// After
app.insert_resource(Time::new_with(Physics::fixed_hz(1.0 / 144.0)));

The physics loop is now also controlled using Time<Physics> and its properties paused and relative_speed.


Migration guide

  • Replace every DeltaTime in PhysicsSchedule and every SubDeltaTime in SubstepSchedule with Time, elsewhere explicitly use Time<Physics> and Time<Substep>
  • When advancing physics manually, instead of setting DeltaTime, advance the Time<Physics> clock using time.advance_by(...)
  • Replace PhysicsTimestep with Time::new_with(Physics::FooBar(...))
  • Replace PhysicsLoop::pause/resume with Time::<Physics>::pause/unpause
  • Replace PhysicsLoop::step with advancing the physics clock using Time::<Physics>::advance_by
  • Replace PhysicsTimescale usage with Time::<Physics>::with/set_relative_speed

Todo

  • Add code examples to documentation
  • Verify that all timesteps function correctly

Future work

It'd be nice to use Time<Virtual> and Time<Fixed> directly, but this has limitations that currently make it not viable.

@Jondolf Jondolf added C-Enhancement New feature or request A-Scheduling Relates to scheduling or system sets labels Oct 31, 2023
@Jondolf Jondolf marked this pull request as ready for review November 1, 2023 20:31
@Jondolf Jondolf merged commit 3720f86 into bevy-0.12 Nov 3, 2023
4 checks passed
@Jondolf Jondolf deleted the physics-clocks branch November 3, 2023 22:45
@Jondolf Jondolf mentioned this pull request Nov 3, 2023
Jondolf added a commit that referenced this pull request Nov 4, 2023
# Objective

Add Bevy 0.12 support.

## Solution

- Update Bevy (and Criterion)
- Migrate to `Interned` `ScheduleLabel`s (#204)
- Fix deprecated methods
- Unify time and simulation loop APIs under `Time<Physics>` and `Time<Substeps>` (#214)
- Update `README.md` (and its feature list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Scheduling Relates to scheduling or system sets C-Enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant