rust/hwphysics/src/grid.rs
author Wuzzy <Wuzzy2@mail.ru>
Sun, 07 Apr 2019 19:26:16 +0200
changeset 14755 ab7bf5036314
parent 14721 8e74d4eb89f5
child 15125 febccab419b1
permissions -rw-r--r--
Render mine timer if gear info is active
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14183
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
     1
use crate::{
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
     2
    collision::{fppoint_round, CircleBounds, DetectedCollisions},
14183
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
     3
    common::GearId,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
     4
};
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
     5
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
     6
use fpnum::FPPoint;
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
     7
use integral_geometry::{GridIndex, Point, Size};
14183
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
     8
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
     9
struct GridBin {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    10
    refs: Vec<GearId>,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    11
    static_entries: Vec<CircleBounds>,
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    12
    dynamic_entries: Vec<CircleBounds>,
14183
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    13
}
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    14
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    15
impl GridBin {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    16
    fn new() -> Self {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    17
        Self {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    18
            refs: vec![],
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    19
            static_entries: vec![],
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    20
            dynamic_entries: vec![],
14183
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    21
        }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    22
    }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    23
}
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    24
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    25
const GRID_BIN_SIZE: usize = 128;
14183
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    26
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    27
pub struct Grid {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    28
    bins: Vec<GridBin>,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    29
    space_size: Size,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    30
    bins_count: Size,
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    31
    index: GridIndex,
14183
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    32
}
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    33
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    34
impl Grid {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    35
    pub fn new(size: Size) -> Self {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    36
        assert!(size.is_power_of_two());
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    37
        let bins_count = Size::new(size.width / GRID_BIN_SIZE, size.height / GRID_BIN_SIZE);
14183
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    38
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    39
        Self {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    40
            bins: (0..bins_count.area()).map(|_| GridBin::new()).collect(),
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    41
            space_size: size,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    42
            bins_count,
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    43
            index: Size::square(GRID_BIN_SIZE).to_grid_index(),
14183
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    44
        }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    45
    }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    46
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    47
    fn bin_index(&self, position: &FPPoint) -> Point {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    48
        self.index.map(fppoint_round(position))
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    49
    }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    50
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    51
    fn lookup_bin(&mut self, position: &FPPoint) -> &mut GridBin {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    52
        let index = self.bin_index(position);
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    53
        &mut self.bins[index.x as usize * self.bins_count.width + index.y as usize]
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    54
    }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    55
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    56
    pub fn insert_static(&mut self, gear_id: GearId, bounds: &CircleBounds) {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    57
        self.lookup_bin(&bounds.center).static_entries.push(*bounds)
14183
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    58
    }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    59
14184
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14183
diff changeset
    60
    pub fn insert_dynamic(&mut self, gear_id: GearId, bounds: &CircleBounds) {
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    61
        self.lookup_bin(&bounds.center)
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    62
            .dynamic_entries
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    63
            .push(*bounds)
14183
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    64
    }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    65
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    66
    pub fn check_collisions(&self, collisions: &mut DetectedCollisions) {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    67
        for bin in &self.bins {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    68
            for bounds in &bin.dynamic_entries {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    69
                for other in &bin.dynamic_entries {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    70
                    if bounds.intersects(other) && bounds != other {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    71
                        collisions.push(0, 0, &bounds.center)
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    72
                    }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    73
                }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    74
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    75
                for other in &bin.static_entries {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    76
                    if bounds.intersects(other) {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    77
                        collisions.push(0, 0, &bounds.center)
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    78
                    }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    79
                }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    80
            }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    81
        }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    82
    }
14721
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14184
diff changeset
    83
}