Skip to content

Commit 9053775

Browse files
committed
Fix examples after changes to Time
1 parent 30971bb commit 9053775

File tree

5 files changed

+37
-31
lines changed

5 files changed

+37
-31
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::{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(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: 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(Time::<Fixed>::from_hz(100.0))
1715
.add_systems(Startup, generate_bodies)
1816
.add_systems(FixedUpdate, (interact_bodies, integrate))
1917
.add_systems(Update, look_at_star)
@@ -41,6 +39,7 @@ struct BodyBundle {
4139
}
4240

4341
fn generate_bodies(
42+
time: Res<Time>,
4443
mut commands: Commands,
4544
mut meshes: ResMut<Assets<Mesh>>,
4645
mut materials: ResMut<Assets<StandardMaterial>>,
@@ -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(Time::<Fixed>::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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use bevy::{
66
diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
77
prelude::*,
8+
utils::Duration,
89
window::{PresentMode, WindowResolution},
910
};
1011
use rand::{rngs::StdRng, thread_rng, Rng, SeedableRng};
@@ -56,7 +57,7 @@ fn main() {
5657
counter_system,
5758
),
5859
)
59-
.insert_resource(FixedTime::new_from_secs(0.2))
60+
.insert_resource(Time::<Fixed>::from_duration(Duration::from_millis(200)))
6061
.run();
6162
}
6263

0 commit comments

Comments
 (0)