rust/hwphysics/src/physics.rs
author unc0rr
Tue, 04 Jun 2019 22:34:42 +0200
changeset 15125 febccab419b1
parent 14721 8e74d4eb89f5
child 15265 775d7efa4e5c
permissions -rw-r--r--
Apply dos2unix to rust sources
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15125
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
     1
use crate::common::{GearData, GearDataProcessor, GearId};
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
     2
use fpnum::*;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
     3
use integral_geometry::{GridIndex, Point, Size};
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
     4
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
     5
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
     6
pub struct PhysicsData {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
     7
    pub position: FPPoint,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
     8
    pub velocity: FPPoint,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
     9
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    10
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    11
impl GearData for PhysicsData {}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    12
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    13
impl PhysicsData {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    14
    pub fn new(position: FPPoint, velocity: FPPoint) -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    15
        Self { position, velocity }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    16
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    17
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    18
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    19
pub struct DynamicPhysicsCollection {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    20
    gear_ids: Vec<GearId>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    21
    positions: Vec<FPPoint>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    22
    velocities: Vec<FPPoint>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    23
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    24
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    25
impl DynamicPhysicsCollection {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    26
    fn new() -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    27
        Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    28
            gear_ids: Vec::new(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    29
            positions: Vec::new(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    30
            velocities: Vec::new(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    31
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    32
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    33
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    34
    fn len(&self) -> usize {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    35
        self.gear_ids.len()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    36
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    37
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    38
    fn push(&mut self, id: GearId, physics: PhysicsData) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    39
        self.gear_ids.push(id);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    40
        self.positions.push(physics.position);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    41
        self.velocities.push(physics.velocity);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    42
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    43
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    44
    fn iter_pos_update(&mut self) -> impl Iterator<Item = (GearId, (&mut FPPoint, &FPPoint))> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    45
        self.gear_ids
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    46
            .iter()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    47
            .cloned()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    48
            .zip(self.positions.iter_mut().zip(self.velocities.iter()))
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    49
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    50
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    51
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    52
pub struct StaticPhysicsCollection {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    53
    gear_ids: Vec<GearId>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    54
    positions: Vec<FPPoint>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    55
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    56
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    57
impl StaticPhysicsCollection {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    58
    fn new() -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    59
        Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    60
            gear_ids: Vec::new(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    61
            positions: Vec::new(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    62
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    63
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    64
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    65
    fn push(&mut self, gear_id: GearId, physics: PhysicsData) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    66
        self.gear_ids.push(gear_id);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    67
        self.positions.push(physics.position);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    68
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    69
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    70
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    71
pub struct PhysicsProcessor {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    72
    dynamic_physics: DynamicPhysicsCollection,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    73
    static_physics: StaticPhysicsCollection,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    74
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    75
    physics_cleanup: Vec<GearId>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    76
    position_updates: PositionUpdates,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    77
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    78
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    79
pub struct PositionUpdates {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    80
    pub gear_ids: Vec<GearId>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    81
    pub positions: Vec<FPPoint>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    82
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    83
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    84
impl PositionUpdates {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    85
    pub fn new(capacity: usize) -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    86
        Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    87
            gear_ids: Vec::with_capacity(capacity),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    88
            positions: Vec::with_capacity(capacity),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    89
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    90
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    91
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    92
    pub fn push(&mut self, gear_id: GearId, position: &FPPoint) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    93
        self.gear_ids.push(gear_id);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    94
        self.positions.push(*position);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    95
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    96
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    97
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    98
impl PhysicsProcessor {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
    99
    pub fn new() -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   100
        PhysicsProcessor {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   101
            dynamic_physics: DynamicPhysicsCollection::new(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   102
            static_physics: StaticPhysicsCollection::new(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   103
            physics_cleanup: Vec::new(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   104
            position_updates: PositionUpdates::new(0),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   105
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   106
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   107
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   108
    pub fn process(&mut self, time_step: FPNum) -> &PositionUpdates {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   109
        for (gear_id, (pos, vel)) in self.dynamic_physics.iter_pos_update() {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   110
            *pos += *vel * time_step;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   111
            if !vel.is_zero() {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   112
                self.position_updates.push(gear_id, pos)
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   113
            } else {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   114
                self.physics_cleanup.push(gear_id)
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   115
            }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   116
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   117
        &self.position_updates
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   118
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   119
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   120
    pub fn push(&mut self, gear_id: GearId, physics_data: PhysicsData) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   121
        if physics_data.velocity.is_zero() {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   122
            self.static_physics.push(gear_id, physics_data);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   123
        } else {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   124
            self.dynamic_physics.push(gear_id, physics_data);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   125
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   126
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   127
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   128
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   129
impl GearDataProcessor<PhysicsData> for PhysicsProcessor {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   130
    fn add(&mut self, gear_id: GearId, gear_data: PhysicsData) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   131
        if gear_data.velocity.is_zero() {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   132
            self.static_physics.push(gear_id, gear_data);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   133
        } else {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   134
            self.dynamic_physics.push(gear_id, gear_data);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   135
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   136
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14721
diff changeset
   137
}