boundary conditions

This commit is contained in:
Zynh Ludwig 2024-09-01 21:04:22 -07:00
parent 5561191864
commit 901a936e18

View file

@ -8,7 +8,7 @@ fn main() {
.init_resource::<BoidMesh>() .init_resource::<BoidMesh>()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_systems(Startup, (boid_mesh::build_boid_mesh, setup).chain()) .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(); .run();
} }
@ -31,6 +31,40 @@ fn move_boids(mut boids: Query<(&mut Transform, &Velocity), With<Boid>>, time: R
} }
} }
fn boid_border_teleport(mut boids: Query<&mut Transform, With<Boid>>, 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( fn setup(
mut commands: Commands, mut commands: Commands,
mut materials: ResMut<Assets<ColorMaterial>>, mut materials: ResMut<Assets<ColorMaterial>>,