dimforge / ncollide

2 and 3-dimensional collision detection library in Rust.
https://ncollide.org
Apache License 2.0
922 stars 106 forks source link

CollisionWorld panics when adding the same trimesh shape twice #188

Closed arturoc closed 6 years ago

arturoc commented 6 years ago

This is related to #182, seems to panic in the same place. It's more of an edge case even and happens when adding a trimesh shape (probably other shapes but happen tested) twice, which didn't crashed before, like:

extern crate ncollide;
extern crate nalgebra;

use nalgebra::{Point3, Vector3, Isometry3, zero};
use ncollide::world::{CollisionWorld, GeometricQueryType, CollisionGroups};
use ncollide::bounding_volume::*;
use ncollide::shape::*;
use std::sync::Arc;

fn main() {
    let mut world: CollisionWorld<Point3<f32>, Isometry3<f32>, ()> = CollisionWorld::new(0.02);
    let groups = CollisionGroups::new();
    let contacts_query = GeometricQueryType::Contacts(0.0, 0.0);

    let vertices = vec![
        Point3::new(1.0,1.0,-1.0),
        Point3::new(-1.0,-1.0,-1.0),
        Point3::new(-1.0,1.0,-1.0),
        Point3::new(1.0,-1.0,-1.0),
        Point3::new(-1.0,-1.0,-1.0),
        Point3::new(1.0,1.0,-1.0),
        Point3::new(-1.0,1.0,1.0),
        Point3::new(-1.0,-1.0,1.0),
        Point3::new(1.0,-1.0,1.0),
        Point3::new(1.0,1.0,1.0),
        Point3::new(-1.0,1.0,1.0),
        Point3::new(1.0,-1.0,1.0),
        Point3::new(1.0,1.0,1.0),
        Point3::new(1.0,-1.0,1.0),
        Point3::new(1.0,-1.0,-1.0),
        Point3::new(1.0,1.0,-1.0),
        Point3::new(1.0,1.0,1.0),
        Point3::new(1.0,-1.0,-1.0),
        Point3::new(1.0,-1.0,1.0),
        Point3::new(-1.0,-1.0,1.0),
        Point3::new(-1.0,-1.0,-1.0),
        Point3::new(1.0,-1.0,-1.0),
        Point3::new(1.0,-1.0,1.0),
        Point3::new(-1.0,-1.0,-1.0),
        Point3::new(-1.0,-1.0,-1.0),
        Point3::new(-1.0,1.0,1.0),
        Point3::new(-1.0,1.0,-1.0),
        Point3::new(-1.0,-1.0,1.0),
        Point3::new(-1.0,1.0,1.0),
        Point3::new(-1.0,-1.0,-1.0),
        Point3::new(1.0,1.0,-1.0),
        Point3::new(-1.0,1.0,-1.0),
        Point3::new(-1.0,1.0,1.0),
        Point3::new(1.0,1.0,1.0),
        Point3::new(1.0,1.0,-1.0),
        Point3::new(-1.0,1.0,1.0),
    ];

    let indices = vec![
        Point3::new(0,1,2),
        Point3::new(3,4,5),
        Point3::new(6,7,8),
        Point3::new(9,10,11),
        Point3::new(12,13,14),
        Point3::new(15,16,17),
        Point3::new(18,19,20),
        Point3::new(21,22,23),
        Point3::new(24,25,26),
        Point3::new(27,28,29),
        Point3::new(30,31,32),
        Point3::new(33,34,35),
    ];

    let mesh = TriMesh::new(
        Arc::new(vertices),
        Arc::new(indices),
        None,
        None
    );

    let iso = Isometry3::new(Vector3::new(0., 0., 0.), zero());
    let shape: ShapeHandle3<f32> = ShapeHandle::new(mesh.clone());
    world.add(iso, shape, groups, contacts_query, ());

    let iso = Isometry3::new(Vector3::new(0., 0., 0.), zero());
    let shape: ShapeHandle3<f32> = ShapeHandle::new(mesh);
    world.add(iso, shape, groups, contacts_query, ());

    world.update();
}

This has actually helped me figure out that i was adding some objects more than once in the collisions world it's not something i really need to do, in case you are wondering :)

sebcrozet commented 6 years ago

Closing this since the provided examples no longer seem to crash. Please reopen if the issue persists.