Braking function to slow all boids

This will help during debugging to prevent things from screaming around
the play area. Especially when the acceleration routines are unbounded
and can infinitely add energy to the system.
This commit is contained in:
2024-07-10 16:27:21 -05:00
parent 7eb65df7cf
commit d99326d849

View File

@@ -11,6 +11,7 @@ const TURN_FACTOR: f32 = 1.0;
const BOID_VIEW_RANGE: f32 = 50.0;
const COHESION_FACTOR: f32 = 1.0;
const SEPARATION_FACTOR: f32 = 1.0;
const ALIGNMENT_FACTOR: f32 = 1.0;
pub struct BoidsPlugin;
@@ -27,8 +28,10 @@ impl Plugin for BoidsPlugin {
apply_velocity,
turn_if_edge,
check_keyboard,
cohesion,
// cohesion,
separation,
// alignment,
space_brakes,
));
}
}
@@ -78,10 +81,10 @@ fn spawn_boids(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let num_boids = 1000;
let num_boids = 50;
for i in 0..num_boids {
let frac = 2.0 * std::f32::consts::PI / (num_boids as f32) * (i as f32);
let vel = Vec3::new(frac.cos() * 10.0, frac.sin() * 10.0, 0.0);
let vel = Vec3::new(frac.cos() * 1.0, frac.sin() * 1.0, 0.0);
commands.spawn((
BoidBundle::new(vel),
MaterialMesh2dBundle {
@@ -107,6 +110,13 @@ fn spawn_boids(
));
}
fn space_brakes(mut mobs: Query<&mut Acceleration, With<Boid>>) {
for mut accel in &mut mobs {
let braking_dir = -accel.0 * 0.01;
accel.0 += braking_dir;
}
}
fn turn_if_edge(
mut query: Query<(&mut Transform, &mut Velocity), With<Boid>>,
window: Query<&Window>,