rust/hwphysics/src/physics.rs
author alfadur
Thu, 29 Aug 2019 00:06:31 +0300
changeset 15381 52844baced17
parent 15380 6e3e5be8b2e2
child 15382 6ad92b6ac43c
permissions -rw-r--r--
add gravity
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15380
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15282
diff changeset
     1
use crate::{
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15282
diff changeset
     2
    common::{GearId, Millis},
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15282
diff changeset
     3
    data::GearDataManager,
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15282
diff changeset
     4
};
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
     5
use fpnum::*;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
     6
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
     7
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
15380
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15282
diff changeset
     8
#[repr(transparent)]
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15282
diff changeset
     9
pub struct PositionData(pub FPPoint);
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    10
15380
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15282
diff changeset
    11
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15282
diff changeset
    12
#[repr(transparent)]
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15282
diff changeset
    13
pub struct VelocityData(pub FPPoint);
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    14
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    15
pub struct PositionUpdates {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    16
    pub gear_ids: Vec<GearId>,
15260
775d7efa4e5c save full shifts to position updates
alfadur
parents: 15120
diff changeset
    17
    pub shifts: Vec<(FPPoint, FPPoint)>,
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    18
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    19
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    20
impl PositionUpdates {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    21
    pub fn new(capacity: usize) -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    22
        Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    23
            gear_ids: Vec::with_capacity(capacity),
15260
775d7efa4e5c save full shifts to position updates
alfadur
parents: 15120
diff changeset
    24
            shifts: Vec::with_capacity(capacity),
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    25
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    26
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    27
15260
775d7efa4e5c save full shifts to position updates
alfadur
parents: 15120
diff changeset
    28
    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
    29
        self.gear_ids.push(gear_id);
15260
775d7efa4e5c save full shifts to position updates
alfadur
parents: 15120
diff changeset
    30
        self.shifts.push((*old_position, *new_position));
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    31
    }
15261
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15260
diff changeset
    32
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15260
diff changeset
    33
    pub fn iter(&self) -> impl Iterator<Item = (GearId, &FPPoint, &FPPoint)> {
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15260
diff changeset
    34
        self.gear_ids
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15260
diff changeset
    35
            .iter()
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15260
diff changeset
    36
            .cloned()
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15260
diff changeset
    37
            .zip(self.shifts.iter())
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15260
diff changeset
    38
            .map(|(id, (from, to))| (id, from, to))
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15260
diff changeset
    39
    }
15266
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15261
diff changeset
    40
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15261
diff changeset
    41
    pub fn clear(&mut self) {
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15261
diff changeset
    42
        self.gear_ids.clear();
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15261
diff changeset
    43
        self.shifts.clear();
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15261
diff changeset
    44
    }
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    45
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    46
15381
52844baced17 add gravity
alfadur
parents: 15380
diff changeset
    47
pub struct PhysicsProcessor {
52844baced17 add gravity
alfadur
parents: 15380
diff changeset
    48
    gravity: FPNum,
52844baced17 add gravity
alfadur
parents: 15380
diff changeset
    49
    position_updates: PositionUpdates,
52844baced17 add gravity
alfadur
parents: 15380
diff changeset
    50
}
52844baced17 add gravity
alfadur
parents: 15380
diff changeset
    51
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    52
impl PhysicsProcessor {
15380
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15282
diff changeset
    53
    pub fn register_components(data: &mut GearDataManager) {
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15282
diff changeset
    54
        data.register::<PositionData>();
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15282
diff changeset
    55
        data.register::<VelocityData>();
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15282
diff changeset
    56
    }
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15282
diff changeset
    57
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    58
    pub fn new() -> Self {
15270
7446258fab98 add time events
alfadur
parents: 15266
diff changeset
    59
        Self {
15381
52844baced17 add gravity
alfadur
parents: 15380
diff changeset
    60
            gravity: fp!(100),
15380
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15282
diff changeset
    61
            position_updates: PositionUpdates::new(64),
15120
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
15380
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15282
diff changeset
    65
    pub fn process(&mut self, data: &mut GearDataManager, time_step: Millis) -> &PositionUpdates {
15275
66c987015f2d replace time with milliseconds
alfadur
parents: 15274
diff changeset
    66
        let fp_step = time_step.to_fixed();
15266
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15261
diff changeset
    67
        self.position_updates.clear();
15380
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15282
diff changeset
    68
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15282
diff changeset
    69
        data.iter_id(
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15282
diff changeset
    70
            |gear_id, (pos, vel): (&mut PositionData, &mut VelocityData)| {
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15282
diff changeset
    71
                if !vel.0.is_zero() {
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15282
diff changeset
    72
                    let old_pos = pos.0;
15381
52844baced17 add gravity
alfadur
parents: 15380
diff changeset
    73
                    vel.0 -= self.gravity * fp_step;
15380
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15282
diff changeset
    74
                    pos.0 += vel.0 * fp_step;
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15282
diff changeset
    75
                    self.position_updates.push(gear_id, &old_pos, &pos.0)
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15282
diff changeset
    76
                }
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15282
diff changeset
    77
            },
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15282
diff changeset
    78
        );
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15282
diff changeset
    79
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    80
        &self.position_updates
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    81
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14716
diff changeset
    82
}