rust/hwphysics/src/physics.rs
author alfadur
Wed, 24 Jul 2019 23:33:06 +0300
changeset 15261 501dfa1c8deb
parent 15260 775d7efa4e5c
child 15266 b58f98bbc120
permissions -rw-r--r--
update collision grid with position changes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
     1
use crate::common::{GearData, GearDataProcessor, GearId};
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
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    38
    fn push(&mut self, id: GearId, physics: PhysicsData) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    39
        self.gear_ids.push(id);
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);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    42
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    43
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    44
    fn iter_pos_update(&mut self) -> impl Iterator<Item = (GearId, (&mut FPPoint, &FPPoint))> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    45
        self.gear_ids
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    46
            .iter()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    47
            .cloned()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    48
            .zip(self.positions.iter_mut().zip(self.velocities.iter()))
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    49
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    50
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    51
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    52
pub struct StaticPhysicsCollection {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    53
    gear_ids: Vec<GearId>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    54
    positions: Vec<FPPoint>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    55
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    56
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    57
impl StaticPhysicsCollection {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    58
    fn new() -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    59
        Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    60
            gear_ids: Vec::new(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    61
            positions: Vec::new(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    62
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    63
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    64
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    65
    fn push(&mut self, gear_id: GearId, physics: PhysicsData) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    66
        self.gear_ids.push(gear_id);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    67
        self.positions.push(physics.position);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    68
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    69
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    70
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    71
pub struct PhysicsProcessor {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    72
    dynamic_physics: DynamicPhysicsCollection,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    73
    static_physics: StaticPhysicsCollection,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    74
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    75
    physics_cleanup: Vec<GearId>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    76
    position_updates: PositionUpdates,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    77
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    78
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    79
pub struct PositionUpdates {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    80
    pub gear_ids: Vec<GearId>,
15260
775d7efa4e5c save full shifts to position updates
alfadur
parents: 15120
diff changeset
    81
    pub shifts: Vec<(FPPoint, FPPoint)>,
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    82
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    83
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    84
impl PositionUpdates {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    85
    pub fn new(capacity: usize) -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    86
        Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    87
            gear_ids: Vec::with_capacity(capacity),
15260
775d7efa4e5c save full shifts to position updates
alfadur
parents: 15120
diff changeset
    88
            shifts: Vec::with_capacity(capacity),
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    89
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    90
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    91
15260
775d7efa4e5c save full shifts to position updates
alfadur
parents: 15120
diff changeset
    92
    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
    93
        self.gear_ids.push(gear_id);
15260
775d7efa4e5c save full shifts to position updates
alfadur
parents: 15120
diff changeset
    94
        self.shifts.push((*old_position, *new_position));
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    95
    }
15261
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15260
diff changeset
    96
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15260
diff changeset
    97
    pub fn iter(&self) -> impl Iterator<Item = (GearId, &FPPoint, &FPPoint)> {
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15260
diff changeset
    98
        self.gear_ids
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15260
diff changeset
    99
            .iter()
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15260
diff changeset
   100
            .cloned()
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15260
diff changeset
   101
            .zip(self.shifts.iter())
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15260
diff changeset
   102
            .map(|(id, (from, to))| (id, from, to))
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15260
diff changeset
   103
    }
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   104
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   105
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   106
impl PhysicsProcessor {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   107
    pub fn new() -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   108
        PhysicsProcessor {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   109
            dynamic_physics: DynamicPhysicsCollection::new(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   110
            static_physics: StaticPhysicsCollection::new(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   111
            physics_cleanup: Vec::new(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   112
            position_updates: PositionUpdates::new(0),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   113
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   114
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   115
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   116
    pub fn process(&mut self, time_step: FPNum) -> &PositionUpdates {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   117
        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
   118
            let old_pos = *pos;
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   119
            *pos += *vel * time_step;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   120
            if !vel.is_zero() {
15260
775d7efa4e5c save full shifts to position updates
alfadur
parents: 15120
diff changeset
   121
                self.position_updates.push(gear_id, &old_pos, pos)
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   122
            } else {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   123
                self.physics_cleanup.push(gear_id)
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   124
            }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   125
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   126
        &self.position_updates
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   127
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   128
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   129
    pub fn push(&mut self, gear_id: GearId, physics_data: PhysicsData) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   130
        if physics_data.velocity.is_zero() {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   131
            self.static_physics.push(gear_id, physics_data);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   132
        } else {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   133
            self.dynamic_physics.push(gear_id, physics_data);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   134
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   135
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   136
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   137
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   138
impl GearDataProcessor<PhysicsData> for PhysicsProcessor {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   139
    fn add(&mut self, gear_id: GearId, gear_data: PhysicsData) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   140
        if gear_data.velocity.is_zero() {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   141
            self.static_physics.push(gear_id, gear_data);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   142
        } else {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   143
            self.dynamic_physics.push(gear_id, gear_data);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   144
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   145
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
   146
}