-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
42 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,48 @@ | ||
//! Shows how to create systems that run every fixed timestep, rather than every tick. | ||
use bevy::prelude::*; | ||
use bevy::time::TimeContext; | ||
|
||
const FIXED_TIMESTEP: f32 = 0.5; | ||
fn main() { | ||
App::new() | ||
.add_plugins(DefaultPlugins) | ||
// this system will run once every update (it should match your screen's refresh rate) | ||
// set fixed timestep to run systems ten times a second | ||
.insert_resource(FixedTimestep::from_hz(10.0)) | ||
// this system will run every update (this should match the screen refresh rate) | ||
.add_systems(Update, frame_update) | ||
// add our system to the fixed timestep schedule | ||
// this system will run ten times a second | ||
.add_systems(FixedUpdate, fixed_update) | ||
// configure our fixed timestep schedule to run twice a second | ||
.insert_resource(FixedTime::new_from_secs(FIXED_TIMESTEP)) | ||
.run(); | ||
} | ||
|
||
fn frame_update(mut last_time: Local<f32>, time: Res<Time>) { | ||
fn frame_update(mut last_time: Local<f32>, time: Res<Time>, real_time: Res<RealTime>) { | ||
info!( | ||
"time since last frame_update: {}", | ||
time.raw_elapsed_seconds() - *last_time | ||
real_time.elapsed_seconds() - *last_time | ||
); | ||
*last_time = time.raw_elapsed_seconds(); | ||
|
||
assert!(matches!(time.context(), TimeContext::Update)); | ||
*last_time = real_time.elapsed_seconds(); | ||
} | ||
|
||
fn fixed_update(mut last_time: Local<f32>, time: Res<Time>, fixed_time: Res<FixedTime>) { | ||
fn fixed_update( | ||
mut last_time: Local<f32>, | ||
time: Res<Time>, | ||
real_time: Res<RealTime>, | ||
fixed_timestep: Res<FixedTimestep>, | ||
) { | ||
assert!(matches!(time.context(), TimeContext::FixedUpdate)); | ||
assert_eq!(time.delta(), fixed_timestep.size()); | ||
|
||
info!("fixed timestep: {}\n", time.delta_seconds()); | ||
info!( | ||
"time since last fixed_update: {}\n", | ||
time.raw_elapsed_seconds() - *last_time | ||
real_time.elapsed_seconds() - *last_time | ||
); | ||
|
||
info!("fixed timestep: {}\n", FIXED_TIMESTEP); | ||
info!( | ||
"time accrued toward next fixed_update: {}\n", | ||
fixed_time.accumulated().as_secs_f32() | ||
fixed_timestep.overstep().as_secs_f32() | ||
); | ||
*last_time = time.raw_elapsed_seconds(); | ||
*last_time = real_time.elapsed_seconds(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters