Search code examples
rustbevyrapier-2d

Adding collider to floor on bevy and bevy_rapier2d


I try to add a collider to the floor of my 2d game:

fn setup(
    mut commands: Commands,
    asset_server: Res<AssetServer>,
) {
    let vertices = [
        Vec2::new(-WINDOW_WIDTH, 0.0),
        Vec2::new(WINDOW_WIDTH, 0.0),
    ];

    commands
        .spawn(SpriteBundle {
            transform: Transform {
                translation: Vec3::new(0.0, WINDOW_BOTTOM_Y  + (FLOOR_THICKNESS / 2.0), 0.0),
                scale: Vec3::new(BG_WIDTH, FLOOR_THICKNESS, 1.0),
                ..Default::default()
            },
            ..Default::default()
        })
        .insert(RigidBody::Fixed)
        .insert(Collider::convex_polyline(&vertices)); // This line generates an error

   //...
}

I know that I can use a cuboid collider for this polyline example but I want to start with a simple polyline to after complexify it.

The problem is that when I insert this polyline collider I have this error:

error[E0277]: the trait bound `std::option::Option<bevy_rapier2d::geometry::Collider>: bevy::prelude::Component` is not satisfied

When I insert a simple cuboid collider I dont have any error:

.insert(Collider::cuboid(0.5, 0.5));

Solution

  • OK the problem was because i used convex_polyline and not polyline like this:

    .insert(Collider::polyline(&vertices));