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