Skip to content

Commit 7486c1e

Browse files
authored
Fix examples after changes to Time
1 parent 9760fe2 commit 7486c1e

File tree

5 files changed

+38
-32
lines changed

5 files changed

+38
-32
lines changed

examples/2d/rotation.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
33
use bevy::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(Time::<Fixed>::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,14 @@ 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

143-
// get the ship's forward vector by applying the current rotation to the ships initial facing vector
143+
// get the ship's forward vector by applying the current rotation to the ships initial facing
144+
// vector
144145
let movement_direction = transform.rotation * Vec3::Y;
145-
// 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+
// get the distance the ship will move based on direction, the ship's movement speed and delta
147+
// time
148+
let movement_distance = movement_factor * ship.movement_speed * time.delta_seconds();
147149
// create the change in translation using the new movement direction and distance
148150
let translation_delta = movement_direction * movement_distance;
149151
// update the ship translation with our new translation delta
@@ -182,8 +184,8 @@ fn snap_to_player_system(
182184
/// if not, which way to rotate to face the player. The dot product on two unit length vectors
183185
/// will return a value between -1.0 and +1.0 which tells us the following about the two vectors:
184186
///
185-
/// * If the result is 1.0 the vectors are pointing in the same direction, the angle between them
186-
/// is 0 degrees.
187+
/// * If the result is 1.0 the vectors are pointing in the same direction, the angle between them is
188+
/// 0 degrees.
187189
/// * If the result is 0.0 the vectors are perpendicular, the angle between them is 90 degrees.
188190
/// * If the result is -1.0 the vectors are parallel but pointing in opposite directions, the angle
189191
/// between them is 180 degrees.
@@ -198,6 +200,7 @@ fn snap_to_player_system(
198200
/// floating point precision loss, so it pays to clamp your dot product value before calling
199201
/// `acos`.
200202
fn rotate_to_player_system(
203+
time: Res<Time>,
201204
mut query: Query<(&RotateToPlayer, &mut Transform), Without<Player>>,
202205
player_query: Query<&Transform, With<Player>>,
203206
) {
@@ -242,7 +245,8 @@ fn rotate_to_player_system(
242245
let max_angle = forward_dot_player.clamp(-1.0, 1.0).acos(); // clamp acos for safety
243246

244247
// calculate angle of rotation with limit
245-
let rotation_angle = rotation_sign * (config.rotation_speed * TIME_STEP).min(max_angle);
248+
let rotation_angle =
249+
rotation_sign * (config.rotation_speed * time.delta_seconds()).min(max_angle);
246250

247251
// rotate the enemy to face the player
248252
enemy_transform.rotate_z(rotation_angle);

examples/ecs/fixed_timestep.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
use bevy::prelude::*;
44

5-
const FIXED_TIMESTEP: f32 = 0.5;
65
fn main() {
76
App::new()
87
.add_plugins(DefaultPlugins)
@@ -11,28 +10,31 @@ fn main() {
1110
// add our system to the fixed timestep schedule
1211
.add_systems(FixedUpdate, fixed_update)
1312
// configure our fixed timestep schedule to run twice a second
14-
.insert_resource(FixedTime::new_from_secs(FIXED_TIMESTEP))
13+
.insert_resource(Time::<Fixed>::from_seconds(0.5))
1514
.run();
1615
}
1716

1817
fn frame_update(mut last_time: Local<f32>, time: Res<Time>) {
18+
// Default `Time` is `Time<Virtual>` here
1919
info!(
2020
"time since last frame_update: {}",
21-
time.raw_elapsed_seconds() - *last_time
21+
time.elapsed_seconds() - *last_time
2222
);
23-
*last_time = time.raw_elapsed_seconds();
23+
*last_time = time.elapsed_seconds();
2424
}
2525

26-
fn fixed_update(mut last_time: Local<f32>, time: Res<Time>, fixed_time: Res<FixedTime>) {
26+
fn fixed_update(mut last_time: Local<f32>, time: Res<Time>, fixed_time: Res<Time<Fixed>>) {
27+
// Default `Time`is `Time<Fixed>` here
2728
info!(
2829
"time since last fixed_update: {}\n",
29-
time.raw_elapsed_seconds() - *last_time
30+
time.elapsed_seconds() - *last_time
3031
);
3132

32-
info!("fixed timestep: {}\n", FIXED_TIMESTEP);
33+
info!("fixed timestep: {}\n", time.delta_seconds());
34+
// If we want to see the overstep, we need to access `Time<Fixed>` specifically
3335
info!(
3436
"time accrued toward next fixed_update: {}\n",
35-
fixed_time.accumulated().as_secs_f32()
37+
fixed_time.overstep().as_secs_f32()
3638
);
37-
*last_time = time.raw_elapsed_seconds();
39+
*last_time = time.elapsed_seconds();
3840
}

examples/ecs/iter_combinations.rs

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

6-
const DELTA_TIME: f32 = 0.01;
7-
86
fn main() {
97
App::new()
108
.add_plugins(DefaultPlugins)
@@ -13,7 +11,6 @@ fn main() {
1311
..default()
1412
})
1513
.insert_resource(ClearColor(Color::BLACK))
16-
.insert_resource(FixedTime::new_from_secs(DELTA_TIME))
1714
.add_systems(Startup, generate_bodies)
1815
.add_systems(FixedUpdate, (interact_bodies, integrate))
1916
.add_systems(Update, look_at_star)
@@ -41,6 +38,7 @@ struct BodyBundle {
4138
}
4239

4340
fn generate_bodies(
41+
time: Res<Time>,
4442
mut commands: Commands,
4543
mut meshes: ResMut<Assets<Mesh>>,
4644
mut materials: ResMut<Assets<StandardMaterial>>,
@@ -96,7 +94,7 @@ fn generate_bodies(
9694
rng.gen_range(vel_range.clone()),
9795
rng.gen_range(vel_range.clone()),
9896
rng.gen_range(vel_range.clone()),
99-
) * DELTA_TIME,
97+
) * time.delta_seconds(),
10098
),
10199
});
102100
}
@@ -160,8 +158,8 @@ fn interact_bodies(mut query: Query<(&Mass, &GlobalTransform, &mut Acceleration)
160158
}
161159
}
162160

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

examples/games/breakout.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,9 @@ fn main() {
5353
.insert_resource(Scoreboard { score: 0 })
5454
.insert_resource(ClearColor(BACKGROUND_COLOR))
5555
.add_event::<CollisionEvent>()
56-
// Configure how frequently our gameplay systems are run
57-
.insert_resource(FixedTime::new_from_secs(1.0 / 60.0))
5856
.add_systems(Startup, setup)
5957
// Add our gameplay simulation systems to the fixed timestep schedule
58+
// which runs at 64 Hz by default
6059
.add_systems(
6160
FixedUpdate,
6261
(
@@ -306,7 +305,7 @@ fn setup(
306305
fn move_paddle(
307306
keyboard_input: Res<Input<KeyCode>>,
308307
mut query: Query<&mut Transform, With<Paddle>>,
309-
time_step: Res<FixedTime>,
308+
time: Res<Time>,
310309
) {
311310
let mut paddle_transform = query.single_mut();
312311
let mut direction = 0.0;
@@ -321,7 +320,7 @@ fn move_paddle(
321320

322321
// Calculate the new horizontal paddle position based on player input
323322
let new_paddle_position =
324-
paddle_transform.translation.x + direction * PADDLE_SPEED * time_step.period.as_secs_f32();
323+
paddle_transform.translation.x + direction * PADDLE_SPEED * time.delta_seconds();
325324

326325
// Update the paddle position,
327326
// making sure it doesn't cause the paddle to leave the arena
@@ -331,10 +330,10 @@ fn move_paddle(
331330
paddle_transform.translation.x = new_paddle_position.clamp(left_bound, right_bound);
332331
}
333332

334-
fn apply_velocity(mut query: Query<(&mut Transform, &Velocity)>, time_step: Res<FixedTime>) {
333+
fn apply_velocity(mut query: Query<(&mut Transform, &Velocity)>, time: Res<Time>) {
335334
for (mut transform, velocity) in &mut query {
336-
transform.translation.x += velocity.x * time_step.period.as_secs_f32();
337-
transform.translation.y += velocity.y * time_step.period.as_secs_f32();
335+
transform.translation.x += velocity.x * time.delta_seconds();
336+
transform.translation.y += velocity.y * time.delta_seconds();
338337
}
339338
}
340339

examples/stress_tests/bevymark.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use bevy::{
1010
prelude::*,
1111
render::render_resource::{Extent3d, TextureDimension, TextureFormat},
1212
sprite::{MaterialMesh2dBundle, Mesh2dHandle},
13+
utils::Duration,
1314
window::{PresentMode, WindowResolution},
1415
};
1516
use rand::{rngs::StdRng, seq::SliceRandom, Rng, SeedableRng};
@@ -122,7 +123,9 @@ fn main() {
122123
counter_system,
123124
),
124125
)
125-
.insert_resource(FixedTime::new_from_secs(FIXED_TIMESTEP))
126+
.insert_resource(Time::<Fixed>::from_duration(Duration::from_secs_f32(
127+
FIXED_TIMESTEP,
128+
)))
126129
.run();
127130
}
128131

0 commit comments

Comments
 (0)