Skip to content

Port the physics in fixed timestep example to 3D #20089

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

Merged
merged 16 commits into from
Jul 14, 2025

Conversation

janhohenheim
Copy link
Member

@janhohenheim janhohenheim commented Jul 11, 2025

Objective

Since I originally wrote this example, many people on Discord have asked me specifically how to handle camera movement in and around fixed timesteps. I had to write that information up maaaany times, so I believe this is an area where the example falls short of.

Solution

Let's port the example to 3D, where we can better showcase how to handle the camera. The knowledge is trivially transferable to 2D :)
Also, we don't need to average out continuous input. Just using the last one is fine in practice. Still, we need to keep around the AccumulatedInput resource for things like jumping.

Testing

Screencast.From.2025-07-11.20-41-28.mp4

Notes

  • The current implementation is extremely faithful to how it will look like in practice when writing a 3D game using e.g. Avian. With the exception that Avian does the part with the actual physics of course
  • I'd love to showcase how to map sequences of inputs to fixed updates, but winit does not export timestamps
  • I'd also like to showcase instantaneous inputs like activating a boost or shooting a laser, but that would make the example even bigger
  • Not locking the cursor because doing so correctly on Wasm in the current Bevy version is not trivial at all

@janhohenheim janhohenheim added A-ECS Entities, components, systems, and events C-Examples An addition or correction to our examples A-Physics Collisions, kinematics, forces and more M-Deliberate-Rendering-Change An intentional change to how tests and examples are rendered D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jul 11, 2025
@alice-i-cecile alice-i-cecile self-requested a review July 11, 2025 18:45
@janhohenheim janhohenheim added the A-Time Involves time keeping and reporting label Jul 13, 2025
@janhohenheim janhohenheim requested a review from Jondolf July 14, 2025 14:18
Comment on lines +342 to +363
#[derive(Resource, Debug, Deref, DerefMut, Default)]
pub struct DidFixedTimestepRunThisFrame(bool);

/// Reset the flag at the start of every frame.
fn clear_fixed_timestep_flag(
mut did_fixed_timestep_run_this_frame: ResMut<DidFixedTimestepRunThisFrame>,
) {
did_fixed_timestep_run_this_frame.0 = false;
}

/// Set the flag during each fixed timestep.
fn set_fixed_time_step_flag(
mut did_fixed_timestep_run_this_frame: ResMut<DidFixedTimestepRunThisFrame>,
) {
did_fixed_timestep_run_this_frame.0 = true;
}

fn did_fixed_timestep_run_this_frame(
did_fixed_timestep_run_this_frame: Res<DidFixedTimestepRunThisFrame>,
) -> bool {
did_fixed_timestep_run_this_frame.0
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like something that should be built in to bevy instead? Seems pretty widely useful.

Copy link
Member Author

@janhohenheim janhohenheim Jul 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed lol.
Can PR after this is merged.

@janhohenheim janhohenheim requested a review from kristoff3r July 14, 2025 14:57
@janhohenheim janhohenheim added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jul 14, 2025
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jul 14, 2025
Merged via the queue into bevyengine:main with commit 36fb83f Jul 14, 2025
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events A-Physics Collisions, kinematics, forces and more A-Time Involves time keeping and reporting C-Examples An addition or correction to our examples D-Straightforward Simple bug fixes and API improvements, docs, test and examples M-Deliberate-Rendering-Change An intentional change to how tests and examples are rendered S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants