rust/hwphysics/src/grid.rs
author Wuzzy <Wuzzy2@mail.ru>
Tue, 23 Apr 2019 13:22:12 +0200
changeset 14848 266f973745b8
parent 14737 8e74d4eb89f5
child 15141 febccab419b1
permissions -rw-r--r--
Change color of fallback border/land texture in Planes theme to brown
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14199
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
     1
use crate::{
14737
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14200
diff changeset
     2
    collision::{fppoint_round, CircleBounds, DetectedCollisions},
14199
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;
14737
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14200
diff changeset
     7
use integral_geometry::{GridIndex, Point, Size};
14199
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>,
14737
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14200
diff changeset
    12
    dynamic_entries: Vec<CircleBounds>,
14199
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![],
14737
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14200
diff changeset
    20
            dynamic_entries: vec![],
14199
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
14200
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14199
diff changeset
    25
const GRID_BIN_SIZE: usize = 128;
14199
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,
14737
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14200
diff changeset
    31
    index: GridIndex,
14199
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());
14737
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14200
diff changeset
    37
        let bins_count = Size::new(size.width / GRID_BIN_SIZE, size.height / GRID_BIN_SIZE);
14199
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,
14737
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14200
diff changeset
    43
            index: Size::square(GRID_BIN_SIZE).to_grid_index(),
14199
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
14200
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14199
diff changeset
    56
    pub fn insert_static(&mut self, gear_id: GearId, bounds: &CircleBounds) {
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14199
diff changeset
    57
        self.lookup_bin(&bounds.center).static_entries.push(*bounds)
14199
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    58
    }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    59
14200
abbb74b9cb62 generalize adding data to World
alfadur
parents: 14199
diff changeset
    60
    pub fn insert_dynamic(&mut self, gear_id: GearId, bounds: &CircleBounds) {
14737
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14200
diff changeset
    61
        self.lookup_bin(&bounds.center)
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14200
diff changeset
    62
            .dynamic_entries
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14200
diff changeset
    63
            .push(*bounds)
14199
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
    }
14737
8e74d4eb89f5 add random falling stuff
alfadur
parents: 14200
diff changeset
    83
}