dimforge / ncollide

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

interferences_with_ray usage #249

Closed seppo0010 closed 5 years ago

seppo0010 commented 5 years ago

Hello,

I'm trying to use interferences_with_ray but I'm completely unable to find anything. I'm probably doing something wrong, but I cannot find out what. In this test I'm adding a ball with 0.5 radius in the coordinates (1, 1, 1) and I'm casting a ray from (0, 0, 0) looking towards (1, 1, 1). I think the ball should interfere with the ray.

When I run the test I get the following result

---- geometry::interferences_with_ray::interferences_with_ray stdout ----
thread 'geometry::interferences_with_ray::interferences_with_ray' panicked at 'Expected 1 collision, got 0', build/ncollide3d/tests/geometry/interferences_with_ray.rs:35:5
use std::f32;

use na::{Isometry3, Translation3, UnitQuaternion, Vector3, Point3};
use ncollide3d::{
    query::Ray,
    shape::{Ball, ShapeHandle},
    world::{CollisionGroups, CollisionWorld, GeometricQueryType},
};

#[test]
fn interferences_with_ray() {
    let mut world = CollisionWorld::new(0.01);

    let shape = ShapeHandle::new(Ball::new(0.5f32));
    let groups = CollisionGroups::new();
    let query = GeometricQueryType::Contacts(0.0, 0.0);

    let tra = Translation3::new(1.0, 1.0, 1.0);
    let rot = UnitQuaternion::from_scaled_axis(Vector3::y() * f32::consts::PI);
    let iso = Isometry3::from_parts(tra, rot);

    world.add(iso, shape.clone(), groups, query, ());

    let num_collision_objects = world.collision_objects().into_iter().collect::<Vec<_>>().len();
    assert!(
        num_collision_objects  == 1,
        format!("Expected 1 collision object, got {}", num_collision_objects),
    );

    let ray = Ray::new(Point3::new(0.0, 0.0, 0.0), Vector3::new(1.0, 1.0, 1.0));
    let groups = CollisionGroups::new();
    let interferences = world.interferences_with_ray(&ray, &groups);

    let num_collisions = interferences.into_iter().collect::<Vec<_>>().len();
    assert!(
        num_collisions  == 1,
        format!("Expected 1 collision, got {}", num_collisions),
    );
}

Thanks!

seppo0010 commented 5 years ago

So... apparently I had to call update for the collisions objects to be actually inserted.