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