From 3293706328fb18bf00ce1e1fad1e7f0bed5b405c Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Tue, 9 Jul 2024 17:02:14 -0500 Subject: [PATCH] Iterate over all boids instead of only the player I understand what Daniel was talking about when they referred to the movement being very rigid. https://github.com/danieldidiobalsamo/boids/blob/master/src/boids_plugin.rs#L289 Their implementation has a different main loop than mine so that the acceleration can be delayed by one frame. This way things can choose their movements without being able to observe the ones that have already completed theirs for the current frame. I haven't done that, and things are weird. I'm going to try to resolve this by moving down into an acceleration value. Apply a force, integrate at the beginning or end of the frame. --- src/birdoids_plugin.rs | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/src/birdoids_plugin.rs b/src/birdoids_plugin.rs index 7c03dc1d..bc6b5b06 100644 --- a/src/birdoids_plugin.rs +++ b/src/birdoids_plugin.rs @@ -9,7 +9,7 @@ const BACKGROUND_COLOR: Color = Color::srgb(0.4, 0.4, 0.4); 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 = 1.0; +const COHESION_FACTOR: f32 = 0.1; pub struct BoidsPlugin; impl Plugin for BoidsPlugin { @@ -156,24 +156,27 @@ fn check_keyboard( fn cohesion( spatial_tree: Res>, - mut query: Query<(&Transform, &mut Velocity), With>, - player: Query<&Transform, With>, + boids: Query<&Transform, With>, + mut velocities: Query<&mut Velocity, With>, ) { - let player_transform = player.get_single().unwrap(); - - let neighbors = spatial_tree.within_distance( - player_transform.translation.xy(), - BOID_VIEW_RANGE - ); - - if neighbors.len() > 0 { - let center_of_mass = neighbors.iter() - .map(|(pos, _opt_entity)| pos ) - .sum::() / neighbors.len() as f32; - - for (transform, mut velocity) in &mut query { - let towards = (center_of_mass - transform.translation.xy()).normalize(); - **velocity += towards.extend(0.0) * COHESION_FACTOR; - } + let it = boids.iter() + .map(|transform| { + let neighbors = spatial_tree.within_distance( + transform.translation.xy(), + BOID_VIEW_RANGE + ); + if neighbors.len() > 0 { + let center_of_mass = neighbors.iter() + .map(|(pos, _opt_entity)| pos ) + .sum::() / neighbors.len() as f32; + + for mut velocity in &mut velocities { + let towards = (center_of_mass - transform.translation.xy()).normalize(); + **velocity += towards.extend(0.0) * COHESION_FACTOR; + } + } + }); + for _ in it{ + continue; } }