rust/hwphysics/src/collision.rs
author alfadur
Fri, 09 Nov 2018 01:05:34 +0300
changeset 14199 a4c17cfaa4c9
child 14200 abbb74b9cb62
permissions -rw-r--r--
split hwphysics into modules
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14199
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
     1
use std::{
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
     2
    ops::RangeInclusive
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
     3
};
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
     4
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
     5
use crate::{
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
     6
    common::GearId,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
     7
    physics::PhysicsData,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
     8
    grid::Grid
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
     9
};
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    10
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    11
use fpnum::*;
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    12
use integral_geometry::{
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    13
    Point, Size, GridIndex
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    14
};
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    15
use land2d::Land2D;
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    16
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    17
pub fn fppoint_round(point: &FPPoint) -> Point {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    18
    Point::new(point.x().round() as i32, point.y().round() as i32)
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    19
}
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    20
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    21
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    22
pub struct CircleBounds {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    23
    pub center: FPPoint,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    24
    pub radius: FPNum
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    25
}
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    26
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    27
impl CircleBounds {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    28
    pub fn intersects(&self, other: &CircleBounds) -> bool {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    29
        (other.center - self.center).is_in_range(self.radius + other.radius)
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    30
    }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    31
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    32
    pub fn rows(&self) -> impl Iterator<Item = (usize, RangeInclusive<usize>)> {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    33
        let radius = self.radius.abs_round() as usize;
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    34
        let center = Point::from_fppoint(&self.center);
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    35
        (center.y as usize - radius..=center.y as usize + radius)
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    36
            .map(move |row| (row, center.x as usize - radius..=center.x as usize + radius))
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    37
    }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    38
}
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    39
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    40
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    41
pub struct CollisionData {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    42
    pub bounds: CircleBounds
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    43
}
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    44
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    45
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    46
pub struct ContactData {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    47
    pub elasticity: FPNum,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    48
    pub friction: FPNum
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
struct EnabledCollisionsCollection {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    52
    gear_ids: Vec<GearId>,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    53
    collisions: Vec<CollisionData>
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    54
}
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    55
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    56
impl EnabledCollisionsCollection {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    57
    fn push(&mut self, gear_id: GearId, collision: CollisionData) {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    58
        self.gear_ids.push(gear_id);
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    59
        self.collisions.push(collision);
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    60
    }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    61
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    62
    fn iter(&self) -> impl Iterator<Item = (GearId, &CollisionData)> {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    63
        self.gear_ids.iter().cloned().zip(self.collisions.iter())
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
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    67
pub struct CollisionProcessor {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    68
    grid: Grid,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    69
    enabled_collisions: EnabledCollisionsCollection,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    70
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    71
    detected_collisions: DetectedCollisions,
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
pub struct DetectedCollisions {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    75
    pub pairs: Vec<(GearId, GearId)>,
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    76
    pub positions: Vec<Point>
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    77
}
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    78
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    79
impl DetectedCollisions {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    80
    pub fn new(capacity: usize) -> Self {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    81
        Self {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    82
            pairs: Vec::with_capacity(capacity),
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    83
            positions: Vec::with_capacity(capacity),
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    84
        }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    85
    }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    86
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    87
    pub fn push(&mut self, contact_gear_id1: GearId, contact_gear_id2: GearId, position: &FPPoint) {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    88
        self.pairs.push((contact_gear_id1, contact_gear_id2));
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    89
        self.positions.push(fppoint_round(position));
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    90
    }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    91
}
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    92
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    93
impl CollisionProcessor {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    94
    pub fn process(&mut self, land: &Land2D<u32>, updates: &crate::physics::PositionUpdate) {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    95
        self.grid.check_collisions(&mut self.detected_collisions);
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    96
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    97
        for (gear_id, collision) in self.enabled_collisions.iter() {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    98
            if collision.bounds.rows().any(|(y, r)| (&land[y][r]).iter().any(|v| *v != 0)) {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
    99
                self.detected_collisions.push(0, 0, &collision.bounds.center)
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
   100
            }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
   101
        }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
   102
    }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
   103
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
   104
    pub fn push(&mut self, gear_id: GearId, physics_data: PhysicsData, collision_data: CollisionData) {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
   105
        if physics_data.velocity.is_zero() {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
   106
            self.grid.insert_static(0, &physics_data.position, &collision_data.bounds);
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
   107
        } else {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
   108
            self.grid.insert_dynamic(0, &physics_data.position, &collision_data.bounds);
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
   109
        }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
   110
    }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents:
diff changeset
   111
}