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