physics.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. use crate::com::{Blocking, Collision, Velocity, Position};
  2. use crate::types::{NPhase,BPhase};
  3. use crate::game::MyGame;
  4. use nalgebra::{Isometry2, Vector2};
  5. use ncollide2d::broad_phase::{
  6. ProxyHandle,
  7. broad_phase::BroadPhase,
  8. BroadPhaseInterferenceHandler,
  9. };
  10. use specs::{Join,RunNow};
  11. struct InterferenceHandler<'a> {
  12. collisions: specs::WriteStorage<'a, Collision>,
  13. }
  14. impl<'a> BroadPhaseInterferenceHandler<specs::Entity> for InterferenceHandler<'a> {
  15. fn is_interference_allowed(&mut self, a: &specs::Entity, b: &specs::Entity) -> bool {
  16. // Prevent self-collision.
  17. *a != *b
  18. }
  19. fn interference_started(&mut self, a: &specs::Entity, b: &specs::Entity) {
  20. self.collisions.get_mut(*a).map (|r| r.has_collision = true );
  21. self.collisions.get_mut(*b).map (|r| r.has_collision = true );
  22. }
  23. fn interference_stopped(&mut self, _: &specs::Entity, _: &specs::Entity) {
  24. }
  25. }
  26. struct Collide;
  27. impl<'a> specs::System<'a> for Collide {
  28. type SystemData = (
  29. specs::Entities<'a>,
  30. specs::ReadStorage<'a, Position>,
  31. specs::ReadStorage<'a, Velocity>,
  32. specs::ReadStorage<'a, Blocking>,
  33. specs::WriteStorage<'a, Collision>,
  34. specs::WriteExpect<'a, BPhase>,
  35. specs::WriteExpect<'a, NPhase>,
  36. );
  37. fn run(&mut self, (entities, position, velocity, blocking, collisions, mut bf, _narrow): Self::SystemData) {
  38. let handles: Vec<ProxyHandle> =
  39. (&entities, &position, &blocking).join().map( |(e, pos, bl)| {
  40. let np = if let Some(vel) = velocity.get(e) {
  41. pos.moved(vel)
  42. } else {
  43. pos.clone()
  44. };
  45. bf.create_proxy(
  46. bl.volume.aabb(&Isometry2::new(Vector2::new(pos.x, pos.y), nalgebra::zero())),
  47. e,
  48. )
  49. }).collect();
  50. bf.update(&mut InterferenceHandler {
  51. collisions: collisions,
  52. });
  53. bf.remove(&handles, &mut |_, _| {});
  54. }
  55. }
  56. struct Intersection;
  57. impl<'a> specs::System<'a> for Intersection {
  58. type SystemData = (
  59. specs::Entities<'a>,
  60. specs::ReadStorage<'a, Position>,
  61. specs::ReadStorage<'a, Velocity>,
  62. specs::ReadStorage<'a, Blocking>,
  63. specs::WriteStorage<'a, Collision>,
  64. );
  65. fn run(&mut self, (entity, position, velocity, blocking, mut collision): Self::SystemData) {
  66. let mut spacemap = std::collections::HashMap::new();
  67. for (e, pos, _) in (&entity, &position, &blocking).join() {
  68. spacemap.insert(pos.to_grid(), e);
  69. }
  70. for (pos, vel, col) in (&position, &velocity, &mut collision).join() {
  71. if let Some(_) = spacemap.get(&pos.moved(vel).to_grid()) {
  72. col.has_collision = true;
  73. }
  74. }
  75. }
  76. }
  77. struct Physics;
  78. impl <'a> specs::System<'a> for Physics {
  79. type SystemData = (
  80. specs::ReadStorage<'a, Velocity>,
  81. specs::ReadStorage<'a, Collision>,
  82. specs::WriteStorage<'a, Position>,
  83. );
  84. fn run(&mut self, (velocity, collision, mut position): Self::SystemData) {
  85. for (vel, col, pos) in (&velocity, &collision, &mut position).join() {
  86. if !col.has_collision {
  87. pos.x += vel.dx;
  88. pos.y += vel.dy;
  89. }
  90. }
  91. }
  92. }
  93. struct ResetCollision;
  94. impl<'a> specs::System<'a> for ResetCollision {
  95. type SystemData =
  96. specs::WriteStorage<'a, Collision>;
  97. fn run(&mut self, mut collision: Self::SystemData) {
  98. for mut e in (&mut collision).join() {
  99. e.has_collision = false;
  100. }
  101. }
  102. }
  103. pub fn systems(game: &mut MyGame) {
  104. Collide.run_now(&game.world.res);
  105. // Intersection.run_now(&game.world.res);
  106. Physics.run_now(&game.world.res);
  107. ResetCollision.run_now(&game.world.res);
  108. }