Refactor alignment() to use velocity_of_boids
This commit is contained in:
@@ -252,23 +252,21 @@ fn alignment(
|
|||||||
// averaging divides by length. Guard against an empty set of neighbors
|
// averaging divides by length. Guard against an empty set of neighbors
|
||||||
// so that we don't divide by zero.
|
// so that we don't divide by zero.
|
||||||
if neighbors.len() > 0 {
|
if neighbors.len() > 0 {
|
||||||
let velocity_sum = neighbors.iter()
|
if let Some(avg_velocity) = velocity_of_boids(
|
||||||
.map(|(_pos, entity_id)| {
|
neighbors.iter().map(|(vel, opt_entity)| {
|
||||||
if let Some(boid_id) = entity_id {
|
// I've observed no panics in the old version, nor the debug_plugins version
|
||||||
if let Ok(boid) = boid_velocities.get(*boid_id) {
|
// I'm not clear on the conditions that cause a None option, but I want to
|
||||||
boid.0
|
// crash when I find one.
|
||||||
} else {
|
let entity_id = opt_entity.unwrap_or_else(|| panic!("Boid has no Entity ID!"));
|
||||||
panic!("Err: Got a Boid during neighbor collection, but it has no Entity ID!");
|
let vel = boid_velocities.get(entity_id).unwrap_or_else(|_| panic!("Boid has no velocity!"));
|
||||||
}
|
(*vel).xy()
|
||||||
} else {
|
})
|
||||||
panic!("Err: Found entity with component TrackedByKdTree that has no Entity ID!");
|
) {
|
||||||
}
|
let deviation = -velocity.0 + avg_velocity.extend(0.0);
|
||||||
}).sum::<Vec3>();
|
|
||||||
let velocity_average = velocity_sum / (neighbors.len() as f32);
|
|
||||||
let deviation = -velocity.0 + velocity_average;
|
|
||||||
acceleration.0 += deviation * ALIGNMENT_FACTOR;
|
acceleration.0 += deviation * ALIGNMENT_FACTOR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user