author | alfadur |
Wed, 28 Aug 2019 23:06:34 +0300 | |
changeset 15401 | 6e3e5be8b2e2 |
parent 15303 | 478d5372eb4a |
child 15402 | 52844baced17 |
permissions | -rw-r--r-- |
15401 | 1 |
use crate::{ |
2 |
common::{GearId, Millis}, |
|
3 |
data::GearDataManager, |
|
4 |
}; |
|
15141 | 5 |
use fpnum::*; |
6 |
||
7 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
|
15401 | 8 |
#[repr(transparent)] |
9 |
pub struct PositionData(pub FPPoint); |
|
15141 | 10 |
|
15401 | 11 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
12 |
#[repr(transparent)] |
|
13 |
pub struct VelocityData(pub FPPoint); |
|
15141 | 14 |
|
15 |
pub struct PhysicsProcessor { |
|
16 |
position_updates: PositionUpdates, |
|
17 |
} |
|
18 |
||
19 |
pub struct PositionUpdates { |
|
20 |
pub gear_ids: Vec<GearId>, |
|
15281 | 21 |
pub shifts: Vec<(FPPoint, FPPoint)>, |
15141 | 22 |
} |
23 |
||
24 |
impl PositionUpdates { |
|
25 |
pub fn new(capacity: usize) -> Self { |
|
26 |
Self { |
|
27 |
gear_ids: Vec::with_capacity(capacity), |
|
15281 | 28 |
shifts: Vec::with_capacity(capacity), |
15141 | 29 |
} |
30 |
} |
|
31 |
||
15281 | 32 |
pub fn push(&mut self, gear_id: GearId, old_position: &FPPoint, new_position: &FPPoint) { |
15141 | 33 |
self.gear_ids.push(gear_id); |
15281 | 34 |
self.shifts.push((*old_position, *new_position)); |
15141 | 35 |
} |
15282 | 36 |
|
37 |
pub fn iter(&self) -> impl Iterator<Item = (GearId, &FPPoint, &FPPoint)> { |
|
38 |
self.gear_ids |
|
39 |
.iter() |
|
40 |
.cloned() |
|
41 |
.zip(self.shifts.iter()) |
|
42 |
.map(|(id, (from, to))| (id, from, to)) |
|
43 |
} |
|
15287
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15282
diff
changeset
|
44 |
|
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15282
diff
changeset
|
45 |
pub fn clear(&mut self) { |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15282
diff
changeset
|
46 |
self.gear_ids.clear(); |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15282
diff
changeset
|
47 |
self.shifts.clear(); |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15282
diff
changeset
|
48 |
} |
15141 | 49 |
} |
50 |
||
51 |
impl PhysicsProcessor { |
|
15401 | 52 |
pub fn register_components(data: &mut GearDataManager) { |
53 |
data.register::<PositionData>(); |
|
54 |
data.register::<VelocityData>(); |
|
55 |
} |
|
56 |
||
15141 | 57 |
pub fn new() -> Self { |
15291 | 58 |
Self { |
15401 | 59 |
position_updates: PositionUpdates::new(64), |
15141 | 60 |
} |
61 |
} |
|
62 |
||
15401 | 63 |
pub fn process(&mut self, data: &mut GearDataManager, time_step: Millis) -> &PositionUpdates { |
15296 | 64 |
let fp_step = time_step.to_fixed(); |
15287
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15282
diff
changeset
|
65 |
self.position_updates.clear(); |
15401 | 66 |
|
67 |
data.iter_id( |
|
68 |
|gear_id, (pos, vel): (&mut PositionData, &mut VelocityData)| { |
|
69 |
if !vel.0.is_zero() { |
|
70 |
let old_pos = pos.0; |
|
71 |
pos.0 += vel.0 * fp_step; |
|
72 |
self.position_updates.push(gear_id, &old_pos, &pos.0) |
|
73 |
} |
|
74 |
}, |
|
75 |
); |
|
76 |
||
15141 | 77 |
&self.position_updates |
78 |
} |
|
79 |
} |