From 368d6c920166f83f943717f290a9fea5962f37e4 Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Wed, 10 Jul 2024 10:34:16 -0500 Subject: [PATCH] Boids clumping (try for separation function) I've updated the position integration function to account for acceleration. I need to change it once more to represent force, instead. The idea is that the steering functions can all do their thing and apply a force to the boid. Then, all mobs have their acceleration (forces) integrated up through velocity and into their new position. This version produces clumps of fast moving boids. Boids out on their own will slowly come to a stop. I'm not sure why either of these is the case. --- src/birdoids_plugin.rs | 57 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/src/birdoids_plugin.rs b/src/birdoids_plugin.rs index 7064da41..bb2e18af 100644 --- a/src/birdoids_plugin.rs +++ b/src/birdoids_plugin.rs @@ -10,6 +10,8 @@ const PLAYERBOID_COLOR: Color = Color::srgb(1.0, 0.0, 0.0); const TURN_FACTOR: f32 = 1.; const BOID_VIEW_RANGE: f32 = 50.0; const COHESION_FACTOR: f32 = 0.1; +const SEPARATION_FACTOR: f32 = 10.; + pub struct BoidsPlugin; impl Plugin for BoidsPlugin { @@ -25,7 +27,8 @@ impl Plugin for BoidsPlugin { apply_velocity, turn_if_edge, check_keyboard, - cohesion, + // cohesion, + separation, )); } } @@ -41,6 +44,9 @@ struct PlayerBoid; #[derive(Component, Deref, DerefMut)] struct Velocity(Vec3); +#[derive(Component, Deref, DerefMut)] +struct Acceleration(Vec3); + #[derive(Component)] struct TrackedByKdTree; @@ -48,6 +54,7 @@ struct TrackedByKdTree; struct BoidBundle { boid: Boid, velocity: Velocity, + accel: Acceleration, spatial: TrackedByKdTree, } @@ -56,6 +63,7 @@ impl BoidBundle { Self { boid: Boid, velocity: Velocity(vel), + accel: Acceleration(Vec3::ZERO), spatial: TrackedByKdTree, } } @@ -120,9 +128,13 @@ fn turn_if_edge( } } -fn apply_velocity(mut query: Query<(&mut Transform, &Velocity)>, time: Res