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 |
}
|
|
96 |
}
|
|
97 |
|
|
98 |
impl PhysicsProcessor {
|
|
99 |
pub fn new() -> Self {
|
|
100 |
PhysicsProcessor {
|
|
101 |
dynamic_physics: DynamicPhysicsCollection::new(),
|
|
102 |
static_physics: StaticPhysicsCollection::new(),
|
|
103 |
physics_cleanup: Vec::new(),
|
|
104 |
position_updates: PositionUpdates::new(0),
|
|
105 |
}
|
|
106 |
}
|
|
107 |
|
|
108 |
pub fn process(&mut self, time_step: FPNum) -> &PositionUpdates {
|
|
109 |
for (gear_id, (pos, vel)) in self.dynamic_physics.iter_pos_update() {
|
15265
|
110 |
let old_pos = *pos;
|
15125
|
111 |
*pos += *vel * time_step;
|
|
112 |
if !vel.is_zero() {
|
15265
|
113 |
self.position_updates.push(gear_id, &old_pos, pos)
|
15125
|
114 |
} else {
|
|
115 |
self.physics_cleanup.push(gear_id)
|
|
116 |
}
|
|
117 |
}
|
|
118 |
&self.position_updates
|
|
119 |
}
|
|
120 |
|
|
121 |
pub fn push(&mut self, gear_id: GearId, physics_data: PhysicsData) {
|
|
122 |
if physics_data.velocity.is_zero() {
|
|
123 |
self.static_physics.push(gear_id, physics_data);
|
|
124 |
} else {
|
|
125 |
self.dynamic_physics.push(gear_id, physics_data);
|
|
126 |
}
|
|
127 |
}
|
|
128 |
}
|
|
129 |
|
|
130 |
impl GearDataProcessor<PhysicsData> for PhysicsProcessor {
|
|
131 |
fn add(&mut self, gear_id: GearId, gear_data: PhysicsData) {
|
|
132 |
if gear_data.velocity.is_zero() {
|
|
133 |
self.static_physics.push(gear_id, gear_data);
|
|
134 |
} else {
|
|
135 |
self.dynamic_physics.push(gear_id, gear_data);
|
|
136 |
}
|
|
137 |
}
|
|
138 |
}
|