physics.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. broad_phase::BroadPhase,
  7. broad_phase::BroadPhaseProxyHandle,
  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, mut collisions, mut bf, _narrow): Self::SystemData) {
  38. let _: Vec<()> = (&mut collisions).join().map( |c| c.has_collision = false ).collect();
  39. let handles: Vec<BroadPhaseProxyHandle> =
  40. (&entities, &position, &blocking).join().map( |(e, pos, bl)| {
  41. let np = if let Some(vel) = velocity.get(e) {
  42. pos.moved(vel)
  43. } else {
  44. pos.clone()
  45. };
  46. bf.create_proxy(
  47. bl.volume.aabb(&Isometry2::new(Vector2::new(np.x, np.y), nalgebra::zero())),
  48. e,
  49. )
  50. }).collect();
  51. bf.update(&mut InterferenceHandler {
  52. collisions: collisions,
  53. });
  54. bf.remove(&handles, &mut |_, _| {});
  55. }
  56. }
  57. struct Intersection;
  58. impl<'a> specs::System<'a> for Intersection {
  59. type SystemData = (
  60. specs::Entities<'a>,
  61. specs::ReadStorage<'a, Position>,
  62. specs::ReadStorage<'a, Velocity>,
  63. specs::ReadStorage<'a, Blocking>,
  64. specs::WriteStorage<'a, Collision>,
  65. );
  66. fn run(&mut self, (entity, position, velocity, blocking, mut collision): Self::SystemData) {
  67. let mut spacemap = std::collections::HashMap::new();
  68. for (e, pos, _) in (&entity, &position, &blocking).join() {
  69. spacemap.insert(pos.to_grid(), e);
  70. }
  71. for (pos, vel, col) in (&position, &velocity, &mut collision).join() {
  72. if let Some(_) = spacemap.get(&pos.moved(vel).to_grid()) {
  73. col.has_collision = true;
  74. }
  75. }
  76. }
  77. }
  78. struct Physics;
  79. impl <'a> specs::System<'a> for Physics {
  80. type SystemData = (
  81. specs::ReadStorage<'a, Velocity>,
  82. specs::ReadStorage<'a, Collision>,
  83. specs::WriteStorage<'a, Position>,
  84. );
  85. fn run(&mut self, (velocity, collision, mut position): Self::SystemData) {
  86. for (vel, col, pos) in (&velocity, &collision, &mut position).join() {
  87. if !col.has_collision {
  88. pos.x += vel.dx;
  89. pos.y += vel.dy;
  90. }
  91. }
  92. }
  93. }
  94. struct ResetCollision;
  95. impl<'a> specs::System<'a> for ResetCollision {
  96. type SystemData =
  97. specs::WriteStorage<'a, Collision>;
  98. fn run(&mut self, mut collision: Self::SystemData) {
  99. for mut e in (&mut collision).join() {
  100. e.has_collision = false;
  101. }
  102. }
  103. }
  104. pub fn systems(game: &mut MyGame) {
  105. Collide.run_now(&game.world);
  106. // Intersection.run_now(&game.world.res);
  107. Physics.run_now(&game.world);
  108. ResetCollision.run_now(&game.world);
  109. }