Skip to content

Commit

Permalink
make physics systems dependent on TIME_STEP
Browse files Browse the repository at this point in the history
  • Loading branch information
hymm committed Mar 15, 2021
1 parent fb19e10 commit c02bf1c
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions examples/game/breakout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use bevy::{
};

/// An implementation of the classic game "Breakout"
const TIME_STEP: f32 = 1.0 / 60.0;
fn main() {
App::build()
.add_plugins(DefaultPlugins)
Expand All @@ -15,7 +16,7 @@ fn main() {
.add_stage(
FixedUpdateStage,
SystemStage::parallel()
.with_run_criteria(FixedTimestep::step(1.0 / 60.0))
.with_run_criteria(FixedTimestep::step(TIME_STEP as f64))
.with_system(paddle_movement_system.system())
.with_system(ball_collision_system.system())
.with_system(ball_movement_system.system()),
Expand Down Expand Up @@ -62,7 +63,7 @@ fn setup(
sprite: Sprite::new(Vec2::new(120.0, 30.0)),
..Default::default()
})
.with(Paddle { speed: 8.0 })
.with(Paddle { speed: 500.0 })
.with(Collider::Paddle)
// ball
.spawn(SpriteBundle {
Expand All @@ -72,7 +73,7 @@ fn setup(
..Default::default()
})
.with(Ball {
velocity: 6.0 * Vec3::new(0.5, -0.5, 0.0).normalize(),
velocity: 400.0 * Vec3::new(0.5, -0.5, 0.0).normalize(),
})
// scoreboard
.spawn(TextBundle {
Expand Down Expand Up @@ -194,15 +195,15 @@ fn paddle_movement_system(

let translation = &mut transform.translation;
// move the paddle horizontally
translation.x += direction * paddle.speed;
translation.x += direction * paddle.speed * TIME_STEP;
// bound the paddle within the walls
translation.x = translation.x.min(380.0).max(-380.0);
}
}

fn ball_movement_system(mut ball_query: Query<(&Ball, &mut Transform)>) {
if let Ok((ball, mut transform)) = ball_query.single_mut() {
transform.translation += ball.velocity;
transform.translation += ball.velocity * TIME_STEP;
}
}

Expand Down

0 comments on commit c02bf1c

Please sign in to comment.