rust/hwphysics/src/physics.rs
author alfadur
Tue, 30 Jul 2019 19:53:23 +0300
changeset 15281 8095853811a6
parent 15275 66c987015f2d
child 15282 478d5372eb4a
permissions -rw-r--r--
add gear lookup to the physics processor
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15281
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
     1
use crate::common::{GearData, GearDataLookup, GearDataProcessor, GearId, LookupEntry, Millis};
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
     2
use fpnum::*;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
     3
use integral_geometry::{GridIndex, Point, Size};
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
     4
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
     5
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
     6
pub struct PhysicsData {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
     7
    pub position: FPPoint,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
     8
    pub velocity: FPPoint,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
     9
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    10
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    11
impl GearData for PhysicsData {}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    12
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    13
impl PhysicsData {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    14
    pub fn new(position: FPPoint, velocity: FPPoint) -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    15
        Self { position, velocity }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    16
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    17
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    18
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    19
pub struct DynamicPhysicsCollection {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    20
    gear_ids: Vec<GearId>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    21
    positions: Vec<FPPoint>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    22
    velocities: Vec<FPPoint>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    23
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    24
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    25
impl DynamicPhysicsCollection {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    26
    fn new() -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    27
        Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    28
            gear_ids: Vec::new(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    29
            positions: Vec::new(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    30
            velocities: Vec::new(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    31
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    32
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    33
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    34
    fn len(&self) -> usize {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    35
        self.gear_ids.len()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    36
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    37
15281
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
    38
    fn push(&mut self, gear_id: GearId, physics: PhysicsData) -> u16 {
15274
42b710b0f883 add gear allocator
alfadur
parents: 15270
diff changeset
    39
        self.gear_ids.push(gear_id);
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    40
        self.positions.push(physics.position);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    41
        self.velocities.push(physics.velocity);
15281
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
    42
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
    43
        (self.gear_ids.len() - 1) as u16
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    44
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    45
15281
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
    46
    fn remove(&mut self, index: usize) -> Option<GearId> {
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
    47
        self.gear_ids.swap_remove(index);
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
    48
        self.positions.swap_remove(index);
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
    49
        self.velocities.swap_remove(index);
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
    50
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
    51
        self.gear_ids.get(index).cloned()
15274
42b710b0f883 add gear allocator
alfadur
parents: 15270
diff changeset
    52
    }
42b710b0f883 add gear allocator
alfadur
parents: 15270
diff changeset
    53
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    54
    fn iter_pos_update(&mut self) -> impl Iterator<Item = (GearId, (&mut FPPoint, &FPPoint))> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    55
        self.gear_ids
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    56
            .iter()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    57
            .cloned()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    58
            .zip(self.positions.iter_mut().zip(self.velocities.iter()))
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    59
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    60
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    61
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    62
pub struct StaticPhysicsCollection {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    63
    gear_ids: Vec<GearId>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    64
    positions: Vec<FPPoint>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    65
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    66
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    67
impl StaticPhysicsCollection {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    68
    fn new() -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    69
        Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    70
            gear_ids: Vec::new(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    71
            positions: Vec::new(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    72
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    73
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    74
15281
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
    75
    fn push(&mut self, gear_id: GearId, physics: PhysicsData) -> u16 {
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    76
        self.gear_ids.push(gear_id);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    77
        self.positions.push(physics.position);
15281
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
    78
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
    79
        (self.gear_ids.len() - 1) as u16
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    80
    }
15274
42b710b0f883 add gear allocator
alfadur
parents: 15270
diff changeset
    81
15281
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
    82
    fn remove(&mut self, index: usize) -> Option<GearId> {
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
    83
        self.gear_ids.swap_remove(index);
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
    84
        self.positions.swap_remove(index);
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
    85
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
    86
        self.gear_ids.get(index).cloned()
15274
42b710b0f883 add gear allocator
alfadur
parents: 15270
diff changeset
    87
    }
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    88
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    89
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    90
pub struct PhysicsProcessor {
15281
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
    91
    gear_lookup: GearDataLookup<bool>,
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    92
    dynamic_physics: DynamicPhysicsCollection,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    93
    static_physics: StaticPhysicsCollection,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    94
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    95
    physics_cleanup: Vec<GearId>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    96
    position_updates: PositionUpdates,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    97
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    98
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    99
pub struct PositionUpdates {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   100
    pub gear_ids: Vec<GearId>,
15260
775d7efa4e5c save full shifts to position updates
alfadur
parents: 15120
diff changeset
   101
    pub shifts: Vec<(FPPoint, FPPoint)>,
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   102
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   103
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   104
impl PositionUpdates {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   105
    pub fn new(capacity: usize) -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   106
        Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   107
            gear_ids: Vec::with_capacity(capacity),
15260
775d7efa4e5c save full shifts to position updates
alfadur
parents: 15120
diff changeset
   108
            shifts: Vec::with_capacity(capacity),
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   109
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   110
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   111
15260
775d7efa4e5c save full shifts to position updates
alfadur
parents: 15120
diff changeset
   112
    pub fn push(&mut self, gear_id: GearId, old_position: &FPPoint, new_position: &FPPoint) {
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   113
        self.gear_ids.push(gear_id);
15260
775d7efa4e5c save full shifts to position updates
alfadur
parents: 15120
diff changeset
   114
        self.shifts.push((*old_position, *new_position));
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   115
    }
15261
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15260
diff changeset
   116
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15260
diff changeset
   117
    pub fn iter(&self) -> impl Iterator<Item = (GearId, &FPPoint, &FPPoint)> {
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15260
diff changeset
   118
        self.gear_ids
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15260
diff changeset
   119
            .iter()
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15260
diff changeset
   120
            .cloned()
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15260
diff changeset
   121
            .zip(self.shifts.iter())
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15260
diff changeset
   122
            .map(|(id, (from, to))| (id, from, to))
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15260
diff changeset
   123
    }
15266
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15261
diff changeset
   124
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15261
diff changeset
   125
    pub fn clear(&mut self) {
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15261
diff changeset
   126
        self.gear_ids.clear();
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15261
diff changeset
   127
        self.shifts.clear();
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15261
diff changeset
   128
    }
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   129
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   130
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   131
impl PhysicsProcessor {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   132
    pub fn new() -> Self {
15270
7446258fab98 add time events
alfadur
parents: 15266
diff changeset
   133
        Self {
15281
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
   134
            gear_lookup: GearDataLookup::new(),
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   135
            dynamic_physics: DynamicPhysicsCollection::new(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   136
            static_physics: StaticPhysicsCollection::new(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   137
            physics_cleanup: Vec::new(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   138
            position_updates: PositionUpdates::new(0),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   139
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   140
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   141
15275
66c987015f2d replace time with milliseconds
alfadur
parents: 15274
diff changeset
   142
    pub fn process(&mut self, time_step: Millis) -> &PositionUpdates {
66c987015f2d replace time with milliseconds
alfadur
parents: 15274
diff changeset
   143
        let fp_step = time_step.to_fixed();
15266
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15261
diff changeset
   144
        self.position_updates.clear();
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   145
        for (gear_id, (pos, vel)) in self.dynamic_physics.iter_pos_update() {
15260
775d7efa4e5c save full shifts to position updates
alfadur
parents: 15120
diff changeset
   146
            let old_pos = *pos;
15275
66c987015f2d replace time with milliseconds
alfadur
parents: 15274
diff changeset
   147
            *pos += *vel * fp_step;
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   148
            if !vel.is_zero() {
15260
775d7efa4e5c save full shifts to position updates
alfadur
parents: 15120
diff changeset
   149
                self.position_updates.push(gear_id, &old_pos, pos)
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   150
            } else {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   151
                self.physics_cleanup.push(gear_id)
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   152
            }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   153
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   154
        &self.position_updates
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   155
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   156
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   157
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   158
impl GearDataProcessor<PhysicsData> for PhysicsProcessor {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   159
    fn add(&mut self, gear_id: GearId, gear_data: PhysicsData) {
15281
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
   160
        let is_dynamic = !gear_data.velocity.is_zero();
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
   161
        let index = if is_dynamic {
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
   162
            self.dynamic_physics.push(gear_id, gear_data)
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   163
        } else {
15281
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
   164
            self.static_physics.push(gear_id, gear_data)
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
   165
        };
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
   166
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
   167
        self.gear_lookup[gear_id] = LookupEntry::new(index, is_dynamic);
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   168
    }
15274
42b710b0f883 add gear allocator
alfadur
parents: 15270
diff changeset
   169
42b710b0f883 add gear allocator
alfadur
parents: 15270
diff changeset
   170
    fn remove(&mut self, gear_id: GearId) {
15281
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
   171
        let location = self.gear_lookup[gear_id];
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
   172
        let relocated_gear_id = if location.value {
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
   173
            self.dynamic_physics.remove(location.index as usize)
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
   174
        } else {
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
   175
            self.static_physics.remove(location.index as usize)
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
   176
        };
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
   177
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
   178
        if let Some(id) = relocated_gear_id {
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
   179
            self.gear_lookup[id].index = location.index;
8095853811a6 add gear lookup to the physics processor
alfadur
parents: 15275
diff changeset
   180
        }
15274
42b710b0f883 add gear allocator
alfadur
parents: 15270
diff changeset
   181
    }
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   182
}