Skip to content

Commit

Permalink
update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
maniwani committed Jun 24, 2023
1 parent eb3c364 commit e41c567
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 31 deletions.
12 changes: 7 additions & 5 deletions examples/2d/rotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

use bevy::{math::Vec3Swizzles, prelude::*};

const TIME_STEP: f32 = 1.0 / 60.0;
const BOUNDS: Vec2 = Vec2::new(1200.0, 640.0);

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.insert_resource(FixedTime::new_from_secs(TIME_STEP))
.insert_resource(FixedTimestep::from_hz(60.0))
.add_systems(Startup, setup)
.add_systems(
FixedUpdate,
Expand Down Expand Up @@ -117,6 +116,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {

/// Demonstrates applying rotation and movement based on keyboard input.
fn player_movement_system(
time: Res<Time>,
keyboard_input: Res<Input<KeyCode>>,
mut query: Query<(&Player, &mut Transform)>,
) {
Expand All @@ -138,12 +138,12 @@ fn player_movement_system(
}

// update the ship rotation around the Z axis (perpendicular to the 2D plane of the screen)
transform.rotate_z(rotation_factor * ship.rotation_speed * TIME_STEP);
transform.rotate_z(rotation_factor * ship.rotation_speed * time.delta_seconds());

// 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
Expand Down Expand Up @@ -198,6 +198,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<Time>,
mut query: Query<(&RotateToPlayer, &mut Transform), Without<Player>>,
player_query: Query<&Transform, With<Player>>,
) {
Expand Down Expand Up @@ -242,7 +243,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);

// rotate the enemy to face the player
enemy_transform.rotate_z(rotation_angle);
Expand Down
36 changes: 23 additions & 13 deletions examples/ecs/fixed_timestep.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,48 @@
//! Shows how to create systems that run every fixed timestep, rather than every tick.

use bevy::prelude::*;
use bevy::time::TimeContext;

const FIXED_TIMESTEP: f32 = 0.5;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
// this system will run once every update (it should match your screen's refresh rate)
// set fixed timestep to run systems ten times a second
.insert_resource(FixedTimestep::from_hz(10.0))
// this system will run every update (this should match the screen refresh rate)
.add_systems(Update, frame_update)
// add our system to the fixed timestep schedule
// this system will run ten times a second
.add_systems(FixedUpdate, fixed_update)
// configure our fixed timestep schedule to run twice a second
.insert_resource(FixedTime::new_from_secs(FIXED_TIMESTEP))
.run();
}

fn frame_update(mut last_time: Local<f32>, time: Res<Time>) {
fn frame_update(mut last_time: Local<f32>, time: Res<Time>, real_time: Res<RealTime>) {
info!(
"time since last frame_update: {}",
time.raw_elapsed_seconds() - *last_time
real_time.elapsed_seconds() - *last_time
);
*last_time = time.raw_elapsed_seconds();

assert!(matches!(time.context(), TimeContext::Update));
*last_time = real_time.elapsed_seconds();
}

fn fixed_update(mut last_time: Local<f32>, time: Res<Time>, fixed_time: Res<FixedTime>) {
fn fixed_update(
mut last_time: Local<f32>,
time: Res<Time>,
real_time: Res<RealTime>,
fixed_timestep: Res<FixedTimestep>,
) {
assert!(matches!(time.context(), TimeContext::FixedUpdate));
assert_eq!(time.delta(), fixed_timestep.size());

info!("fixed timestep: {}\n", time.delta_seconds());
info!(
"time since last fixed_update: {}\n",
time.raw_elapsed_seconds() - *last_time
real_time.elapsed_seconds() - *last_time
);

info!("fixed timestep: {}\n", FIXED_TIMESTEP);
info!(
"time accrued toward next fixed_update: {}\n",
fixed_time.accumulated().as_secs_f32()
fixed_timestep.overstep().as_secs_f32()
);
*last_time = time.raw_elapsed_seconds();
*last_time = real_time.elapsed_seconds();
}
11 changes: 5 additions & 6 deletions examples/ecs/iter_combinations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
use bevy::{pbr::AmbientLight, prelude::*};
use rand::{thread_rng, Rng};

const DELTA_TIME: f32 = 0.01;

fn main() {
App::new()
.add_plugins(DefaultPlugins)
Expand All @@ -13,7 +11,7 @@ fn main() {
..default()
})
.insert_resource(ClearColor(Color::BLACK))
.insert_resource(FixedTime::new_from_secs(DELTA_TIME))
.insert_resource(FixedTimestep::from_hz(100.0))
.add_systems(Startup, generate_bodies)
.add_systems(FixedUpdate, (interact_bodies, integrate))
.add_systems(Update, look_at_star)
Expand Down Expand Up @@ -42,6 +40,7 @@ struct BodyBundle {

fn generate_bodies(
mut commands: Commands,
time: Res<Time>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
Expand Down Expand Up @@ -96,7 +95,7 @@ fn generate_bodies(
rng.gen_range(vel_range.clone()),
rng.gen_range(vel_range.clone()),
rng.gen_range(vel_range.clone()),
) * DELTA_TIME,
) * time.delta_seconds(),
),
});
}
Expand Down Expand Up @@ -160,8 +159,8 @@ fn interact_bodies(mut query: Query<(&Mass, &GlobalTransform, &mut Acceleration)
}
}

fn integrate(mut query: Query<(&mut Acceleration, &mut Transform, &mut LastPos)>) {
let dt_sq = DELTA_TIME * DELTA_TIME;
fn integrate(time: Res<Time>, mut query: Query<(&mut Acceleration, &mut Transform, &mut LastPos)>) {
let dt_sq = time.delta_seconds() * time.delta_seconds();
for (mut acceleration, mut transform, mut last_pos) in &mut query {
// verlet integration
// x(t+dt) = 2x(t) - x(t-dt) + a(t)dt^2 + O(dt^4)
Expand Down
12 changes: 6 additions & 6 deletions examples/games/breakout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn main() {
.insert_resource(ClearColor(BACKGROUND_COLOR))
.add_event::<CollisionEvent>()
// Configure how frequently our gameplay systems are run
.insert_resource(FixedTime::new_from_secs(1.0 / 60.0))
.insert_resource(FixedTimestep::from_hz(60.0))
.add_systems(Startup, setup)
// Add our gameplay simulation systems to the fixed timestep schedule
.add_systems(
Expand Down Expand Up @@ -311,7 +311,7 @@ fn setup(
fn move_paddle(
keyboard_input: Res<Input<KeyCode>>,
mut query: Query<&mut Transform, With<Paddle>>,
time_step: Res<FixedTime>,
time: Res<Time>,
) {
let mut paddle_transform = query.single_mut();
let mut direction = 0.0;
Expand All @@ -326,7 +326,7 @@ fn move_paddle(

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

// Update the paddle position,
// making sure it doesn't cause the paddle to leave the arena
Expand All @@ -336,10 +336,10 @@ fn move_paddle(
paddle_transform.translation.x = new_paddle_position.clamp(left_bound, right_bound);
}

fn apply_velocity(mut query: Query<(&mut Transform, &Velocity)>, time_step: Res<FixedTime>) {
fn apply_velocity(mut query: Query<(&mut Transform, &Velocity)>, time: Res<Time>) {
for (mut transform, velocity) in &mut query {
transform.translation.x += velocity.x * time_step.period.as_secs_f32();
transform.translation.y += velocity.y * time_step.period.as_secs_f32();
transform.translation.x += velocity.x * time.delta_seconds();
transform.translation.y += velocity.y * time.delta_seconds();
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/stress_tests/bevymark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn main() {
counter_system,
),
)
.insert_resource(FixedTime::new_from_secs(0.2))
.insert_resource(FixedTimestep::from_hz(60.0))
.run();
}

Expand Down

0 comments on commit e41c567

Please sign in to comment.