Skip to content

Commit e41c567

Browse files
committed
update examples
1 parent eb3c364 commit e41c567

File tree

5 files changed

+42
-31
lines changed

5 files changed

+42
-31
lines changed

examples/2d/rotation.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
33
use bevy::{math::Vec3Swizzles, prelude::*};
44

5-
const TIME_STEP: f32 = 1.0 / 60.0;
65
const BOUNDS: Vec2 = Vec2::new(1200.0, 640.0);
76

87
fn main() {
98
App::new()
109
.add_plugins(DefaultPlugins)
11-
.insert_resource(FixedTime::new_from_secs(TIME_STEP))
10+
.insert_resource(FixedTimestep::from_hz(60.0))
1211
.add_systems(Startup, setup)
1312
.add_systems(
1413
FixedUpdate,
@@ -117,6 +116,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
117116

118117
/// Demonstrates applying rotation and movement based on keyboard input.
119118
fn player_movement_system(
119+
time: Res<Time>,
120120
keyboard_input: Res<Input<KeyCode>>,
121121
mut query: Query<(&Player, &mut Transform)>,
122122
) {
@@ -138,12 +138,12 @@ fn player_movement_system(
138138
}
139139

140140
// update the ship rotation around the Z axis (perpendicular to the 2D plane of the screen)
141-
transform.rotate_z(rotation_factor * ship.rotation_speed * TIME_STEP);
141+
transform.rotate_z(rotation_factor * ship.rotation_speed * time.delta_seconds());
142142

143143
// get the ship's forward vector by applying the current rotation to the ships initial facing vector
144144
let movement_direction = transform.rotation * Vec3::Y;
145145
// get the distance the ship will move based on direction, the ship's movement speed and delta time
146-
let movement_distance = movement_factor * ship.movement_speed * TIME_STEP;
146+
let movement_distance = movement_factor * ship.movement_speed * time.delta_seconds();
147147
// create the change in translation using the new movement direction and distance
148148
let translation_delta = movement_direction * movement_distance;
149149
// update the ship translation with our new translation delta
@@ -198,6 +198,7 @@ fn snap_to_player_system(
198198
/// floating point precision loss, so it pays to clamp your dot product value before calling
199199
/// `acos`.
200200
fn rotate_to_player_system(
201+
time: Res<Time>,
201202
mut query: Query<(&RotateToPlayer, &mut Transform), Without<Player>>,
202203
player_query: Query<&Transform, With<Player>>,
203204
) {
@@ -242,7 +243,8 @@ fn rotate_to_player_system(
242243
let max_angle = forward_dot_player.clamp(-1.0, 1.0).acos(); // clamp acos for safety
243244

244245
// calculate angle of rotation with limit
245-
let rotation_angle = rotation_sign * (config.rotation_speed * TIME_STEP).min(max_angle);
246+
let rotation_angle =
247+
rotation_sign * (config.rotation_speed * time.delta_seconds()).min(max_angle);
246248

247249
// rotate the enemy to face the player
248250
enemy_transform.rotate_z(rotation_angle);

examples/ecs/fixed_timestep.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,48 @@
11
//! Shows how to create systems that run every fixed timestep, rather than every tick.
22
33
use bevy::prelude::*;
4+
use bevy::time::TimeContext;
45

5-
const FIXED_TIMESTEP: f32 = 0.5;
66
fn main() {
77
App::new()
88
.add_plugins(DefaultPlugins)
9-
// this system will run once every update (it should match your screen's refresh rate)
9+
// set fixed timestep to run systems ten times a second
10+
.insert_resource(FixedTimestep::from_hz(10.0))
11+
// this system will run every update (this should match the screen refresh rate)
1012
.add_systems(Update, frame_update)
11-
// add our system to the fixed timestep schedule
13+
// this system will run ten times a second
1214
.add_systems(FixedUpdate, fixed_update)
13-
// configure our fixed timestep schedule to run twice a second
14-
.insert_resource(FixedTime::new_from_secs(FIXED_TIMESTEP))
1515
.run();
1616
}
1717

18-
fn frame_update(mut last_time: Local<f32>, time: Res<Time>) {
18+
fn frame_update(mut last_time: Local<f32>, time: Res<Time>, real_time: Res<RealTime>) {
1919
info!(
2020
"time since last frame_update: {}",
21-
time.raw_elapsed_seconds() - *last_time
21+
real_time.elapsed_seconds() - *last_time
2222
);
23-
*last_time = time.raw_elapsed_seconds();
23+
24+
assert!(matches!(time.context(), TimeContext::Update));
25+
*last_time = real_time.elapsed_seconds();
2426
}
2527

26-
fn fixed_update(mut last_time: Local<f32>, time: Res<Time>, fixed_time: Res<FixedTime>) {
28+
fn fixed_update(
29+
mut last_time: Local<f32>,
30+
time: Res<Time>,
31+
real_time: Res<RealTime>,
32+
fixed_timestep: Res<FixedTimestep>,
33+
) {
34+
assert!(matches!(time.context(), TimeContext::FixedUpdate));
35+
assert_eq!(time.delta(), fixed_timestep.size());
36+
37+
info!("fixed timestep: {}\n", time.delta_seconds());
2738
info!(
2839
"time since last fixed_update: {}\n",
29-
time.raw_elapsed_seconds() - *last_time
40+
real_time.elapsed_seconds() - *last_time
3041
);
3142

32-
info!("fixed timestep: {}\n", FIXED_TIMESTEP);
3343
info!(
3444
"time accrued toward next fixed_update: {}\n",
35-
fixed_time.accumulated().as_secs_f32()
45+
fixed_timestep.overstep().as_secs_f32()
3646
);
37-
*last_time = time.raw_elapsed_seconds();
47+
*last_time = real_time.elapsed_seconds();
3848
}

examples/ecs/iter_combinations.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
use bevy::{pbr::AmbientLight, prelude::*};
44
use rand::{thread_rng, Rng};
55

6-
const DELTA_TIME: f32 = 0.01;
7-
86
fn main() {
97
App::new()
108
.add_plugins(DefaultPlugins)
@@ -13,7 +11,7 @@ fn main() {
1311
..default()
1412
})
1513
.insert_resource(ClearColor(Color::BLACK))
16-
.insert_resource(FixedTime::new_from_secs(DELTA_TIME))
14+
.insert_resource(FixedTimestep::from_hz(100.0))
1715
.add_systems(Startup, generate_bodies)
1816
.add_systems(FixedUpdate, (interact_bodies, integrate))
1917
.add_systems(Update, look_at_star)
@@ -42,6 +40,7 @@ struct BodyBundle {
4240

4341
fn generate_bodies(
4442
mut commands: Commands,
43+
time: Res<Time>,
4544
mut meshes: ResMut<Assets<Mesh>>,
4645
mut materials: ResMut<Assets<StandardMaterial>>,
4746
) {
@@ -96,7 +95,7 @@ fn generate_bodies(
9695
rng.gen_range(vel_range.clone()),
9796
rng.gen_range(vel_range.clone()),
9897
rng.gen_range(vel_range.clone()),
99-
) * DELTA_TIME,
98+
) * time.delta_seconds(),
10099
),
101100
});
102101
}
@@ -160,8 +159,8 @@ fn interact_bodies(mut query: Query<(&Mass, &GlobalTransform, &mut Acceleration)
160159
}
161160
}
162161

163-
fn integrate(mut query: Query<(&mut Acceleration, &mut Transform, &mut LastPos)>) {
164-
let dt_sq = DELTA_TIME * DELTA_TIME;
162+
fn integrate(time: Res<Time>, mut query: Query<(&mut Acceleration, &mut Transform, &mut LastPos)>) {
163+
let dt_sq = time.delta_seconds() * time.delta_seconds();
165164
for (mut acceleration, mut transform, mut last_pos) in &mut query {
166165
// verlet integration
167166
// x(t+dt) = 2x(t) - x(t-dt) + a(t)dt^2 + O(dt^4)

examples/games/breakout.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn main() {
5454
.insert_resource(ClearColor(BACKGROUND_COLOR))
5555
.add_event::<CollisionEvent>()
5656
// Configure how frequently our gameplay systems are run
57-
.insert_resource(FixedTime::new_from_secs(1.0 / 60.0))
57+
.insert_resource(FixedTimestep::from_hz(60.0))
5858
.add_systems(Startup, setup)
5959
// Add our gameplay simulation systems to the fixed timestep schedule
6060
.add_systems(
@@ -311,7 +311,7 @@ fn setup(
311311
fn move_paddle(
312312
keyboard_input: Res<Input<KeyCode>>,
313313
mut query: Query<&mut Transform, With<Paddle>>,
314-
time_step: Res<FixedTime>,
314+
time: Res<Time>,
315315
) {
316316
let mut paddle_transform = query.single_mut();
317317
let mut direction = 0.0;
@@ -326,7 +326,7 @@ fn move_paddle(
326326

327327
// Calculate the new horizontal paddle position based on player input
328328
let new_paddle_position =
329-
paddle_transform.translation.x + direction * PADDLE_SPEED * time_step.period.as_secs_f32();
329+
paddle_transform.translation.x + direction * PADDLE_SPEED * time.delta_seconds();
330330

331331
// Update the paddle position,
332332
// making sure it doesn't cause the paddle to leave the arena
@@ -336,10 +336,10 @@ fn move_paddle(
336336
paddle_transform.translation.x = new_paddle_position.clamp(left_bound, right_bound);
337337
}
338338

339-
fn apply_velocity(mut query: Query<(&mut Transform, &Velocity)>, time_step: Res<FixedTime>) {
339+
fn apply_velocity(mut query: Query<(&mut Transform, &Velocity)>, time: Res<Time>) {
340340
for (mut transform, velocity) in &mut query {
341-
transform.translation.x += velocity.x * time_step.period.as_secs_f32();
342-
transform.translation.y += velocity.y * time_step.period.as_secs_f32();
341+
transform.translation.x += velocity.x * time.delta_seconds();
342+
transform.translation.y += velocity.y * time.delta_seconds();
343343
}
344344
}
345345

examples/stress_tests/bevymark.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ fn main() {
5656
counter_system,
5757
),
5858
)
59-
.insert_resource(FixedTime::new_from_secs(0.2))
59+
.insert_resource(FixedTimestep::from_hz(60.0))
6060
.run();
6161
}
6262

0 commit comments

Comments
 (0)