22
33use bevy:: { math:: Vec3Swizzles , prelude:: * } ;
44
5- const TIME_STEP : f32 = 1.0 / 60.0 ;
65const BOUNDS : Vec2 = Vec2 :: new ( 1200.0 , 640.0 ) ;
76
87fn 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.
119118fn 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`.
200200fn 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,7 @@ 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 = rotation_sign * ( config. rotation_speed * time . delta_seconds ( ) ) . min ( max_angle) ;
246247
247248 // rotate the enemy to face the player
248249 enemy_transform. rotate_z ( rotation_angle) ;
0 commit comments