author | alfadur |
Tue, 30 Jul 2019 19:53:23 +0300 | |
changeset 15286 | 8095853811a6 |
parent 15280 | 66c987015f2d |
child 15287 | 478d5372eb4a |
permissions | -rw-r--r-- |
15286 | 1 |
use crate::common::{GearData, GearDataLookup, GearDataProcessor, GearId, LookupEntry, Millis}; |
15125 | 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 |
||
15286 | 38 |
fn push(&mut self, gear_id: GearId, physics: PhysicsData) -> u16 { |
15279 | 39 |
self.gear_ids.push(gear_id); |
15125 | 40 |
self.positions.push(physics.position); |
41 |
self.velocities.push(physics.velocity); |
|
15286 | 42 |
|
43 |
(self.gear_ids.len() - 1) as u16 |
|
15125 | 44 |
} |
45 |
||
15286 | 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() |
|
15279 | 52 |
} |
53 |
||
15125 | 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 |
||
15286 | 75 |
fn push(&mut self, gear_id: GearId, physics: PhysicsData) -> u16 { |
15125 | 76 |
self.gear_ids.push(gear_id); |
77 |
self.positions.push(physics.position); |
|
15286 | 78 |
|
79 |
(self.gear_ids.len() - 1) as u16 |
|
15125 | 80 |
} |
15279 | 81 |
|
15286 | 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() |
|
15279 | 87 |
} |
15125 | 88 |
} |
89 |
||
90 |
pub struct PhysicsProcessor { |
|
15286 | 91 |
gear_lookup: GearDataLookup<bool>, |
15125 | 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>, |
|
15265 | 101 |
pub shifts: Vec<(FPPoint, FPPoint)>, |
15125 | 102 |
} |
103 |
||
104 |
impl PositionUpdates { |
|
105 |
pub fn new(capacity: usize) -> Self { |
|
106 |
Self { |
|
107 |
gear_ids: Vec::with_capacity(capacity), |
|
15265 | 108 |
shifts: Vec::with_capacity(capacity), |
15125 | 109 |
} |
110 |
} |
|
111 |
||
15265 | 112 |
pub fn push(&mut self, gear_id: GearId, old_position: &FPPoint, new_position: &FPPoint) { |
15125 | 113 |
self.gear_ids.push(gear_id); |
15265 | 114 |
self.shifts.push((*old_position, *new_position)); |
15125 | 115 |
} |
15266 | 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 |
} |
|
15271
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15266
diff
changeset
|
124 |
|
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15266
diff
changeset
|
125 |
pub fn clear(&mut self) { |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15266
diff
changeset
|
126 |
self.gear_ids.clear(); |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15266
diff
changeset
|
127 |
self.shifts.clear(); |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15266
diff
changeset
|
128 |
} |
15125 | 129 |
} |
130 |
||
131 |
impl PhysicsProcessor { |
|
132 |
pub fn new() -> Self { |
|
15275 | 133 |
Self { |
15286 | 134 |
gear_lookup: GearDataLookup::new(), |
15125 | 135 |
dynamic_physics: DynamicPhysicsCollection::new(), |
136 |
static_physics: StaticPhysicsCollection::new(), |
|
137 |
physics_cleanup: Vec::new(), |
|
138 |
position_updates: PositionUpdates::new(0), |
|
139 |
} |
|
140 |
} |
|
141 |
||
15280 | 142 |
pub fn process(&mut self, time_step: Millis) -> &PositionUpdates { |
143 |
let fp_step = time_step.to_fixed(); |
|
15271
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15266
diff
changeset
|
144 |
self.position_updates.clear(); |
15125 | 145 |
for (gear_id, (pos, vel)) in self.dynamic_physics.iter_pos_update() { |
15265 | 146 |
let old_pos = *pos; |
15280 | 147 |
*pos += *vel * fp_step; |
15125 | 148 |
if !vel.is_zero() { |
15265 | 149 |
self.position_updates.push(gear_id, &old_pos, pos) |
15125 | 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) { |
|
15286 | 160 |
let is_dynamic = !gear_data.velocity.is_zero(); |
161 |
let index = if is_dynamic { |
|
162 |
self.dynamic_physics.push(gear_id, gear_data) |
|
15125 | 163 |
} else { |
15286 | 164 |
self.static_physics.push(gear_id, gear_data) |
165 |
}; |
|
166 |
||
167 |
self.gear_lookup[gear_id] = LookupEntry::new(index, is_dynamic); |
|
15125 | 168 |
} |
15279 | 169 |
|
170 |
fn remove(&mut self, gear_id: GearId) { |
|
15286 | 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 |
} |
|
15279 | 181 |
} |
15125 | 182 |
} |