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