rust/hwphysics/src/grid.rs
changeset 14179 abbb74b9cb62
parent 14178 a4c17cfaa4c9
child 14716 8e74d4eb89f5
equal deleted inserted replaced
14178:a4c17cfaa4c9 14179:abbb74b9cb62
    28             dynamic_entries: vec![]
    28             dynamic_entries: vec![]
    29         }
    29         }
    30     }
    30     }
    31 }
    31 }
    32 
    32 
    33 const GRID_BIN_SIZE: usize = 256;
    33 const GRID_BIN_SIZE: usize = 128;
    34 
    34 
    35 pub struct Grid {
    35 pub struct Grid {
    36     bins: Vec<GridBin>,
    36     bins: Vec<GridBin>,
    37     space_size: Size,
    37     space_size: Size,
    38     bins_count: Size,
    38     bins_count: Size,
    61     fn lookup_bin(&mut self, position: &FPPoint) -> &mut GridBin {
    61     fn lookup_bin(&mut self, position: &FPPoint) -> &mut GridBin {
    62         let index = self.bin_index(position);
    62         let index = self.bin_index(position);
    63         &mut self.bins[index.x as usize * self.bins_count.width + index.y as usize]
    63         &mut self.bins[index.x as usize * self.bins_count.width + index.y as usize]
    64     }
    64     }
    65 
    65 
    66     pub fn insert_static(&mut self, gear_id: GearId, position: &FPPoint, bounds: &CircleBounds) {
    66     pub fn insert_static(&mut self, gear_id: GearId, bounds: &CircleBounds) {
    67         self.lookup_bin(position).static_entries.push(*bounds)
    67         self.lookup_bin(&bounds.center).static_entries.push(*bounds)
    68     }
    68     }
    69 
    69 
    70     pub fn insert_dynamic(&mut self, gear_id: GearId, position: &FPPoint, bounds: &CircleBounds) {
    70     pub fn insert_dynamic(&mut self, gear_id: GearId, bounds: &CircleBounds) {
    71         self.lookup_bin(position).dynamic_entries.push(*bounds)
    71         self.lookup_bin(&bounds.center).dynamic_entries.push(*bounds)
    72     }
    72     }
    73 
    73 
    74     pub fn check_collisions(&self, collisions: &mut DetectedCollisions) {
    74     pub fn check_collisions(&self, collisions: &mut DetectedCollisions) {
    75         for bin in &self.bins {
    75         for bin in &self.bins {
    76             for bounds in &bin.dynamic_entries {
    76             for bounds in &bin.dynamic_entries {