author | sheepluva |
Sun, 08 Dec 2019 02:09:19 +0100 | |
branch | hedgeroid |
changeset 15520 | de08a3ae4f8d |
parent 15385 | 6e3e5be8b2e2 |
permissions | -rw-r--r-- |
15125 | 1 |
use std::ops::RangeInclusive; |
2 |
||
15385 | 3 |
use crate::{common::GearId, data::GearDataManager, grid::Grid}; |
15125 | 4 |
|
5 |
use fpnum::*; |
|
15287 | 6 |
use integral_geometry::{Point, Size}; |
15125 | 7 |
use land2d::Land2D; |
8 |
||
9 |
pub fn fppoint_round(point: &FPPoint) -> Point { |
|
15275 | 10 |
Point::new(point.x().round(), point.y().round()) |
15125 | 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 { |
|
15268 | 74 |
pub pairs: Vec<(GearId, Option<GearId>)>, |
15125 | 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 |
||
15268 | 86 |
pub fn push( |
87 |
&mut self, |
|
88 |
contact_gear_id1: GearId, |
|
89 |
contact_gear_id2: Option<GearId>, |
|
90 |
position: &FPPoint, |
|
91 |
) { |
|
15125 | 92 |
self.pairs.push((contact_gear_id1, contact_gear_id2)); |
93 |
self.positions.push(fppoint_round(&position)); |
|
94 |
} |
|
15271
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15268
diff
changeset
|
95 |
|
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15268
diff
changeset
|
96 |
pub fn clear(&mut self) { |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15268
diff
changeset
|
97 |
self.pairs.clear(); |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15268
diff
changeset
|
98 |
self.positions.clear() |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15268
diff
changeset
|
99 |
} |
15125 | 100 |
} |
101 |
||
102 |
impl CollisionProcessor { |
|
15385 | 103 |
pub fn register_components(data: &mut GearDataManager) { |
104 |
data.register::<CollisionData>(); |
|
105 |
data.register::<ContactData>(); |
|
106 |
} |
|
107 |
||
15125 | 108 |
pub fn new(size: Size) -> Self { |
109 |
Self { |
|
110 |
grid: Grid::new(size), |
|
111 |
enabled_collisions: EnabledCollisionsCollection::new(), |
|
112 |
detected_collisions: DetectedCollisions::new(0), |
|
113 |
} |
|
114 |
} |
|
115 |
||
15385 | 116 |
pub fn add(&mut self, gear_id: GearId, gear_data: CollisionData) { |
117 |
self.grid.insert_static(gear_id, &gear_data.bounds); |
|
118 |
} |
|
119 |
||
120 |
pub fn remove(&mut self, gear_id: GearId) { |
|
121 |
self.grid.remove(gear_id); |
|
122 |
} |
|
123 |
||
124 |
pub fn get(&mut self, gear_id: GearId) -> Option<CollisionData> { |
|
125 |
None |
|
126 |
} |
|
127 |
||
15271
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15268
diff
changeset
|
128 |
pub fn process( |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15268
diff
changeset
|
129 |
&mut self, |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15268
diff
changeset
|
130 |
land: &Land2D<u32>, |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15268
diff
changeset
|
131 |
updates: &crate::physics::PositionUpdates, |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15268
diff
changeset
|
132 |
) -> &DetectedCollisions { |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15268
diff
changeset
|
133 |
self.detected_collisions.clear(); |
15266 | 134 |
for (id, old_position, new_position) in updates.iter() { |
135 |
self.grid.update_position(id, old_position, new_position) |
|
136 |
} |
|
15271
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15268
diff
changeset
|
137 |
|
15125 | 138 |
self.grid.check_collisions(&mut self.detected_collisions); |
139 |
||
140 |
for (gear_id, collision) in self.enabled_collisions.iter() { |
|
141 |
if collision |
|
142 |
.bounds |
|
143 |
.rows() |
|
144 |
.any(|(y, r)| (&land[y][r]).iter().any(|v| *v != 0)) |
|
145 |
{ |
|
146 |
self.detected_collisions |
|
15268 | 147 |
.push(gear_id, None, &collision.bounds.center) |
15125 | 148 |
} |
149 |
} |
|
15271
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15268
diff
changeset
|
150 |
|
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15268
diff
changeset
|
151 |
&self.detected_collisions |
15125 | 152 |
} |
153 |
} |