Completed velocity pointer gizmo

Right-clicking toggles velocity sensing mode and the cursor gets a line
that indicates the average velocity of the targeted points.

There is a coefficient of 50.0 on the pointer magnitude. The boids move
so slowly that the line is very short without this magnification.

There is also something wrong with the math. The boids expand and slow
down, but the velocity vector does not reflect this. It *grows* slightly
and then stays at that size as the boids slowly approach a stop.

When writing the average velocity function, I realized that it is
**exactly** the same as the center of mass function. They're both just
averaging a bunch of points. Both functions are implemented as pass-
through calls to a general purpose Vec2 averaging function.

The `impl Iterator<...>...` *is* actually necessary because of the use
of `map()`s to filter out the boids parts from the spatial tree.
This commit is contained in:
2024-07-11 14:46:36 -05:00
parent ac25a41f77
commit 336992573e
2 changed files with 25 additions and 2 deletions

View File

@@ -208,6 +208,14 @@ fn cohesion(
}
pub(crate) fn center_of_boids(points: impl Iterator<Item = Vec2>) -> Option<Vec2> {
average_of_vec2s(points)
}
pub(crate) fn velocity_of_boids(points: impl Iterator<Item = Vec2>) -> Option<Vec2> {
average_of_vec2s(points)
}
fn average_of_vec2s(points: impl Iterator<Item = Vec2>) -> Option<Vec2> {
// Average the points by summing them all together, and dividing by
// the total count.
// Passing the points as an iterator means we lose the length of the