flowchart TD
A[Set to allow user to change physics data] --> COPY(Copy physics into background)
COPY --> FORK(Start bevy task)
FORK --> Loop{Elapsed time}
FORK --> SIM["Rapier physics simulation (50ms ; 20fps)"]
Loop -->|"-50ms"| BU["Bevy update (16.6ms ; 60FPS)"]
BU --> Loop
Loop -->|"+50ms"| J(join)
SIM --> J(Join bevy task)
J --> WRITE(Write physics from background task into bevy)
WRITE --> A
It would be interesting to have physics running in a background task over a few frames, and using interpolation to smooth results.
We could have a physics world simulating at 30 or 20 fps ; while rendering would go at 60 fps.
TODO
Plan
flowchart TD A[Set to allow user to change physics data] --> COPY(Copy physics into background) COPY --> FORK(Start bevy task) FORK --> Loop{Elapsed time} FORK --> SIM["Rapier physics simulation (50ms ; 20fps)"] Loop -->|"-50ms"| BU["Bevy update (16.6ms ; 60FPS)"] BU --> Loop Loop -->|"+50ms"| J(join) SIM --> J(Join bevy task) J --> WRITE(Write physics from background task into bevy) WRITE --> AImplementation
step_simulation()is expected to provide the end result of our simulation, It makes sense for it to be the system reading the result of the background computation.start_simulationsystem to dump physics state and start the simulation.apply_*_user_changesto be before thatstart_simulationsystem. Clarify in the docs that any value modified outside of expected set will be without effect.Difficulties
Passing data out of ECS
A naive approach is to copy the whole
RapierContext; butIslandSolverdoesn´t implement Clone, and this goes quite deep ( toSolverConstraintsSet) ; we should keep in mind simd and enhanced-determinism features compatibility.we might not need SolverConstraintsSet (and
physicsPipelinein general) so try to clone everything but that.This should be possible but adding Clone to those traits should probably be behind a feature?
Another approach is to unsafely copy 😱
Another approach would be to
mem::swapthe RapierContext with an empty/incorrect one for the ECS, while the simulation is ongoing.Option<RapierContext>+.take()🤔Attempts
background simulation: See bevy task to run physics #596
interpolation experiments: Interpolation exploration ThierryBerger/bevy_rapier#28
rapier-agnostic approach: Background fixed update Jondolf/bevy_transform_interpolation#7