heron icon indicating copy to clipboard operation
heron copied to clipboard

zero-length normals during RigidBody::Sensor collision detection

Open cark opened this issue 3 years ago • 2 comments

The normals vector is of 0 length when detecting a collision between Sensors. This works fine for RigidBody::Dynamic, but for my particular use case I cannot interact with the other simulated bodies.

Here is a repository with an (hopefully minimal enough) example: https://github.com/cark/HeronMissingNormals

Code reproduced here:

use bevy::prelude::*;
use heron::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugin(PhysicsPlugin::default())
        .add_startup_system(init)
        .add_system(movement)
        .add_system(collision_detection)
        .run();
}

#[derive(Component)]
struct Speed(Vec2);

fn spawn_entity(commands: &mut Commands, position: Vec2, speed: Vec2) {
    commands
        .spawn()
        .insert(Speed(speed))
        .insert(Transform::from_translation(position.extend(0.0)))
        .insert(GlobalTransform::default())
        .insert(RigidBody::Sensor)
        .insert(CollisionShape::Sphere { radius: 10.0 });
}

fn init(mut commands: Commands) {
    commands.spawn_bundle(OrthographicCameraBundle::new_2d());
    spawn_entity(&mut commands, Vec2::new(-100.0, 0.0), Vec2::new(1.0, 0.0));
    spawn_entity(&mut commands, Vec2::new(100.0, 0.0), Vec2::new(-1.0, 0.0));
}

fn movement(mut query: Query<(&Speed, &mut Transform)>) {
    for (speed, mut transform) in query.iter_mut() {
        transform.translation.x += speed.0.x;
        transform.translation.y += speed.0.y;
    }
}

fn collision_detection(mut events: EventReader<CollisionEvent>) {
    for event in events.iter() {
        if let CollisionEvent::Started(d1, d2) = event {
            info!(
                "normal count 1: {}, normal count 2: {}",
                d1.normals().len(),
                d2.normals().len()
            );
        }
    }
}

Notice the collision_detection function, I log the length of the normal vectors there.

What i hoped to see in the console:

2022-04-14T20:34:52.409381Z  INFO heron_test: normal count 1: 1, normal count 2: 1

What i actually see:

2022-04-14T20:34:52.409381Z  INFO heron_test: normal count 1: 0, normal count 2: 0

I guess I could extract those normals by querying the global transforms of the entities. But I think this might be a bug, so here I am, reporting it.

cark avatar Apr 15 '22 02:04 cark

Thanks for the report.

That's because heron on only check the contact graph. It should also check the intersection graph.

jcornaz avatar Apr 15 '22 07:04 jcornaz

Can I be assigned to this,if the bug hasn't been fixed already?

Shreyan11 avatar Jun 29 '22 10:06 Shreyan11