parry icon indicating copy to clipboard operation
parry copied to clipboard

Bounding sphere merge sometimes results in non including both shapes

Open ThierryBerger opened this issue 1 year ago • 0 comments

Due to float imprecisions, we can't be certain a call to bounding1.merged(bounding2) will results in a bounding containing both shapes (or contains might return false for 1 or both shapes).

Reproduction

        let cube1_pos = Vec2::new(3.75, 2.6516504);
        let cube1_pos = Isometry2::translation(cube1_pos.x, cube1_pos.y);
        let cube2_pos = Isometry2::identity();
        dbg!(cube1_pos.translation);
        dbg!(cube2_pos.translation);

        let bounding_sphere_cube1 = cube1.bounding_sphere(&cube1_pos);
        let bounding_sphere_cube2 = cube2.bounding_sphere(&cube2_pos);

        // Merge the two spheres.
        let bounding_bounding_sphere = dbg!(bounding_sphere_cube1.merged(&bounding_sphere_cube2));

        // This fails /!\
        assert!(bounding_bounding_sphere.contains(&bounding_sphere_cube2));
[crates/parry2d/examples/bounding_sphere2d.rs:31:9] cube1_pos.translation = [
    3.75,
    2.6516504,
]
[crates/parry2d/examples/bounding_sphere2d.rs:32:9] cube2_pos.translation = [
    0.0,
    0.0,
]
[crates/parry2d/examples/bounding_sphere2d.rs:38:40] bounding_sphere_cube1.merged(&bounding_sphere_cube2) = BoundingSphere {
    center: [
        1.7072396,
        1.2072008,
    ],
    radius: 3.208967,
}
thread 'main' panicked at crates/parry2d/examples/bounding_sphere2d.rs:39:9:
assertion failed: bounding_bounding_sphere.contains(&bounding_sphere_cube2)

That's why bounding.loosened() exists.

The examples shouldn't encourage this assumption:

https://github.com/dimforge/parry/blob/837291f1a051dc04e6e4ab8bc2e5b438ce66d257/crates/parry2d/examples/bounding_sphere2d.rs#L31

  • Context: https://github.com/dimforge/parry/pull/259 ; especially 359b76b9af2a441a11c4f153509a671940898480

ThierryBerger avatar Sep 04 '24 09:09 ThierryBerger