rust/hwphysics/src/physics.rs
author S.D.
Tue, 27 Sep 2022 14:59:03 +0300
changeset 15900 fc3cb23fd26f
parent 15850 44b49f255e31
permissions -rw-r--r--
Allow to see rooms of incompatible versions in the lobby For the new clients the room version is shown in a separate column. There is also a hack for previous versions clients: the room vesion specifier is prepended to the room names for rooms of incompatible versions, and the server shows 'incompatible version' error if the client tries to join them.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15401
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15303
diff changeset
     1
use crate::{
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15303
diff changeset
     2
    common::{GearId, Millis},
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15303
diff changeset
     3
    data::GearDataManager,
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15303
diff changeset
     4
};
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
     5
use fpnum::*;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
     6
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
     7
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
15401
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15303
diff changeset
     8
#[repr(transparent)]
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15303
diff changeset
     9
pub struct PositionData(pub FPPoint);
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    10
15401
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15303
diff changeset
    11
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15303
diff changeset
    12
#[repr(transparent)]
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15303
diff changeset
    13
pub struct VelocityData(pub FPPoint);
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    14
15414
0ef770a40e75 add wind to physics processor
alfadur
parents: 15413
diff changeset
    15
pub struct AffectedByWind;
0ef770a40e75 add wind to physics processor
alfadur
parents: 15413
diff changeset
    16
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    17
pub struct PositionUpdates {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    18
    pub gear_ids: Vec<GearId>,
15281
775d7efa4e5c save full shifts to position updates
alfadur
parents: 15141
diff changeset
    19
    pub shifts: Vec<(FPPoint, FPPoint)>,
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    20
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    21
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    22
impl PositionUpdates {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    23
    pub fn new(capacity: usize) -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    24
        Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    25
            gear_ids: Vec::with_capacity(capacity),
15281
775d7efa4e5c save full shifts to position updates
alfadur
parents: 15141
diff changeset
    26
            shifts: Vec::with_capacity(capacity),
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    27
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    28
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    29
15281
775d7efa4e5c save full shifts to position updates
alfadur
parents: 15141
diff changeset
    30
    pub fn push(&mut self, gear_id: GearId, old_position: &FPPoint, new_position: &FPPoint) {
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    31
        self.gear_ids.push(gear_id);
15281
775d7efa4e5c save full shifts to position updates
alfadur
parents: 15141
diff changeset
    32
        self.shifts.push((*old_position, *new_position));
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    33
    }
15282
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15281
diff changeset
    34
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15281
diff changeset
    35
    pub fn iter(&self) -> impl Iterator<Item = (GearId, &FPPoint, &FPPoint)> {
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15281
diff changeset
    36
        self.gear_ids
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15281
diff changeset
    37
            .iter()
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15281
diff changeset
    38
            .cloned()
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15281
diff changeset
    39
            .zip(self.shifts.iter())
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15281
diff changeset
    40
            .map(|(id, (from, to))| (id, from, to))
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15281
diff changeset
    41
    }
15287
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15282
diff changeset
    42
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15282
diff changeset
    43
    pub fn clear(&mut self) {
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15282
diff changeset
    44
        self.gear_ids.clear();
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15282
diff changeset
    45
        self.shifts.clear();
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15282
diff changeset
    46
    }
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    47
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    48
15402
52844baced17 add gravity
alfadur
parents: 15401
diff changeset
    49
pub struct PhysicsProcessor {
52844baced17 add gravity
alfadur
parents: 15401
diff changeset
    50
    gravity: FPNum,
15414
0ef770a40e75 add wind to physics processor
alfadur
parents: 15413
diff changeset
    51
    wind: FPNum,
15402
52844baced17 add gravity
alfadur
parents: 15401
diff changeset
    52
    position_updates: PositionUpdates,
52844baced17 add gravity
alfadur
parents: 15401
diff changeset
    53
}
52844baced17 add gravity
alfadur
parents: 15401
diff changeset
    54
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    55
impl PhysicsProcessor {
15401
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15303
diff changeset
    56
    pub fn register_components(data: &mut GearDataManager) {
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15303
diff changeset
    57
        data.register::<PositionData>();
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15303
diff changeset
    58
        data.register::<VelocityData>();
15414
0ef770a40e75 add wind to physics processor
alfadur
parents: 15413
diff changeset
    59
        data.register::<AffectedByWind>();
15401
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15303
diff changeset
    60
    }
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15303
diff changeset
    61
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    62
    pub fn new() -> Self {
15291
7446258fab98 add time events
alfadur
parents: 15287
diff changeset
    63
        Self {
15404
701ad89a9f2a avoid time multiplication when not skipping ticks
alfadur
parents: 15403
diff changeset
    64
            gravity: fp!(1 / 10),
15414
0ef770a40e75 add wind to physics processor
alfadur
parents: 15413
diff changeset
    65
            wind: fp!(0),
15401
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15303
diff changeset
    66
            position_updates: PositionUpdates::new(64),
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    67
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    68
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    69
15850
44b49f255e31 add type safe power of two sizes
alfadur
parents: 15802
diff changeset
    70
    pub fn process(&mut self, data: &mut GearDataManager, time_step: Millis) -> &PositionUpdates {
15802
f4b563a9ac5e add const generics to physics step
alfadur
parents: 15414
diff changeset
    71
        if time_step == Millis::new(1) {
f4b563a9ac5e add const generics to physics step
alfadur
parents: 15414
diff changeset
    72
            self.process_impl::<true>(data, time_step)
f4b563a9ac5e add const generics to physics step
alfadur
parents: 15414
diff changeset
    73
        } else {
f4b563a9ac5e add const generics to physics step
alfadur
parents: 15414
diff changeset
    74
            self.process_impl::<false>(data, time_step)
f4b563a9ac5e add const generics to physics step
alfadur
parents: 15414
diff changeset
    75
        }
f4b563a9ac5e add const generics to physics step
alfadur
parents: 15414
diff changeset
    76
    }
f4b563a9ac5e add const generics to physics step
alfadur
parents: 15414
diff changeset
    77
f4b563a9ac5e add const generics to physics step
alfadur
parents: 15414
diff changeset
    78
    fn process_impl<const SINGLE_TICK: bool>(
f4b563a9ac5e add const generics to physics step
alfadur
parents: 15414
diff changeset
    79
        &mut self,
f4b563a9ac5e add const generics to physics step
alfadur
parents: 15414
diff changeset
    80
        data: &mut GearDataManager,
f4b563a9ac5e add const generics to physics step
alfadur
parents: 15414
diff changeset
    81
        time_step: Millis,
f4b563a9ac5e add const generics to physics step
alfadur
parents: 15414
diff changeset
    82
    ) -> &PositionUpdates {
f4b563a9ac5e add const generics to physics step
alfadur
parents: 15414
diff changeset
    83
        let fp_step = if SINGLE_TICK {
f4b563a9ac5e add const generics to physics step
alfadur
parents: 15414
diff changeset
    84
            fp!(1)
f4b563a9ac5e add const generics to physics step
alfadur
parents: 15414
diff changeset
    85
        } else {
f4b563a9ac5e add const generics to physics step
alfadur
parents: 15414
diff changeset
    86
            time_step.to_fixed()
f4b563a9ac5e add const generics to physics step
alfadur
parents: 15414
diff changeset
    87
        };
15414
0ef770a40e75 add wind to physics processor
alfadur
parents: 15413
diff changeset
    88
        let gravity = FPPoint::unit_y() * (self.gravity * fp_step);
0ef770a40e75 add wind to physics processor
alfadur
parents: 15413
diff changeset
    89
        let wind = FPPoint::unit_x() * (self.wind * fp_step);
0ef770a40e75 add wind to physics processor
alfadur
parents: 15413
diff changeset
    90
15287
b58f98bbc120 clear intermediate result structures between iterations
alfadur
parents: 15282
diff changeset
    91
        self.position_updates.clear();
15401
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15303
diff changeset
    92
15414
0ef770a40e75 add wind to physics processor
alfadur
parents: 15413
diff changeset
    93
        data.iter()
0ef770a40e75 add wind to physics processor
alfadur
parents: 15413
diff changeset
    94
            .with_tags::<&AffectedByWind>()
0ef770a40e75 add wind to physics processor
alfadur
parents: 15413
diff changeset
    95
            .run(|(vel,): (&mut VelocityData,)| {
0ef770a40e75 add wind to physics processor
alfadur
parents: 15413
diff changeset
    96
                vel.0 += wind;
0ef770a40e75 add wind to physics processor
alfadur
parents: 15413
diff changeset
    97
            });
0ef770a40e75 add wind to physics processor
alfadur
parents: 15413
diff changeset
    98
15413
b387a51705ac implement iteration with tags
alfadur
parents: 15404
diff changeset
    99
        data.iter().run_id(
15401
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15303
diff changeset
   100
            |gear_id, (pos, vel): (&mut PositionData, &mut VelocityData)| {
15403
6ad92b6ac43c remove velocity check
alfadur
parents: 15402
diff changeset
   101
                let old_pos = pos.0;
15414
0ef770a40e75 add wind to physics processor
alfadur
parents: 15413
diff changeset
   102
                vel.0 += gravity;
15802
f4b563a9ac5e add const generics to physics step
alfadur
parents: 15414
diff changeset
   103
                pos.0 += if SINGLE_TICK { vel.0 } else { vel.0 * fp_step };
15403
6ad92b6ac43c remove velocity check
alfadur
parents: 15402
diff changeset
   104
                self.position_updates.push(gear_id, &old_pos, &pos.0)
15401
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15303
diff changeset
   105
            },
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15303
diff changeset
   106
        );
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15303
diff changeset
   107
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
   108
        &self.position_updates
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
   109
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
   110
}