rust/hwphysics/src/collision.rs
author alfadur
Thu, 25 Jul 2019 22:31:24 +0300
changeset 15279 42b710b0f883
parent 15275 7446258fab98
child 15287 478d5372eb4a
permissions -rw-r--r--
add gear allocator
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 {
15275
7446258fab98 add time events
alfadur
parents: 15271
diff changeset
    14
    Point::new(point.x().round(), point.y().round())
15125
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 {
15268
24828281c9c5 reserve zero gear id
alfadur
parents: 15266
diff changeset
    82
    pub pairs: Vec<(GearId, Option<GearId>)>,
15125
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
15268
24828281c9c5 reserve zero gear id
alfadur
parents: 15266
diff changeset
    94
    pub fn push(
24828281c9c5 reserve zero gear id
alfadur
parents: 15266
diff changeset
    95
        &mut self,
24828281c9c5 reserve zero gear id
alfadur
parents: 15266
diff changeset
    96
        contact_gear_id1: GearId,
24828281c9c5 reserve zero gear id
alfadur
parents: 15266
diff changeset
    97
        contact_gear_id2: Option<GearId>,
24828281c9c5 reserve zero gear id
alfadur
parents: 15266
diff changeset
    98
        position: &FPPoint,
24828281c9c5 reserve zero gear id
alfadur
parents: 15266
diff changeset
    99
    ) {
15125
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   100
        self.pairs.push((contact_gear_id1, contact_gear_id2));
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   101
        self.positions.push(fppoint_round(&position));
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   102
    }
15271
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15268
diff changeset
   103
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15268
diff changeset
   104
    pub fn clear(&mut self) {
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15268
diff changeset
   105
        self.pairs.clear();
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15268
diff changeset
   106
        self.positions.clear()
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15268
diff changeset
   107
    }
15125
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   108
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   109
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   110
impl CollisionProcessor {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   111
    pub fn new(size: Size) -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   112
        Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   113
            grid: Grid::new(size),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   114
            enabled_collisions: EnabledCollisionsCollection::new(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   115
            detected_collisions: DetectedCollisions::new(0),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   116
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   117
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   118
15271
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15268
diff changeset
   119
    pub fn process(
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15268
diff changeset
   120
        &mut self,
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15268
diff changeset
   121
        land: &Land2D<u32>,
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15268
diff changeset
   122
        updates: &crate::physics::PositionUpdates,
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15268
diff changeset
   123
    ) -> &DetectedCollisions {
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15268
diff changeset
   124
        self.detected_collisions.clear();
15266
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15125
diff changeset
   125
        for (id, old_position, new_position) in updates.iter() {
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15125
diff changeset
   126
            self.grid.update_position(id, old_position, new_position)
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15125
diff changeset
   127
        }
15271
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15268
diff changeset
   128
15125
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   129
        self.grid.check_collisions(&mut self.detected_collisions);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   130
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   131
        for (gear_id, collision) in self.enabled_collisions.iter() {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   132
            if collision
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   133
                .bounds
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   134
                .rows()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   135
                .any(|(y, r)| (&land[y][r]).iter().any(|v| *v != 0))
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   136
            {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   137
                self.detected_collisions
15268
24828281c9c5 reserve zero gear id
alfadur
parents: 15266
diff changeset
   138
                    .push(gear_id, None, &collision.bounds.center)
15125
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   139
            }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   140
        }
15271
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15268
diff changeset
   141
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15268
diff changeset
   142
        &self.detected_collisions
15125
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   143
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   144
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   145
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   146
impl GearDataProcessor<CollisionData> for CollisionProcessor {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   147
    fn add(&mut self, gear_id: GearId, gear_data: CollisionData) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   148
        self.grid.insert_static(gear_id, &gear_data.bounds);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   149
    }
15279
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
   150
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
   151
    fn remove(&mut self, gear_id: GearId) {
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
   152
        self.grid.remove(gear_id);
42b710b0f883 add gear allocator
alfadur
parents: 15275
diff changeset
   153
    }
15125
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   154
}