rust/hwphysics/src/collision.rs
changeset 15380 6e3e5be8b2e2
parent 15282 478d5372eb4a
child 15827 64b0a5cead86
equal deleted inserted replaced
15379:27915135f87f 15380:6e3e5be8b2e2
     1 use std::ops::RangeInclusive;
     1 use std::ops::RangeInclusive;
     2 
     2 
     3 use crate::{
     3 use crate::{common::GearId, data::GearDataManager, grid::Grid};
     4     common::{GearData, GearDataProcessor, GearId},
       
     5     grid::Grid,
       
     6 };
       
     7 
     4 
     8 use fpnum::*;
     5 use fpnum::*;
     9 use integral_geometry::{Point, Size};
     6 use integral_geometry::{Point, Size};
    10 use land2d::Land2D;
     7 use land2d::Land2D;
    11 
     8 
    35 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
    32 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
    36 pub struct CollisionData {
    33 pub struct CollisionData {
    37     pub bounds: CircleBounds,
    34     pub bounds: CircleBounds,
    38 }
    35 }
    39 
    36 
    40 impl GearData for CollisionData {}
       
    41 
       
    42 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
    37 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
    43 pub struct ContactData {
    38 pub struct ContactData {
    44     pub elasticity: FPNum,
    39     pub elasticity: FPNum,
    45     pub friction: FPNum,
    40     pub friction: FPNum,
    46 }
    41 }
    47 
       
    48 impl GearData for ContactData {}
       
    49 
    42 
    50 struct EnabledCollisionsCollection {
    43 struct EnabledCollisionsCollection {
    51     gear_ids: Vec<GearId>,
    44     gear_ids: Vec<GearId>,
    52     collisions: Vec<CollisionData>,
    45     collisions: Vec<CollisionData>,
    53 }
    46 }
   105         self.positions.clear()
    98         self.positions.clear()
   106     }
    99     }
   107 }
   100 }
   108 
   101 
   109 impl CollisionProcessor {
   102 impl CollisionProcessor {
       
   103     pub fn register_components(data: &mut GearDataManager) {
       
   104         data.register::<CollisionData>();
       
   105         data.register::<ContactData>();
       
   106     }
       
   107 
   110     pub fn new(size: Size) -> Self {
   108     pub fn new(size: Size) -> Self {
   111         Self {
   109         Self {
   112             grid: Grid::new(size),
   110             grid: Grid::new(size),
   113             enabled_collisions: EnabledCollisionsCollection::new(),
   111             enabled_collisions: EnabledCollisionsCollection::new(),
   114             detected_collisions: DetectedCollisions::new(0),
   112             detected_collisions: DetectedCollisions::new(0),
   115         }
   113         }
       
   114     }
       
   115 
       
   116     pub fn add(&mut self, gear_id: GearId, gear_data: CollisionData) {
       
   117         self.grid.insert_static(gear_id, &gear_data.bounds);
       
   118     }
       
   119 
       
   120     pub fn remove(&mut self, gear_id: GearId) {
       
   121         self.grid.remove(gear_id);
       
   122     }
       
   123 
       
   124     pub fn get(&mut self, gear_id: GearId) -> Option<CollisionData> {
       
   125         None
   116     }
   126     }
   117 
   127 
   118     pub fn process(
   128     pub fn process(
   119         &mut self,
   129         &mut self,
   120         land: &Land2D<u32>,
   130         land: &Land2D<u32>,
   139         }
   149         }
   140 
   150 
   141         &self.detected_collisions
   151         &self.detected_collisions
   142     }
   152     }
   143 }
   153 }
   144 
       
   145 impl GearDataProcessor<CollisionData> for CollisionProcessor {
       
   146     fn add(&mut self, gear_id: GearId, gear_data: CollisionData) {
       
   147         self.grid.insert_static(gear_id, &gear_data.bounds);
       
   148     }
       
   149 
       
   150     fn remove(&mut self, gear_id: GearId) {
       
   151         self.grid.remove(gear_id);
       
   152     }
       
   153 
       
   154     fn get(&mut self, gear_id: GearId) -> Option<CollisionData> {
       
   155         None
       
   156     }
       
   157 }