Rust: bevy_rapier2d 0.22.0 doesn't detect obvious collisions

Expectations

I would like to see the following code example spamming "Received collision event: ..." in stdout.

Result

After cargo run you will see two red squares representing colliders of 2 entities. One is hero, the other is enemy.
I believe there's a collision between them as one collider is inside the other.
Unfortunately, bevy_rapier2d doesn't seem to produce any collision events.

Code example

use {bevy::prelude::*, bevy_rapier2d::prelude::*};

fn main() {
    App::new()
        .add_plugins((
            DefaultPlugins,
            RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(100.),
            RapierDebugRenderPlugin::default(),
        ))
        .add_systems(Startup, (spawn_camera, spawn_hero, spawn_enemy))
        .add_systems(Update, (detect_collisions,))
        .run()
}

fn spawn_camera(mut cmds: Commands) {
    cmds.spawn((Camera2dBundle::default(),));
}
fn spawn_hero(mut cmds: Commands) {
    cmds.spawn((
        TransformBundle::default(),
        Collider::cuboid(100., 100.),
        ActiveEvents::COLLISION_EVENTS, // Hero collisions are to always have effect.
        Sensor,
    ));
}
fn spawn_enemy(mut cmds: Commands) {
    cmds.spawn((
        TransformBundle::default(),
        Collider::cuboid(70., 70.),
        // ActiveEvents::COLLISION_EVENTS,
        /* Enemy may collide with other enemies which should have
        no effect */
        Sensor,
    ));
}
/// In future this func. will be replaced with real collision handling (eg. player takes demage)
pub fn detect_collisions(mut collision_events: EventReader<CollisionEvent>) {
    println!("`detect_collisions` system tries to find collisions...");
    for collision_event in collision_events.iter() {
        println!("Received collision event: {:?}", collision_event);
    }
}

I copied the Cargo.toml from my original game to ensure that my bug is reproduced in the same environment.

[package]
name = "bevy_template"
version = "0.1.0"
edition = "2021"

[dependencies]
bevy = {version="0.11", features=["dynamic_linking"]}
bevy-inspector-egui = "0.19.0"
bevy_rapier2d = "0.22.0"
derive_more = "0.99.17"

[profile.dev]
opt-level = 1
[profile.dev.package."*"]
opt-level = 3

From surveying

I understood that Sensors with ActiveEvents::COLLISION_EVENTS should always generate CollisionEvents when one Collider is touching another.

Full scenario

In my game, there's an enemy that slowly moves toward the hero and their bodies start to overlap, but no event is produced at any phase of their convergence.

This Bevy example code may help:

Unfortunately, this is just an implementation of a simplified collision system/engine.
But what I want is a general purpose, simple and efficient collision engine that can handle 10000 collisions.
I thought bevy_rapier2d would come up to my expectations.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.