author | unC0Rr |
Tue, 21 Jan 2025 22:14:28 +0100 | |
changeset 16089 | 3f73daa3f212 |
parent 16010 | 5ba4d3a0c3eb |
permissions | -rw-r--r-- |
15141 | 1 |
use std::ops::RangeInclusive; |
2 |
||
15401 | 3 |
use crate::{common::GearId, data::GearDataManager, grid::Grid}; |
15141 | 4 |
|
5 |
use fpnum::*; |
|
15850 | 6 |
use integral_geometry::{Point, PotSize}; |
15141 | 7 |
use land2d::Land2D; |
8 |
||
9 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
|
10 |
pub struct CircleBounds { |
|
11 |
pub center: FPPoint, |
|
12 |
pub radius: FPNum, |
|
13 |
} |
|
14 |
||
15 |
impl CircleBounds { |
|
16 |
pub fn intersects(&self, other: &CircleBounds) -> bool { |
|
17 |
(other.center - self.center).is_in_range(self.radius + other.radius) |
|
18 |
} |
|
19 |
||
20 |
pub fn rows(&self) -> impl Iterator<Item = (usize, RangeInclusive<usize>)> { |
|
21 |
let radius = self.radius.abs_round() as usize; |
|
22 |
let center = Point::from_fppoint(&self.center); |
|
23 |
(center.y as usize - radius..=center.y as usize + radius) |
|
24 |
.map(move |row| (row, center.x as usize - radius..=center.x as usize + radius)) |
|
25 |
} |
|
26 |
} |
|
27 |
||
28 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
|
29 |
pub struct CollisionData { |
|
30 |
pub bounds: CircleBounds, |
|
31 |
} |
|
32 |
||
33 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
|
34 |
pub struct ContactData { |
|
35 |
pub elasticity: FPNum, |
|
36 |
pub friction: FPNum, |
|
37 |
} |
|
38 |
||
39 |
struct EnabledCollisionsCollection { |
|
40 |
gear_ids: Vec<GearId>, |
|
41 |
collisions: Vec<CollisionData>, |
|
42 |
} |
|
43 |
||
44 |
impl EnabledCollisionsCollection { |
|
45 |
fn new() -> Self { |
|
46 |
Self { |
|
47 |
gear_ids: Vec::new(), |
|
48 |
collisions: Vec::new(), |
|
49 |
} |
|
50 |
} |
|
51 |
||
52 |
fn push(&mut self, gear_id: GearId, collision: CollisionData) { |
|
53 |
self.gear_ids.push(gear_id); |
|
54 |
self.collisions.push(collision); |
|
55 |
} |
|
56 |
||
57 |
fn iter(&self) -> impl Iterator<Item = (GearId, &CollisionData)> { |
|
58 |
self.gear_ids.iter().cloned().zip(self.collisions.iter()) |
|
59 |
} |
|
60 |
} |
|
61 |
||
62 |
pub struct CollisionProcessor { |
|
63 |
grid: Grid, |
|
64 |
enabled_collisions: EnabledCollisionsCollection, |
|
65 |
||
66 |
detected_collisions: DetectedCollisions, |
|
67 |
} |
|
68 |
||
69 |
pub struct DetectedCollisions { |
|
15284 | 70 |
pub pairs: Vec<(GearId, Option<GearId>)>, |
15141 | 71 |
pub positions: Vec<Point>, |
72 |
} |
|
73 |
||
74 |
impl DetectedCollisions { |
|
75 |
pub fn new(capacity: usize) -> Self { |
|
76 |
Self { |
|
77 |
pairs: Vec::with_capacity(capacity), |
|
78 |
positions: Vec::with_capacity(capacity), |
|
79 |
} |
|
80 |
} |
|
81 |
||
15284 | 82 |
pub fn push( |
83 |
&mut self, |
|
84 |
contact_gear_id1: GearId, |
|
85 |
contact_gear_id2: Option<GearId>, |
|
86 |
position: &FPPoint, |
|
87 |
) { |
|
15141 | 88 |
self.pairs.push((contact_gear_id1, contact_gear_id2)); |
16010 | 89 |
self.positions.push(Point::from_fppoint(&position)); |
15141 | 90 |
} |
15287
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15284
diff
changeset
|
91 |
|
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15284
diff
changeset
|
92 |
pub fn clear(&mut self) { |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15284
diff
changeset
|
93 |
self.pairs.clear(); |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15284
diff
changeset
|
94 |
self.positions.clear() |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15284
diff
changeset
|
95 |
} |
15141 | 96 |
} |
97 |
||
98 |
impl CollisionProcessor { |
|
15401 | 99 |
pub fn register_components(data: &mut GearDataManager) { |
100 |
data.register::<CollisionData>(); |
|
101 |
data.register::<ContactData>(); |
|
102 |
} |
|
103 |
||
15850 | 104 |
pub fn new(size: PotSize) -> Self { |
15141 | 105 |
Self { |
106 |
grid: Grid::new(size), |
|
107 |
enabled_collisions: EnabledCollisionsCollection::new(), |
|
108 |
detected_collisions: DetectedCollisions::new(0), |
|
109 |
} |
|
110 |
} |
|
111 |
||
15401 | 112 |
pub fn add(&mut self, gear_id: GearId, gear_data: CollisionData) { |
15849 | 113 |
self.grid.insert(gear_id, &gear_data.bounds); |
15401 | 114 |
} |
115 |
||
116 |
pub fn remove(&mut self, gear_id: GearId) { |
|
15849 | 117 |
self.grid.remove(gear_id, None); |
15401 | 118 |
} |
119 |
||
120 |
pub fn get(&mut self, gear_id: GearId) -> Option<CollisionData> { |
|
121 |
None |
|
122 |
} |
|
123 |
||
15287
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15284
diff
changeset
|
124 |
pub fn process( |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15284
diff
changeset
|
125 |
&mut self, |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15284
diff
changeset
|
126 |
land: &Land2D<u32>, |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15284
diff
changeset
|
127 |
updates: &crate::physics::PositionUpdates, |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15284
diff
changeset
|
128 |
) -> &DetectedCollisions { |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15284
diff
changeset
|
129 |
self.detected_collisions.clear(); |
15141 | 130 |
self.grid.check_collisions(&mut self.detected_collisions); |
131 |
||
132 |
for (gear_id, collision) in self.enabled_collisions.iter() { |
|
133 |
if collision |
|
134 |
.bounds |
|
135 |
.rows() |
|
136 |
.any(|(y, r)| (&land[y][r]).iter().any(|v| *v != 0)) |
|
137 |
{ |
|
138 |
self.detected_collisions |
|
15284 | 139 |
.push(gear_id, None, &collision.bounds.center) |
15141 | 140 |
} |
141 |
} |
|
15287
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15284
diff
changeset
|
142 |
|
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15284
diff
changeset
|
143 |
&self.detected_collisions |
15141 | 144 |
} |
145 |
} |