15120
|
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>,
|
15260
|
81 |
pub shifts: Vec<(FPPoint, FPPoint)>,
|
15120
|
82 |
}
|
|
83 |
|
|
84 |
impl PositionUpdates {
|
|
85 |
pub fn new(capacity: usize) -> Self {
|
|
86 |
Self {
|
|
87 |
gear_ids: Vec::with_capacity(capacity),
|
15260
|
88 |
shifts: Vec::with_capacity(capacity),
|
15120
|
89 |
}
|
|
90 |
}
|
|
91 |
|
15260
|
92 |
pub fn push(&mut self, gear_id: GearId, old_position: &FPPoint, new_position: &FPPoint) {
|
15120
|
93 |
self.gear_ids.push(gear_id);
|
15260
|
94 |
self.shifts.push((*old_position, *new_position));
|
15120
|
95 |
}
|
15261
|
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 |
}
|
15120
|
104 |
}
|
|
105 |
|
|
106 |
impl PhysicsProcessor {
|
|
107 |
pub fn new() -> Self {
|
|
108 |
PhysicsProcessor {
|
|
109 |
dynamic_physics: DynamicPhysicsCollection::new(),
|
|
110 |
static_physics: StaticPhysicsCollection::new(),
|
|
111 |
physics_cleanup: Vec::new(),
|
|
112 |
position_updates: PositionUpdates::new(0),
|
|
113 |
}
|
|
114 |
}
|
|
115 |
|
|
116 |
pub fn process(&mut self, time_step: FPNum) -> &PositionUpdates {
|
|
117 |
for (gear_id, (pos, vel)) in self.dynamic_physics.iter_pos_update() {
|
15260
|
118 |
let old_pos = *pos;
|
15120
|
119 |
*pos += *vel * time_step;
|
|
120 |
if !vel.is_zero() {
|
15260
|
121 |
self.position_updates.push(gear_id, &old_pos, pos)
|
15120
|
122 |
} else {
|
|
123 |
self.physics_cleanup.push(gear_id)
|
|
124 |
}
|
|
125 |
}
|
|
126 |
&self.position_updates
|
|
127 |
}
|
|
128 |
|
|
129 |
pub fn push(&mut self, gear_id: GearId, physics_data: PhysicsData) {
|
|
130 |
if physics_data.velocity.is_zero() {
|
|
131 |
self.static_physics.push(gear_id, physics_data);
|
|
132 |
} else {
|
|
133 |
self.dynamic_physics.push(gear_id, physics_data);
|
|
134 |
}
|
|
135 |
}
|
|
136 |
}
|
|
137 |
|
|
138 |
impl GearDataProcessor<PhysicsData> for PhysicsProcessor {
|
|
139 |
fn add(&mut self, gear_id: GearId, gear_data: PhysicsData) {
|
|
140 |
if gear_data.velocity.is_zero() {
|
|
141 |
self.static_physics.push(gear_id, gear_data);
|
|
142 |
} else {
|
|
143 |
self.dynamic_physics.push(gear_id, gear_data);
|
|
144 |
}
|
|
145 |
}
|
|
146 |
}
|