rust/hwphysics/src/collision.rs
author unc0rr
Tue, 04 Jun 2019 22:34:42 +0200
changeset 15125 febccab419b1
parent 14721 8e74d4eb89f5
child 15266 501dfa1c8deb
permissions -rw-r--r--
Apply dos2unix to rust sources
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15125
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
     1
use std::ops::RangeInclusive;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
     2
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
     3
use crate::{
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
     4
    common::{GearData, GearDataProcessor, GearId},
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
     5
    grid::Grid,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
     6
    physics::PhysicsData,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
     7
};
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
     8
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
     9
use fpnum::*;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    10
use integral_geometry::{GridIndex, Point, Size};
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    11
use land2d::Land2D;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    12
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    13
pub fn fppoint_round(point: &FPPoint) -> Point {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    14
    Point::new(point.x().round() as i32, point.y().round() as i32)
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    15
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    16
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    17
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    18
pub struct CircleBounds {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    19
    pub center: FPPoint,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    20
    pub radius: FPNum,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    21
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    22
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    23
impl CircleBounds {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    24
    pub fn intersects(&self, other: &CircleBounds) -> bool {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    25
        (other.center - self.center).is_in_range(self.radius + other.radius)
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    26
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    27
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    28
    pub fn rows(&self) -> impl Iterator<Item = (usize, RangeInclusive<usize>)> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    29
        let radius = self.radius.abs_round() as usize;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    30
        let center = Point::from_fppoint(&self.center);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    31
        (center.y as usize - radius..=center.y as usize + radius)
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    32
            .map(move |row| (row, center.x as usize - radius..=center.x as usize + radius))
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    33
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    34
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    35
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    36
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    37
pub struct CollisionData {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    38
    pub bounds: CircleBounds,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    39
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    40
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    41
impl GearData for CollisionData {}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    42
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    43
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    44
pub struct ContactData {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    45
    pub elasticity: FPNum,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    46
    pub friction: FPNum,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    47
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    48
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    49
impl GearData for ContactData {}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    50
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    51
struct EnabledCollisionsCollection {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    52
    gear_ids: Vec<GearId>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    53
    collisions: Vec<CollisionData>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    54
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    55
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    56
impl EnabledCollisionsCollection {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    57
    fn new() -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    58
        Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    59
            gear_ids: Vec::new(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    60
            collisions: Vec::new(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    61
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    62
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    63
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    64
    fn push(&mut self, gear_id: GearId, collision: CollisionData) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    65
        self.gear_ids.push(gear_id);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    66
        self.collisions.push(collision);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    67
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    68
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    69
    fn iter(&self) -> impl Iterator<Item = (GearId, &CollisionData)> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    70
        self.gear_ids.iter().cloned().zip(self.collisions.iter())
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    71
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    72
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    73
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    74
pub struct CollisionProcessor {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    75
    grid: Grid,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    76
    enabled_collisions: EnabledCollisionsCollection,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    77
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    78
    detected_collisions: DetectedCollisions,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    79
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    80
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    81
pub struct DetectedCollisions {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    82
    pub pairs: Vec<(GearId, GearId)>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    83
    pub positions: Vec<Point>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    84
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    85
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    86
impl DetectedCollisions {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    87
    pub fn new(capacity: usize) -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    88
        Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    89
            pairs: Vec::with_capacity(capacity),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    90
            positions: Vec::with_capacity(capacity),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    91
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    92
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    93
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    94
    pub fn push(&mut self, contact_gear_id1: GearId, contact_gear_id2: GearId, position: &FPPoint) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    95
        self.pairs.push((contact_gear_id1, contact_gear_id2));
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    96
        self.positions.push(fppoint_round(&position));
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    97
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    98
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    99
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   100
impl CollisionProcessor {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   101
    pub fn new(size: Size) -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   102
        Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   103
            grid: Grid::new(size),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   104
            enabled_collisions: EnabledCollisionsCollection::new(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   105
            detected_collisions: DetectedCollisions::new(0),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   106
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   107
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   108
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   109
    pub fn process(&mut self, land: &Land2D<u32>, updates: &crate::physics::PositionUpdates) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   110
        self.grid.check_collisions(&mut self.detected_collisions);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   111
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   112
        for (gear_id, collision) in self.enabled_collisions.iter() {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   113
            if collision
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   114
                .bounds
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   115
                .rows()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   116
                .any(|(y, r)| (&land[y][r]).iter().any(|v| *v != 0))
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   117
            {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   118
                self.detected_collisions
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   119
                    .push(gear_id, 0, &collision.bounds.center)
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   120
            }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   121
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   122
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   123
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   124
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   125
impl GearDataProcessor<CollisionData> for CollisionProcessor {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   126
    fn add(&mut self, gear_id: GearId, gear_data: CollisionData) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   127
        self.grid.insert_static(gear_id, &gear_data.bounds);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   128
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   129
}