I'm wanting to draw debug AABBs for each of my collidable entities. I'm trying to use bevy_polyline for this purpose. I'm struggling to understand the ECS way of connecting the vertices of the polyline with the transform of a given entity.
I wasn't quite sure how best to approach this, so I kind of blindly fumbled.
My initial approach was to create a component:
#[derive(Component)]
struct AABBLink(Handle<Polyline>);
and add it to the entities whose AABBs I wanted to draw.
I don't understand Assets that well, but it seemed like in the bevy_polyline examples there would always be a ResMut<Assets<Polyline>> where all the polylines were stored, so when I was creating each entity, I could add a corresponding polyline to the assets and get a Handle<Polyline> back. Then I figured I could store the handle in a component on that entity, and have a system update the vertices of the polyline, something like this:
pub fn update_aabb_line_system(
commands: Commands,
q: Query<(&Transform, &AABBLink)>,
mut polylines: ResMut<Assets<Polyline>>,
) {
for (transform, AABBLink(handle)) in q.iter() {
let top_left: Vec3 = transform.translation;
let top_right: Vec3 = top_left + transform.scale.x * Vec3::X;
let bottom_left: Vec3 = top_left + transform.scale.y * Vec3::Y;
let bottom_right: Vec3 = top_left + transform.scale;
if let Some(&mut polyline) = polylines.get_mut(handle) {
polyline.vertices = vec![top_left, top_right, bottom_right, bottom_left, top_left ];
}
}
}
However, as experienced rustaceans have no doubt already realised, this ran into issues with the borrow checker when I tried to create the AABBLink and insert it on the entities. This makes perfect sense - I can't guarantee the compiler that the Handle<Polyline will remain valid.
So what is the proper ECS way to approach this?