From 901a936e1814d1537136fe66c8990fc2692f98df Mon Sep 17 00:00:00 2001 From: Zynh Ludwig Date: Sun, 1 Sep 2024 21:04:22 -0700 Subject: [PATCH] boundary conditions --- src/main.rs | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index a419f0e..2ae0391 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ fn main() { .init_resource::() .add_plugins(DefaultPlugins) .add_systems(Startup, (boid_mesh::build_boid_mesh, setup).chain()) - .add_systems(Update, move_boids) + .add_systems(Update, (move_boids, boid_border_teleport).chain()) .run(); } @@ -31,6 +31,40 @@ fn move_boids(mut boids: Query<(&mut Transform, &Velocity), With>, time: R } } +fn boid_border_teleport(mut boids: Query<&mut Transform, With>, windows: Query<&Window>) { + let window = windows.get_single().unwrap(); + let width = window.resolution.width(); + let height = window.resolution.height(); + + let half_width = width / 2.; + let half_height = height / 2.; + + let left_bound = -half_width; + let right_bound = half_width; + let top_bound = half_height; + let bottom_bound = -half_height; + + for boid_transform in &mut boids { + let mut translation = boid_transform.translation; + + if translation.y > top_bound { + translation.y = bottom_bound; + } + + if translation.y < bottom_bound { + translation.y = top_bound; + } + + if translation.x < left_bound { + translation.x = right_bound; + } + + if translation.x > right_bound { + translation.x = left_bound; + } + } +} + fn setup( mut commands: Commands, mut materials: ResMut>,