#box selection

16 messages ยท Page 1 of 1 (latest)

willow pier
#

I have this struct

#[derive(Component)]
struct Selectable{
    collider: parry3d::shape::Capsule
}

which is used in that function:

let hit = parry3d::query::intersection_test(
   &selectable_position,
   &selectable.collider,
   &Isometry3::identity(),
   &collider,
);

Intersection test takes &dyn shape, so a trait object. Now I would actually like to keep it somewhat that generic when calling

commands.spawn((
    PbrBundle {
        transform: Transform::from_xyz(x, y, z),
        mesh: unit_mesh.clone(),
        material: unit_material.clone(),
        ..default()
    },
    Selectable{collider: parry3d::shape::Capsule::new(Point::new(0.0,-0.5, 0.0), Point::new(0.0, 0.5, 0.0), 0.5)},
));

But since I can't control the lifetime here, I don't see how I'd be supposed to add a trait object to the struct above (which I would like right now).
Also using generics, I would need to specify the generics in the app as I add the system too. So I really don't know how I could pass this whole thing as generic/dynamic.
Any Ideas?

#

This is the code I'm using. This is not my code, I'm just trying to change it from using bounding boxes to more exact colliders like capsules

willow pier
#

Oh and anything that might be not idiomatic is appreciated too ๐Ÿ˜„

spare night
#

I believe that the solution is to box the trait object

struct Selectable {
    collider: Box<dyn Shape>
}
willow pier
#

That's what I tried at first, but where would I initialize it, so that it has a lifetime that outlives selectable?
Creating it as a ressource seems somewhat weird, especially since I would like every entity to have a different selectable collider

spare night
#

It seems to work fine for me just creating the box when I spawn the units.

    commands.spawn((
        PbrBundle {
            transform: Transform::from_xyz(-6.0, 1.2, 8.0),
            mesh: large_unit_mesh,
            material: unit_material.clone(),
            ..default()
        },
        Selectable {
            collider: Box::new(parry3d::shape::Capsule::new(
                Point::new(0.0, -0.5, 0.0),
                Point::new(0.0, 0.5, 0.0),
                1.0,
            )),
        },
    ));
willow pier
#

Wait does Box not work like a reference to that struct?
I thought the inner struct would go out of scope right after creation?
I still have so much to learn ๐Ÿ™ˆ

#

when calling it, i get the following error:

            let selectable_collider = selectable.collider.as_shape().unwrap();

            let hit = parry3d::query::intersection_test(
                &selectable_position,
                selectable_collider,
                &Isometry3::identity(),
                &collider,
            );
error[E0277]: the size for values of type `dyn Shape` cannot be known at compilation time
   --> src\unit_selection.rs:195:59
    |
195 |             let selectable_collider = selectable.collider.as_shape().unwrap();
    |                                                           ^^^^^^^^ doesn't have a size known at compile-time
    |
    = help: the trait `Sized` is not implemented for `dyn Shape`
note: required by a bound in `<(dyn Shape + 'static)>::as_shape`
   --> C:\Users\Arne\.cargo\registry\src\index.crates.io-6f17d22bba15001f\parry3d-0.13.5\src\shape\shape.rs:362:21
    |
362 |     pub fn as_shape<T: Shape>(&self) -> Option<&T> {
    |                     ^ required by this bound in `<dyn Shape>::as_shape`

spare night
#

try selectable.collider.as_ref(), I think that's what I did when I tested it

willow pier
#

oh that makes sense, since it will not be able to statically get the size, a pointer to it will do fine, right?

#

nice! Now i have working unit selection ๐Ÿ˜„ Thanks a lot lei ๐Ÿ™‚

spare night
#

Yeah, as_ref() will turn Box<T> into &T, which is what you want since you have Box<dyn Shape> and the function takes a &dyn Shape

spare night
willow pier
#

oooh ok! so that's why normal refs wouldn't have worked

#

I would love to know, when I will feel just a bit proficient in rust ๐Ÿ˜›

spare night
#

Yep since non-boxed structs would be stack allocated and dropped at the end of the scope