author | alfadur |
Tue, 30 Jul 2019 19:53:23 +0300 | |
changeset 15281 | 8095853811a6 |
parent 15275 | 66c987015f2d |
child 15282 | 478d5372eb4a |
permissions | -rw-r--r-- |
15281 | 1 |
use crate::common::{GearData, GearDataLookup, GearDataProcessor, GearId, LookupEntry, Millis}; |
15120 | 2 |
use fpnum::*; |
3 |
use integral_geometry::{GridIndex, Point, Size}; |
|
4 |
||
5 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
|
6 |
pub struct PhysicsData { |
|
7 |
pub position: FPPoint, |
|
8 |
pub velocity: FPPoint, |
|
9 |
} |
|
10 |
||
11 |
impl GearData for PhysicsData {} |
|
12 |
||
13 |
impl PhysicsData { |
|
14 |
pub fn new(position: FPPoint, velocity: FPPoint) -> Self { |
|
15 |
Self { position, velocity } |
|
16 |
} |
|
17 |
} |
|
18 |
||
19 |
pub struct DynamicPhysicsCollection { |
|
20 |
gear_ids: Vec<GearId>, |
|
21 |
positions: Vec<FPPoint>, |
|
22 |
velocities: Vec<FPPoint>, |
|
23 |
} |
|
24 |
||
25 |
impl DynamicPhysicsCollection { |
|
26 |
fn new() -> Self { |
|
27 |
Self { |
|
28 |
gear_ids: Vec::new(), |
|
29 |
positions: Vec::new(), |
|
30 |
velocities: Vec::new(), |
|
31 |
} |
|
32 |
} |
|
33 |
||
34 |
fn len(&self) -> usize { |
|
35 |
self.gear_ids.len() |
|
36 |
} |
|
37 |
||
15281 | 38 |
fn push(&mut self, gear_id: GearId, physics: PhysicsData) -> u16 { |
15274 | 39 |
self.gear_ids.push(gear_id); |
15120 | 40 |
self.positions.push(physics.position); |
41 |
self.velocities.push(physics.velocity); |
|
15281 | 42 |
|
43 |
(self.gear_ids.len() - 1) as u16 |
|
15120 | 44 |
} |
45 |
||
15281 | 46 |
fn remove(&mut self, index: usize) -> Option<GearId> { |
47 |
self.gear_ids.swap_remove(index); |
|
48 |
self.positions.swap_remove(index); |
|
49 |
self.velocities.swap_remove(index); |
|
50 |
||
51 |
self.gear_ids.get(index).cloned() |
|
15274 | 52 |
} |
53 |
||
15120 | 54 |
fn iter_pos_update(&mut self) -> impl Iterator<Item = (GearId, (&mut FPPoint, &FPPoint))> { |
55 |
self.gear_ids |
|
56 |
.iter() |
|
57 |
.cloned() |
|
58 |
.zip(self.positions.iter_mut().zip(self.velocities.iter())) |
|
59 |
} |
|
60 |
} |
|
61 |
||
62 |
pub struct StaticPhysicsCollection { |
|
63 |
gear_ids: Vec<GearId>, |
|
64 |
positions: Vec<FPPoint>, |
|
65 |
} |
|
66 |
||
67 |
impl StaticPhysicsCollection { |
|
68 |
fn new() -> Self { |
|
69 |
Self { |
|
70 |
gear_ids: Vec::new(), |
|
71 |
positions: Vec::new(), |
|
72 |
} |
|
73 |
} |
|
74 |
||
15281 | 75 |
fn push(&mut self, gear_id: GearId, physics: PhysicsData) -> u16 { |
15120 | 76 |
self.gear_ids.push(gear_id); |
77 |
self.positions.push(physics.position); |
|
15281 | 78 |
|
79 |
(self.gear_ids.len() - 1) as u16 |
|
15120 | 80 |
} |
15274 | 81 |
|
15281 | 82 |
fn remove(&mut self, index: usize) -> Option<GearId> { |
83 |
self.gear_ids.swap_remove(index); |
|
84 |
self.positions.swap_remove(index); |
|
85 |
||
86 |
self.gear_ids.get(index).cloned() |
|
15274 | 87 |
} |
15120 | 88 |
} |
89 |
||
90 |
pub struct PhysicsProcessor { |
|
15281 | 91 |
gear_lookup: GearDataLookup<bool>, |
15120 | 92 |
dynamic_physics: DynamicPhysicsCollection, |
93 |
static_physics: StaticPhysicsCollection, |
|
94 |
||
95 |
physics_cleanup: Vec<GearId>, |
|
96 |
position_updates: PositionUpdates, |
|
97 |
} |
|
98 |
||
99 |
pub struct PositionUpdates { |
|
100 |
pub gear_ids: Vec<GearId>, |
|
15260 | 101 |
pub shifts: Vec<(FPPoint, FPPoint)>, |
15120 | 102 |
} |
103 |
||
104 |
impl PositionUpdates { |
|
105 |
pub fn new(capacity: usize) -> Self { |
|
106 |
Self { |
|
107 |
gear_ids: Vec::with_capacity(capacity), |
|
15260 | 108 |
shifts: Vec::with_capacity(capacity), |
15120 | 109 |
} |
110 |
} |
|
111 |
||
15260 | 112 |
pub fn push(&mut self, gear_id: GearId, old_position: &FPPoint, new_position: &FPPoint) { |
15120 | 113 |
self.gear_ids.push(gear_id); |
15260 | 114 |
self.shifts.push((*old_position, *new_position)); |
15120 | 115 |
} |
15261 | 116 |
|
117 |
pub fn iter(&self) -> impl Iterator<Item = (GearId, &FPPoint, &FPPoint)> { |
|
118 |
self.gear_ids |
|
119 |
.iter() |
|
120 |
.cloned() |
|
121 |
.zip(self.shifts.iter()) |
|
122 |
.map(|(id, (from, to))| (id, from, to)) |
|
123 |
} |
|
15266
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15261
diff
changeset
|
124 |
|
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15261
diff
changeset
|
125 |
pub fn clear(&mut self) { |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15261
diff
changeset
|
126 |
self.gear_ids.clear(); |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15261
diff
changeset
|
127 |
self.shifts.clear(); |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15261
diff
changeset
|
128 |
} |
15120 | 129 |
} |
130 |
||
131 |
impl PhysicsProcessor { |
|
132 |
pub fn new() -> Self { |
|
15270 | 133 |
Self { |
15281 | 134 |
gear_lookup: GearDataLookup::new(), |
15120 | 135 |
dynamic_physics: DynamicPhysicsCollection::new(), |
136 |
static_physics: StaticPhysicsCollection::new(), |
|
137 |
physics_cleanup: Vec::new(), |
|
138 |
position_updates: PositionUpdates::new(0), |
|
139 |
} |
|
140 |
} |
|
141 |
||
15275 | 142 |
pub fn process(&mut self, time_step: Millis) -> &PositionUpdates { |
143 |
let fp_step = time_step.to_fixed(); |
|
15266
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15261
diff
changeset
|
144 |
self.position_updates.clear(); |
15120 | 145 |
for (gear_id, (pos, vel)) in self.dynamic_physics.iter_pos_update() { |
15260 | 146 |
let old_pos = *pos; |
15275 | 147 |
*pos += *vel * fp_step; |
15120 | 148 |
if !vel.is_zero() { |
15260 | 149 |
self.position_updates.push(gear_id, &old_pos, pos) |
15120 | 150 |
} else { |
151 |
self.physics_cleanup.push(gear_id) |
|
152 |
} |
|
153 |
} |
|
154 |
&self.position_updates |
|
155 |
} |
|
156 |
} |
|
157 |
||
158 |
impl GearDataProcessor<PhysicsData> for PhysicsProcessor { |
|
159 |
fn add(&mut self, gear_id: GearId, gear_data: PhysicsData) { |
|
15281 | 160 |
let is_dynamic = !gear_data.velocity.is_zero(); |
161 |
let index = if is_dynamic { |
|
162 |
self.dynamic_physics.push(gear_id, gear_data) |
|
15120 | 163 |
} else { |
15281 | 164 |
self.static_physics.push(gear_id, gear_data) |
165 |
}; |
|
166 |
||
167 |
self.gear_lookup[gear_id] = LookupEntry::new(index, is_dynamic); |
|
15120 | 168 |
} |
15274 | 169 |
|
170 |
fn remove(&mut self, gear_id: GearId) { |
|
15281 | 171 |
let location = self.gear_lookup[gear_id]; |
172 |
let relocated_gear_id = if location.value { |
|
173 |
self.dynamic_physics.remove(location.index as usize) |
|
174 |
} else { |
|
175 |
self.static_physics.remove(location.index as usize) |
|
176 |
}; |
|
177 |
||
178 |
if let Some(id) = relocated_gear_id { |
|
179 |
self.gear_lookup[id].index = location.index; |
|
180 |
} |
|
15274 | 181 |
} |
15120 | 182 |
} |