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