#Drawing bounding boxes using `bevy_polyline`

7 messages · Page 1 of 1 (latest)

past grail
#

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?

#

Difficulty drawing bounding boxes using bevy_polyline

#

Drawing bounding boxes using bevy_polyline

abstract swift
#

personally i used bevy_prototype_debug_lines for this, it has a bit of a nicer api for this.

As for your question, why store the Handle in the AABBLink component at all? nvm, i must have misremembered the way bevy_polyline is used.

#

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.

were you .clone()-ing the handle? could you show this creation code?

past grail
#

Ah, for some reason I had in my head that handle didn't implement Clone. I will play around

past grail
#

I got it compiling using clone(), but it seems like the boxes aren't actually showing up, and I'm I'm not sure why. I think I'll switch to bevy_prototype_debug_lines, it does look a lot more straightforward