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

@@ -5,7 +5,7 @@ use bevy_spatial::{
};
use crate::birdoids_plugin::{
center_of_boids, Acceleration, Boid, TrackedByKdTree, Velocity
center_of_boids, velocity_of_boids, Acceleration, Boid, TrackedByKdTree, Velocity
};
const SCANRADIUS: f32 = 50.0;
@@ -154,7 +154,22 @@ fn do_scan(
);
}
},
ScannerMode::Velocity => todo!(),
ScannerMode::Velocity => {
if let Some(avg_velocity) = velocity_of_boids(
boids.iter().map(|item| {
let entity_id = item.1.unwrap_or_else(|| panic!("Entity has no ID!"));
let (_, vel, _) = boids_query.get(entity_id).unwrap_or_else(|_| panic!("Boid has no Velocity component!"));
(*vel).xy() * 50.0
})
) {
// cursor_pos.translation is already in world space, so I can skip the window -> world transform like in update_cursor()
gizmos.line_2d(
cursor_pos.translation.xy(),
cursor_pos.translation.xy() + avg_velocity,
bevy::color::palettes::css::RED
);
}
},
}
},
}