Skip to content
This repository has been archived by the owner on Nov 29, 2022. It is now read-only.

Crash in 3d when the collision shape has no depth #255

Open
fgadaleta opened this issue Apr 25, 2022 · 14 comments
Open

Crash in 3d when the collision shape has no depth #255

fgadaleta opened this issue Apr 25, 2022 · 14 comments
Labels
to-investigate This maybe a bug... but needs investigation

Comments

@fgadaleta
Copy link

fgadaleta commented Apr 25, 2022

Running this demo (no changes made) https://github.com/jcornaz/heron/blob/main/examples/demo.rs

I get this

thread 'Compute Task Pool (0)' panicked at 'assertion failed: proxy.aabb.maxs[dim] >= self.min_bound', /home/frag/.cargo/registry/src/github.com-1ecc6299db9ec823/rapier3d-0.11.1/src/geometry/broad_phase_multi_sap/sap_axis.rs:55:13

@jcornaz jcornaz added the bug Something isn't working label Apr 25, 2022
@jcornaz
Copy link
Owner

jcornaz commented Apr 25, 2022

I don't reproduce it yet.

Can you please provide more information? The exact commit at which you tried. What is your OS, rust version, and rendering backend?

Can you share the full call stack?

Don't hesitate to provide any additional information you can think of.

@jcornaz jcornaz added the need-reproducer Not yet clear how to reproduce the problem label Apr 25, 2022
@fgadaleta
Copy link
Author

I made the ball collide with the ground and it crashes
Please find it here https://github.com/fgadaleta/herontest

@jcornaz
Copy link
Owner

jcornaz commented Apr 25, 2022

I see here that you are using the 3d version.

But that example is a 2d example that creates collision shapes without any depth:

And rapier (not heron) crashes when the collision shape has a volume <= 0.

To solve your problem you can either:

  • Use non-zero depth for the collision shapes
  • Use the 2d feature flag instead of the 3d

@jcornaz jcornaz removed need-reproducer Not yet clear how to reproduce the problem bug Something isn't working labels Apr 25, 2022
@jcornaz jcornaz changed the title Demo crash with RigidBody::Dynamic Crash in 3d when the collision shape has no depth Apr 25, 2022
@jcornaz
Copy link
Owner

jcornaz commented Apr 25, 2022

I leave this issue open for discussion about how we could improve the situation.

On top of my head I think we could:

  • ask rapier if it would be possible to not crash for empty volumes
  • fail faster, when the shape is created, rather than when it collides
  • force the user to deal with shape creation failure by having the factory methods return a Result<CollisionShape, Error>
  • document better and warn the user about this limitation

@jcornaz jcornaz added the design label Apr 25, 2022
@fgadaleta
Copy link
Author

That also happens in 3D where there should be a volume already. For the record, this crashes too:

                    commands
                    .entity(entity)
                    .insert_bundle((transform, GlobalTransform::default()))
                    .insert(RigidBody::Dynamic)
                    .insert(CollisionShape::Sphere { radius: 2.7 })
                    .insert(PhysicMaterial {
                        restitution: 0.7,
                        density: 1.0,
                        ..Default::default()
                    })
                    .insert(CollisionLayers::new(Layer::Player, Layer::Player))
                    .insert(Velocity::from(Vec2::X * 300.0)
                            .with_angular(AxisAngle::new(Vec3::Y, -3.14)))
                    .insert(Drone);

@jcornaz
Copy link
Owner

jcornaz commented Apr 25, 2022

That also happens in 3D where there should be a volume already. For the record, this crashes too:

This is only the ball, you also need to give a depth to the ground.

@fgadaleta
Copy link
Author

I see here that you are using the 3d version.

But that example is a 2d example that creates collision shapes without any depth:

* https://github.com/fgadaleta/herontest/blob/main/src/main.rs#L37

* https://github.com/fgadaleta/herontest/blob/main/src/main.rs#L63

And rapier (not heron) crashes when the collision shape has a volume <= 0.

To solve your problem you can either:

* Use non-zero depth for the collision shapes

* Use the `2d` feature flag instead of the `3d`

you are right. Changing flag to 2d does not crash and it actually works (the green ball crashes but in a good way lol)

@fgadaleta
Copy link
Author

In my example I spawn a dynamic body like so:

                    commands
                    .entity(entity)
                    .insert_bundle((transform, GlobalTransform::default()))
                    .insert(RigidBody::Dynamic)
                    .insert(CollisionShape::Cuboid {
                        half_extends: Vec3::new(1., 1., 1.),
                        border_radius: Some(1.),
                    })
                    .insert(PhysicMaterial {
                        restitution: 0.7,
                        density: 1.0,
                        ..Default::default()
                    })
                    .insert(CollisionLayers::new(Layer::Player, Layer::Player))
                    .insert(Velocity::from(Vec2::X * 300.0)
                            .with_angular(AxisAngle::new(Vec3::Y, -3.14)))
                    .insert(Drone);

and all the rest is static, like so:

                commands
                .entity(entity)
                .insert(RigidBody::Static)
                .insert(CollisionShape::Cuboid {
                    half_extends: Vec3::new(1., 1., 1.),
                    border_radius: Some(1.),
                })
                .insert(PhysicMaterial {
                    restitution: 30.,
                    friction: 1.0,
                    density: 100.0,
                    ..Default::default()
                })
                .insert(CollisionLayers::new(Layer::World, Layer::World))
                ;

@fgadaleta
Copy link
Author

That also happens in 3D where there should be a volume already. For the record, this crashes too:

This is only the ball, you also need to give a depth to the ground.

    // The ground
    let size = Vec2::new(1000.0, 50.0);
    commands
        // Spawn a bundle that contains at least a `GlobalTransform`
        .spawn_bundle(SpriteBundle {
            sprite: Sprite {
                color: Color::WHITE,
                custom_size: Some(size),
                ..Default::default()
            },
            transform: Transform::from_translation(Vec3::new(0.0, -300.0, 0.0)),
            ..Default::default()
        })
        // Make it a rigid body
        .insert(RigidBody::Static)
        // Attach a collision shape
        .insert(CollisionShape::Cuboid {
            half_extends: size.extend(2.5) / 2.0,
            border_radius: None,
        })
        // Define restitution (so that it bounces)
        .insert(PhysicMaterial {
            restitution: 0.5,
            ..Default::default()
        });

    // The Ball
    let size = Vec2::new(30.0, 30.0);
    commands
        // Spawn a bundle that contains at least a `GlobalTransform`
        .spawn_bundle(SpriteBundle {
            sprite: Sprite {
                color: Color::GREEN,
                custom_size: Some(size),
                ..Default::default()
            },
            transform: Transform::from_translation(Vec3::new(-400.0, 200.0, 0.0)),
            ..Default::default()
        })
        // Make it a rigid body
        .insert(RigidBody::Dynamic)
        // Attach a collision shape
        .insert(CollisionShape::Cuboid {
            half_extends: size.extend(2.5) / 2.0,
            border_radius: None,
        })
        // Add an initial velocity. (it is also possible to read/mutate this component later)
        .insert(Velocity::from(Vec2::Y * -300.0).with_angular(AxisAngle::new(Vec3::Z, -PI)))
        // Define restitution (so that it bounces)
        .insert(PhysicMaterial {
            restitution: 0.7,
            ..Default::default()
        });

Collision is logged but ball disappears from screen (without actually colliding).
Using feature = ['3d'] here
2d in Cargo.toml works better (kinda)

@jcornaz
Copy link
Owner

jcornaz commented Apr 25, 2022

Collision is logged but ball disappears from screen (without actually colliding).

Due to the fast angular velocity, the body gets moved along the z-axis. I am not sure of the exact reason, but that is probably due to f32 imprecision in rapier's matrix multiplications.

Nevertheless, it does not crash. And if you remove the angular velocity or increase the depth of the ground it interacts correctly. Though it will always look strange, because these are 2d sprites allowed to move and rotate along the z-axis

And anyway, the demo is a 2d example, so I think it is pointless to try making it good in 3d. Though, I acknowledge, that we should clarify that the demo is not meant to run as-is in 3d.

@fgadaleta
Copy link
Author

Collision is logged but ball disappears from screen (without actually colliding).

Due to the fast angular velocity, the body gets moved along the z-axis. I am not sure of the exact reason, but that is probably due to f32 imprecision in rapier's matrix multiplications.

Nevertheless, it does not crash. And if you remove the angular velocity or increase the depth of the ground it interacts correctly. Though it will always look strange, because these are 2d sprites allowed to move and rotate along the z-axis

And anyway, the demo is a 2d example, so I think it is pointless to try making it good in 3d. Though, I acknowledge, that we should clarify that the demo is not meant to run as-is in 3d.

you are right. The demo is meant to be a 2d.
In my other reply I have been mentioning the 3d scenario, in which I have exactly the same problem.
Apologies for the confusion

@jcornaz
Copy link
Owner

jcornaz commented Apr 25, 2022

In my other reply I have been mentioning the 3d scenario, in which I have exactly the same problem.

Can you please provide more information? At least a full reproducer, and the full stack trace you get?

@fgadaleta
Copy link
Author

Absolutely.
Check https://gitlab.com/fgadaleta/bevy_experiment (gltf included from LFS)

If you just cargo run you will see it crashes.
If you change src/world/level1.rs:1148 to RigidBody::Static it will run.

Let me know if there's something missing as this is a repo to reproduce the error only

@jcornaz jcornaz added to-investigate This maybe a bug... but needs investigation and removed design labels May 19, 2022
@bit-garden
Copy link

We should have a note encouraging to check Rapiers common mistakes article, or a friendlier way to hint to what's wrong. this had the answer to start with. A 3d object with 0 in one dimension has 0 mass/volume.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
to-investigate This maybe a bug... but needs investigation
Projects
None yet
Development

No branches or pull requests

3 participants