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