author | alfadur |
Wed, 28 Aug 2019 23:06:34 +0300 | |
changeset 15380 | 6e3e5be8b2e2 |
parent 15282 | 478d5372eb4a |
child 15381 | 52844baced17 |
permissions | -rw-r--r-- |
15380 | 1 |
use crate::{ |
2 |
common::{GearId, Millis}, |
|
3 |
data::GearDataManager, |
|
4 |
}; |
|
15120 | 5 |
use fpnum::*; |
6 |
||
7 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
|
15380 | 8 |
#[repr(transparent)] |
9 |
pub struct PositionData(pub FPPoint); |
|
15120 | 10 |
|
15380 | 11 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
12 |
#[repr(transparent)] |
|
13 |
pub struct VelocityData(pub FPPoint); |
|
15120 | 14 |
|
15 |
pub struct PhysicsProcessor { |
|
16 |
position_updates: PositionUpdates, |
|
17 |
} |
|
18 |
||
19 |
pub struct PositionUpdates { |
|
20 |
pub gear_ids: Vec<GearId>, |
|
15260 | 21 |
pub shifts: Vec<(FPPoint, FPPoint)>, |
15120 | 22 |
} |
23 |
||
24 |
impl PositionUpdates { |
|
25 |
pub fn new(capacity: usize) -> Self { |
|
26 |
Self { |
|
27 |
gear_ids: Vec::with_capacity(capacity), |
|
15260 | 28 |
shifts: Vec::with_capacity(capacity), |
15120 | 29 |
} |
30 |
} |
|
31 |
||
15260 | 32 |
pub fn push(&mut self, gear_id: GearId, old_position: &FPPoint, new_position: &FPPoint) { |
15120 | 33 |
self.gear_ids.push(gear_id); |
15260 | 34 |
self.shifts.push((*old_position, *new_position)); |
15120 | 35 |
} |
15261 | 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 |
} |
|
15266
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15261
diff
changeset
|
44 |
|
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15261
diff
changeset
|
45 |
pub fn clear(&mut self) { |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15261
diff
changeset
|
46 |
self.gear_ids.clear(); |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15261
diff
changeset
|
47 |
self.shifts.clear(); |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15261
diff
changeset
|
48 |
} |
15120 | 49 |
} |
50 |
||
51 |
impl PhysicsProcessor { |
|
15380 | 52 |
pub fn register_components(data: &mut GearDataManager) { |
53 |
data.register::<PositionData>(); |
|
54 |
data.register::<VelocityData>(); |
|
55 |
} |
|
56 |
||
15120 | 57 |
pub fn new() -> Self { |
15270 | 58 |
Self { |
15380 | 59 |
position_updates: PositionUpdates::new(64), |
15120 | 60 |
} |
61 |
} |
|
62 |
||
15380 | 63 |
pub fn process(&mut self, data: &mut GearDataManager, time_step: Millis) -> &PositionUpdates { |
15275 | 64 |
let fp_step = time_step.to_fixed(); |
15266
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15261
diff
changeset
|
65 |
self.position_updates.clear(); |
15380 | 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 |
||
15120 | 77 |
&self.position_updates |
78 |
} |
|
79 |
} |