#Can't make collisions work with bevy_rapier2d

3 messages · Page 1 of 1 (latest)

storm scaffold
#

Hi all. So I have controllable character and tree, and I would like the character to collide with the tree. But when I add colliders, the two objects won't collide but they overlap. Even with RapierDebugRenderPlugin turned on the collider boxes overlap. I tried adding different types of RigidBody to both objects, and only when one of the objects is RigidBody::Dynamic, then collision happens. But I don't want my objects to be RigidBody::Dynamic. Does anyone have idea what is happening? Here is my code:

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
    commands.spawn(Camera2dBundle::default());

    let texture = asset_server.load("punk.png");

    commands
        .spawn((
            SpriteBundle {
                sprite: Sprite {
                    custom_size: Some(Vec2::new(100.0, 100.0)),
                    ..default()
                },
                texture,
                transform: Transform::from_xyz(100.0, 100.0, 0.0),
                ..default()
            },
            Player,
        ))
        .insert(Collider::cuboid(50.0, 50.0));
}

fn spawn_tree(mut commands: Commands, asset_server: Res<AssetServer>) {
    let texture = asset_server.load("tree.png");

    commands
        .spawn(SpriteBundle {
            sprite: Sprite {
                custom_size: Some(Vec2::new(100.0, 100.0)),
                ..default()
            },
            texture,
            ..default()
        })
        .insert(RigidBody::Fixed)
        .insert(Collider::cuboid(50.0, 50.0));
}
proper crown
#

Relevant parts from the docs https://rapier.rs/docs/user_guides/bevy_plugin/rigid_bodies

RigidBodyType::Fixed: Indicates the body cannot move. It acts as if it has an infinite mass and will not be affected by any force. It will continue to collide with dynamic bodies but not with fixed nor with kinematic bodies. This is typically used for the ground or for temporarily freezing a body.

RigidBodyType::KinematicPositionBased: Indicates that the body position must not be altered by the physics engine. The user is free to set its next position and the body velocity will be deduced at each update accordingly to ensure a realistic behavior of dynamic bodies in contact with it. This is typically used for moving platforms, elevators, etc.

--

The whole point of kinematic bodies is to let the user have total control over their trajectory. This means that kinematic bodies will simply ignore any contact force and go through walls and the ground. In other words: if you tell the kinematic to go somewhere, it will go there, no questions asked.

Taking obstacles into account needs to be done manually either by using scene queries to detect nearby obstacles, or by using the built-in character controller.

So I think if you don't want to use dynamic rigidbody, you need to do collision testing for movement manually: https://rapier.rs/docs/user_guides/bevy_plugin/scene_queries

The real-time simulation of rigid-bodies subjected to forces and contacts is the main feature of a physics engine for

Scene queries are geometric queries that take all the colliders of the physics world into account. These queries are

storm scaffold
#

Interesting, I would think collisions like this would work out of the box. So by documentation you posted, using built-in character controller would solve my problem right?