From 1441afd6db7f2de685a6f9443aae41ff02e358db Mon Sep 17 00:00:00 2001 From: Joy <51241057+maniwani@users.noreply.github.com> Date: Thu, 31 Mar 2022 10:28:24 -0700 Subject: [PATCH] fix examples --- examples/2d/rotation.rs | 16 ++++--- examples/2d/text2d.rs | 8 ++-- examples/3d/manual_gltf_animation_player.rs | 12 +++-- examples/3d/update_gltf_scene.rs | 12 ++--- examples/animation/custom_skinned_mesh.rs | 2 +- examples/animation/gltf_skinned_mesh.rs | 2 +- examples/ecs/component_change_detection.rs | 2 +- examples/ecs/fixed_timestep.rs | 50 ++++++++++++--------- examples/ecs/iter_combinations.rs | 19 +++++--- examples/game/alien_cake_addict.rs | 9 +++- examples/game/breakout.rs | 17 ++++--- examples/scene/scene.rs | 4 +- examples/tools/bevymark.rs | 2 +- examples/ui/text.rs | 2 +- examples/ui/text_debug.rs | 2 +- 15 files changed, 98 insertions(+), 61 deletions(-) diff --git a/examples/2d/rotation.rs b/examples/2d/rotation.rs index 8ab5b7fd32050..f6912a0ab1044 100644 --- a/examples/2d/rotation.rs +++ b/examples/2d/rotation.rs @@ -13,7 +13,7 @@ fn main() { .add_startup_system(setup) .add_system_set( SystemSet::new() - .with_run_criteria(FixedTimestep::step(TIME_STEP as f64)) + .with_run_criteria(FixedTimestep::step) .with_system(player_movement_system) .with_system(snap_to_player_system) .with_system(rotate_to_player_system), @@ -51,7 +51,9 @@ struct RotateToPlayer { /// * Z axis goes from far to near (+Z points towards you, out of the screen) /// /// The origin is at the center of the screen. -fn setup(mut commands: Commands, asset_server: Res) { +fn setup(mut commands: Commands, mut time: ResMut, asset_server: Res) { + time.set_delta_seconds(TIME_STEP); + let ship_handle = asset_server.load("textures/simplespace/ship_C.png"); let enemy_a_handle = asset_server.load("textures/simplespace/enemy_A.png"); let enemy_b_handle = asset_server.load("textures/simplespace/enemy_B.png"); @@ -112,6 +114,7 @@ fn setup(mut commands: Commands, asset_server: Res) { /// Demonstrates applying rotation and movement based on keyboard input. fn player_movement_system( + time: Res, keyboard_input: Res>, mut query: Query<(&Player, &mut Transform)>, ) { @@ -133,14 +136,15 @@ fn player_movement_system( } // create the change in rotation around the Z axis (perpendicular to the 2D plane of the screen) - let rotation_delta = Quat::from_rotation_z(rotation_factor * ship.rotation_speed * TIME_STEP); + let rotation_delta = + Quat::from_rotation_z(rotation_factor * ship.rotation_speed * time.delta_seconds()); // update the ship rotation with our rotation delta transform.rotation *= rotation_delta; // get the ship's forward vector by applying the current rotation to the ships initial facing vector let movement_direction = transform.rotation * Vec3::Y; // get the distance the ship will move based on direction, the ship's movement speed and delta time - let movement_distance = movement_factor * ship.movement_speed * TIME_STEP; + let movement_distance = movement_factor * ship.movement_speed * time.delta_seconds(); // create the change in translation using the new movement direction and distance let translation_delta = movement_direction * movement_distance; // update the ship translation with our new translation delta @@ -195,6 +199,7 @@ fn snap_to_player_system( /// floating point precision loss, so it pays to clamp your dot product value before calling /// `acos`. fn rotate_to_player_system( + time: Res, mut query: Query<(&RotateToPlayer, &mut Transform), Without>, player_query: Query<&Transform, With>, ) { @@ -239,7 +244,8 @@ fn rotate_to_player_system( let max_angle = forward_dot_player.clamp(-1.0, 1.0).acos(); // clamp acos for safety // calculate angle of rotation with limit - let rotation_angle = rotation_sign * (config.rotation_speed * TIME_STEP).min(max_angle); + let rotation_angle = + rotation_sign * (config.rotation_speed * time.delta_seconds()).min(max_angle); // get the quaternion to rotate from the current enemy facing direction towards the // direction facing the player diff --git a/examples/2d/text2d.rs b/examples/2d/text2d.rs index 9a41e7912c246..10352bc694828 100644 --- a/examples/2d/text2d.rs +++ b/examples/2d/text2d.rs @@ -94,8 +94,8 @@ fn animate_translation( mut query: Query<&mut Transform, (With, With)>, ) { for mut transform in query.iter_mut() { - transform.translation.x = 100.0 * time.seconds_since_startup().sin() as f32 - 400.0; - transform.translation.y = 100.0 * time.seconds_since_startup().cos() as f32; + transform.translation.x = 100.0 * time.seconds_since_startup().sin() - 400.0; + transform.translation.y = 100.0 * time.seconds_since_startup().cos(); } } @@ -104,7 +104,7 @@ fn animate_rotation( mut query: Query<&mut Transform, (With, With)>, ) { for mut transform in query.iter_mut() { - transform.rotation = Quat::from_rotation_z(time.seconds_since_startup().cos() as f32); + transform.rotation = Quat::from_rotation_z(time.seconds_since_startup().cos()); } } @@ -116,6 +116,6 @@ fn animate_scale( // rendered quad, resulting in a pixellated look. for mut transform in query.iter_mut() { transform.translation = Vec3::new(400.0, 0.0, 0.0); - transform.scale = Vec3::splat((time.seconds_since_startup().sin() as f32 + 1.1) * 2.0); + transform.scale = Vec3::splat((time.seconds_since_startup().sin() + 1.1) * 2.0); } } diff --git a/examples/3d/manual_gltf_animation_player.rs b/examples/3d/manual_gltf_animation_player.rs index efd4b03210a7a..14534715052e7 100644 --- a/examples/3d/manual_gltf_animation_player.rs +++ b/examples/3d/manual_gltf_animation_player.rs @@ -19,7 +19,7 @@ fn main() { .add_startup_system(setup) .add_system_set( SystemSet::new() - .with_run_criteria(FixedTimestep::step(10.0)) + .with_run_criteria(FixedTimestep::step) .with_system(switch_scene), ) .add_system(setup_scene_once_loaded) @@ -82,15 +82,19 @@ struct CurrentScene { } struct CurrentAnimation { - start_time: f64, + start_time: f32, animation: GltfAnimation, } fn setup( mut commands: Commands, + mut time: ResMut, asset_server: Res, mut scene_spawner: ResMut, ) { + // Fixed timestep + time.set_delta_seconds(10.0); + // Insert a resource with the current scene information commands.insert_resource(CurrentScene { // Its instance id, to be able to check that it's loaded @@ -174,8 +178,8 @@ fn gltf_animation_driver( time: Res