fork(2) download
  1. extern crate nalgebra;
  2. extern crate ncollide;
  3.  
  4. use ncollide::broad_phase::{BroadPhase, DBVTBroadPhase};
  5. use ncollide::bounding_volume::BoundingSphere;
  6.  
  7. type Position = nalgebra::Pnt2<f64>;
  8.  
  9. trait Collider {
  10. fn get_bounds(&self) -> BoundingSphere<Position>;
  11. fn do_something(&mut self);
  12. }
  13.  
  14. struct Sphere {
  15. position: Position,
  16. radius: f64,
  17. }
  18.  
  19. impl Collider for Sphere {
  20. fn get_bounds(&self) -> BoundingSphere<Position> {
  21. BoundingSphere::new(self.position, self.radius)
  22. }
  23.  
  24. fn do_something(&mut self) {
  25. }
  26. }
  27.  
  28. type MyBroadPhase<'a> = DBVTBroadPhase<Position, BoundingSphere<Position>, &'a mut Collider>;
  29.  
  30. struct World<'a> {
  31. spheres: Box<[Sphere]>,
  32. num_spheres: usize,
  33. broad: MyBroadPhase<'a>,
  34. }
  35.  
  36. impl<'a> World<'a> {
  37. fn new(count: usize) -> World<'a> {
  38. let mut spheres = Vec::with_capacity(count);
  39.  
  40. let mut broad: MyBroadPhase<'a> = DBVTBroadPhase::new(10.0, true);
  41. for i in 0..count {
  42. spheres.push(Sphere { position: Position::new(0.0, 0.0), radius: 16.0 });
  43. let sphere = spheres.last_mut().unwrap();
  44. //help
  45. //error: `spheres` does not live long enough
  46. let sphere: &'a mut Sphere = unsafe { &mut *(sphere as *mut Sphere) };
  47. broad.deferred_add(i, sphere.get_bounds(), sphere);
  48. }
  49.  
  50. World {
  51. spheres: spheres.into_boxed_slice(),
  52. num_spheres: count,
  53. broad: broad,
  54. }
  55. }
  56. }
  57.  
  58. fn main() {
  59. let world = World::new(1);
  60.  
  61. let mut vec = Vec::new(world.num_spheres);
  62. let bv = BoundingSphere::new(Position::new(0.0, 0.0), 4.0);
  63. world.broad.interferences_with_bounding_volume(&bv, &mut vec);
  64. for &mut collider in vec.iter_mut() {
  65. //help
  66. //error: cannot borrow data mutably in a `&` reference
  67. let collider: &mut Collider = unsafe { *(collider as *const &mut Collider) };
  68. collider.do_something();
  69. }
  70. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.rs:1:1: 1:23 error: can't find crate for `nalgebra`
prog.rs:1 extern crate nalgebra;
          ^~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
stdout
Standard output is empty