dimforge / bevy_rapier

Official Rapier plugin for the Bevy game engine.
https://rapier.rs
Apache License 2.0
1.25k stars 261 forks source link

KinematicCharacterController filter groups don't seem to filter collisions with Dynamic RigidBodies #400

Open agdt3 opened 1 year ago

agdt3 commented 1 year ago

I seem to be having an issue getting the KinematicCharacterController filter groups to work correctly. My setup is that I have a player character modeled by a position-based controller and some projectiles modeled by a dynamic rigid body. I've place both in different groups, but they still show up in the collision detection system. I've also tried ignoring all dynamic bodies using KinematicCharacterController::filter_flags but that doesn't seem to work either. Any help or hints are appreciated.

Cargo.toml

[dependencies]
bevy = { version = "0.10.1" }
bevy_rapier2d = { version = "0.21.0" }
flamegraph = "0.6.2"
rand = "0.8.5"

[workspace]
resolver = "2"

Player:

 .insert(RigidBody::KinematicPositionBased)
        .insert(Collider::cuboid(16., 16.))
        .insert(KinematicCharacterController {
            translation: Some(Vec2::new(0., 0.)),
            filter_groups: Some(CollisionGroups::new(Group::GROUP_2, Group::GROUP_2)),
            ..default()
        });

Projectiles:

 .insert(RigidBody::Dynamic)
                .insert(Collider::cuboid(8., 8.))
                .insert(CollisionGroups::new(
                    Group::GROUP_3,
                    Group::GROUP_3,
                ))
                .insert(Transform::from_xyz(
                    firing_event.starting_position.x,
                    firing_event.starting_position.y,
                    0.,
                ))
                .insert(LockedAxes::ROTATION_LOCKED)
                .insert(Velocity {
                    linvel: Vec2::new(
                        firing_event.direction.x * firing_event.speed.0,
                        firing_event.direction.y * firing_event.speed.0,
                    ),
                    angvel: 0.0,
                })
                .insert(GravityScale(0.0));

Collision detection system:

pub fn player_contact_detection_system(
    rapier_context: Res<RapierContext>,
    query: Query<Entity, With<Player>>,
) {
    let player = query.get_single();
    if let Ok(player_entity) = player {
        for contact_pair in rapier_context.contacts_with(player_entity) {
            let player_collider = if contact_pair.collider1() == player_entity {
                contact_pair.collider1()
            } else {
                contact_pair.collider2()
            };
            let other_collider: Entity = if contact_pair.collider1() == player_entity {
                contact_pair.collider2()
            } else {
                contact_pair.collider1()
            };

            println!(
                "player contact graph between player collider: {:?}, other collider: {:?}",
                player_collider, other_collider
            );
        }
    }
}

Output:

player contact graph between player collider: 1v0, other collider: 11v0
player contact graph between player collider: 1v0, other collider: 12v0
player contact graph between player collider: 1v0, other collider: 13v0
player contact graph between player collider: 1v0, other collider: 14v0
...
Vrixyz commented 4 months ago

the bugs originates from rapier: https://github.com/dimforge/rapier/issues/497 ; keeping this open until resolution.