14716
|
1 |
use crate::common::{GearData, GearDataProcessor, GearId};
|
14178
|
2 |
use fpnum::*;
|
14716
|
3 |
use integral_geometry::{GridIndex, Point, Size};
|
14178
|
4 |
|
|
5 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
|
|
6 |
pub struct PhysicsData {
|
|
7 |
pub position: FPPoint,
|
|
8 |
pub velocity: FPPoint,
|
|
9 |
}
|
|
10 |
|
14179
|
11 |
impl GearData for PhysicsData {}
|
14178
|
12 |
|
14716
|
13 |
impl PhysicsData {
|
|
14 |
pub fn new(position: FPPoint, velocity: FPPoint) -> Self {
|
|
15 |
Self { position, velocity }
|
|
16 |
}
|
|
17 |
}
|
|
18 |
|
14178
|
19 |
pub struct DynamicPhysicsCollection {
|
|
20 |
gear_ids: Vec<GearId>,
|
|
21 |
positions: Vec<FPPoint>,
|
|
22 |
velocities: Vec<FPPoint>,
|
|
23 |
}
|
|
24 |
|
|
25 |
impl DynamicPhysicsCollection {
|
14179
|
26 |
fn new() -> Self {
|
|
27 |
Self {
|
|
28 |
gear_ids: Vec::new(),
|
|
29 |
positions: Vec::new(),
|
14716
|
30 |
velocities: Vec::new(),
|
14179
|
31 |
}
|
|
32 |
}
|
|
33 |
|
14178
|
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))> {
|
14716
|
45 |
self.gear_ids
|
|
46 |
.iter()
|
|
47 |
.cloned()
|
|
48 |
.zip(self.positions.iter_mut().zip(self.velocities.iter()))
|
14178
|
49 |
}
|
|
50 |
}
|
|
51 |
|
|
52 |
pub struct StaticPhysicsCollection {
|
|
53 |
gear_ids: Vec<GearId>,
|
14716
|
54 |
positions: Vec<FPPoint>,
|
14178
|
55 |
}
|
|
56 |
|
|
57 |
impl StaticPhysicsCollection {
|
14179
|
58 |
fn new() -> Self {
|
|
59 |
Self {
|
|
60 |
gear_ids: Vec::new(),
|
14716
|
61 |
positions: Vec::new(),
|
14179
|
62 |
}
|
|
63 |
}
|
|
64 |
|
14178
|
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>,
|
14716
|
76 |
position_updates: PositionUpdates,
|
14178
|
77 |
}
|
|
78 |
|
14179
|
79 |
pub struct PositionUpdates {
|
14178
|
80 |
pub gear_ids: Vec<GearId>,
|
14716
|
81 |
pub positions: Vec<FPPoint>,
|
14178
|
82 |
}
|
|
83 |
|
14179
|
84 |
impl PositionUpdates {
|
14178
|
85 |
pub fn new(capacity: usize) -> Self {
|
|
86 |
Self {
|
|
87 |
gear_ids: Vec::with_capacity(capacity),
|
|
88 |
positions: Vec::with_capacity(capacity),
|
|
89 |
}
|
|
90 |
}
|
|
91 |
|
|
92 |
pub fn push(&mut self, gear_id: GearId, position: &FPPoint) {
|
|
93 |
self.gear_ids.push(gear_id);
|
|
94 |
self.positions.push(*position);
|
|
95 |
}
|
|
96 |
}
|
|
97 |
|
|
98 |
impl PhysicsProcessor {
|
14179
|
99 |
pub fn new() -> Self {
|
|
100 |
PhysicsProcessor {
|
|
101 |
dynamic_physics: DynamicPhysicsCollection::new(),
|
|
102 |
static_physics: StaticPhysicsCollection::new(),
|
|
103 |
physics_cleanup: Vec::new(),
|
14716
|
104 |
position_updates: PositionUpdates::new(0),
|
14179
|
105 |
}
|
|
106 |
}
|
|
107 |
|
|
108 |
pub fn process(&mut self, time_step: FPNum) -> &PositionUpdates {
|
14178
|
109 |
for (gear_id, (pos, vel)) in self.dynamic_physics.iter_pos_update() {
|
|
110 |
*pos += *vel * time_step;
|
|
111 |
if !vel.is_zero() {
|
|
112 |
self.position_updates.push(gear_id, pos)
|
|
113 |
} else {
|
|
114 |
self.physics_cleanup.push(gear_id)
|
|
115 |
}
|
|
116 |
}
|
|
117 |
&self.position_updates
|
|
118 |
}
|
|
119 |
|
|
120 |
pub fn push(&mut self, gear_id: GearId, physics_data: PhysicsData) {
|
|
121 |
if physics_data.velocity.is_zero() {
|
|
122 |
self.static_physics.push(gear_id, physics_data);
|
|
123 |
} else {
|
|
124 |
self.dynamic_physics.push(gear_id, physics_data);
|
|
125 |
}
|
|
126 |
}
|
14179
|
127 |
}
|
|
128 |
|
|
129 |
impl GearDataProcessor<PhysicsData> for PhysicsProcessor {
|
|
130 |
fn add(&mut self, gear_id: GearId, gear_data: PhysicsData) {
|
|
131 |
if gear_data.velocity.is_zero() {
|
|
132 |
self.static_physics.push(gear_id, gear_data);
|
|
133 |
} else {
|
|
134 |
self.dynamic_physics.push(gear_id, gear_data);
|
|
135 |
}
|
|
136 |
}
|
14716
|
137 |
}
|