dimforge / rapier

2D and 3D physics engines focused on performance.
https://rapier.rs
Apache License 2.0
3.99k stars 249 forks source link

Removing a collider and stepping causes a panic in 0.7 #163

Closed Demindiro closed 3 years ago

Demindiro commented 3 years ago

Removing a collider and stepping causes a panic:

thread 'main' panicked at 'No element at index', /home/david/.cargo/registry/src/github.com-1ecc6299db9ec823/rapier3d-0.7.1/src/geometry/collider_set.rs:142:17

It seems to be caused by the recent change that tracks modifications to colliders:

https://github.com/dimforge/rapier/blob/7557d2a6ee20488835998c8aef4541ed5ada576f/src/geometry/collider_set.rs#L133-L145

Reproduction project:

// src/main.rs

use rapier3d::pipeline::*;
use rapier3d::dynamics::*;
use rapier3d::geometry::*;
use rapier3d::na::*;

fn main() {

    let mut pipeline = PhysicsPipeline::new();
    let gravity = Vector3::new(0.0, -9.81, 0.0);
    let integration_parameters = IntegrationParameters::default();
    let mut broad_phase = BroadPhase::new();
    let mut narrow_phase = NarrowPhase::new();
    let mut bodies = RigidBodySet::new();
    let mut colliders = ColliderSet::new();
    let mut ccd = CCDSolver::new();
    let mut joints = JointSet::new();
    let physics_hooks = ();
    let event_handler = ();

    let body = RigidBodyBuilder::new_dynamic().translation(0.0, 1.01, 0.0).build();
    let b_handle = bodies.insert(body);
    let collider = ColliderBuilder::ball(1.0).build();
    let c_handle = colliders.insert(collider, b_handle, &mut bodies);
    colliders.remove(c_handle, &mut bodies, true);
    bodies.remove(b_handle, &mut colliders, &mut joints); // Happens with and without this line

    loop {
        pipeline.step(
            &gravity,
            &integration_parameters,
            &mut broad_phase,
            &mut narrow_phase,
            &mut bodies,
            &mut colliders,
            &mut joints,
            &mut ccd,
            &physics_hooks,
            &event_handler,
        );
    }
}
# Cargo.toml

[package]
name = "rapier3d_removed_collider_modify_panic"
version = "0.1.0"
authors = ["David Hoppenbrouwers <david@salt-inc.org>"]
edition = "2018"

[dependencies]
rapier3d = "*"
sebcrozet commented 3 years ago

Thank you for pointing this out, and for the test case! This will be fixed by #164.

sebcrozet commented 3 years ago

The fix for this issue is now released in the version 0.7.2.