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