author | alfadur |
Thu, 25 Jul 2019 19:58:19 +0300 | |
changeset 15275 | 7446258fab98 |
parent 15271 | b58f98bbc120 |
child 15279 | 42b710b0f883 |
permissions | -rw-r--r-- |
15125 | 1 |
use crate::common::{GearData, GearDataProcessor, GearId}; |
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 |
||
38 |
fn push(&mut self, id: GearId, physics: PhysicsData) { |
|
39 |
self.gear_ids.push(id); |
|
40 |
self.positions.push(physics.position); |
|
41 |
self.velocities.push(physics.velocity); |
|
42 |
} |
|
43 |
||
44 |
fn iter_pos_update(&mut self) -> impl Iterator<Item = (GearId, (&mut FPPoint, &FPPoint))> { |
|
45 |
self.gear_ids |
|
46 |
.iter() |
|
47 |
.cloned() |
|
48 |
.zip(self.positions.iter_mut().zip(self.velocities.iter())) |
|
49 |
} |
|
50 |
} |
|
51 |
||
52 |
pub struct StaticPhysicsCollection { |
|
53 |
gear_ids: Vec<GearId>, |
|
54 |
positions: Vec<FPPoint>, |
|
55 |
} |
|
56 |
||
57 |
impl StaticPhysicsCollection { |
|
58 |
fn new() -> Self { |
|
59 |
Self { |
|
60 |
gear_ids: Vec::new(), |
|
61 |
positions: Vec::new(), |
|
62 |
} |
|
63 |
} |
|
64 |
||
65 |
fn push(&mut self, gear_id: GearId, physics: PhysicsData) { |
|
66 |
self.gear_ids.push(gear_id); |
|
67 |
self.positions.push(physics.position); |
|
68 |
} |
|
69 |
} |
|
70 |
||
71 |
pub struct PhysicsProcessor { |
|
72 |
dynamic_physics: DynamicPhysicsCollection, |
|
73 |
static_physics: StaticPhysicsCollection, |
|
74 |
||
75 |
physics_cleanup: Vec<GearId>, |
|
76 |
position_updates: PositionUpdates, |
|
77 |
} |
|
78 |
||
79 |
pub struct PositionUpdates { |
|
80 |
pub gear_ids: Vec<GearId>, |
|
15265 | 81 |
pub shifts: Vec<(FPPoint, FPPoint)>, |
15125 | 82 |
} |
83 |
||
84 |
impl PositionUpdates { |
|
85 |
pub fn new(capacity: usize) -> Self { |
|
86 |
Self { |
|
87 |
gear_ids: Vec::with_capacity(capacity), |
|
15265 | 88 |
shifts: Vec::with_capacity(capacity), |
15125 | 89 |
} |
90 |
} |
|
91 |
||
15265 | 92 |
pub fn push(&mut self, gear_id: GearId, old_position: &FPPoint, new_position: &FPPoint) { |
15125 | 93 |
self.gear_ids.push(gear_id); |
15265 | 94 |
self.shifts.push((*old_position, *new_position)); |
15125 | 95 |
} |
15266 | 96 |
|
97 |
pub fn iter(&self) -> impl Iterator<Item = (GearId, &FPPoint, &FPPoint)> { |
|
98 |
self.gear_ids |
|
99 |
.iter() |
|
100 |
.cloned() |
|
101 |
.zip(self.shifts.iter()) |
|
102 |
.map(|(id, (from, to))| (id, from, to)) |
|
103 |
} |
|
15271
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15266
diff
changeset
|
104 |
|
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15266
diff
changeset
|
105 |
pub fn clear(&mut self) { |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15266
diff
changeset
|
106 |
self.gear_ids.clear(); |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15266
diff
changeset
|
107 |
self.shifts.clear(); |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15266
diff
changeset
|
108 |
} |
15125 | 109 |
} |
110 |
||
111 |
impl PhysicsProcessor { |
|
112 |
pub fn new() -> Self { |
|
15275 | 113 |
Self { |
15125 | 114 |
dynamic_physics: DynamicPhysicsCollection::new(), |
115 |
static_physics: StaticPhysicsCollection::new(), |
|
116 |
physics_cleanup: Vec::new(), |
|
117 |
position_updates: PositionUpdates::new(0), |
|
118 |
} |
|
119 |
} |
|
120 |
||
121 |
pub fn process(&mut self, time_step: FPNum) -> &PositionUpdates { |
|
15271
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15266
diff
changeset
|
122 |
self.position_updates.clear(); |
15125 | 123 |
for (gear_id, (pos, vel)) in self.dynamic_physics.iter_pos_update() { |
15265 | 124 |
let old_pos = *pos; |
15125 | 125 |
*pos += *vel * time_step; |
126 |
if !vel.is_zero() { |
|
15265 | 127 |
self.position_updates.push(gear_id, &old_pos, pos) |
15125 | 128 |
} else { |
129 |
self.physics_cleanup.push(gear_id) |
|
130 |
} |
|
131 |
} |
|
132 |
&self.position_updates |
|
133 |
} |
|
134 |
||
135 |
pub fn push(&mut self, gear_id: GearId, physics_data: PhysicsData) { |
|
136 |
if physics_data.velocity.is_zero() { |
|
137 |
self.static_physics.push(gear_id, physics_data); |
|
138 |
} else { |
|
139 |
self.dynamic_physics.push(gear_id, physics_data); |
|
140 |
} |
|
141 |
} |
|
142 |
} |
|
143 |
||
144 |
impl GearDataProcessor<PhysicsData> for PhysicsProcessor { |
|
145 |
fn add(&mut self, gear_id: GearId, gear_data: PhysicsData) { |
|
146 |
if gear_data.velocity.is_zero() { |
|
147 |
self.static_physics.push(gear_id, gear_data); |
|
148 |
} else { |
|
149 |
self.dynamic_physics.push(gear_id, gear_data); |
|
150 |
} |
|
151 |
} |
|
152 |
} |