rust/hwphysics/src/physics.rs
changeset 14716 8e74d4eb89f5
parent 14179 abbb74b9cb62
child 15120 febccab419b1
equal deleted inserted replaced
14715:e519802076e9 14716:8e74d4eb89f5
     1 use crate::{
     1 use crate::common::{GearData, GearDataProcessor, GearId};
     2     common::{GearId, GearData, GearDataProcessor}
       
     3 };
       
     4 use fpnum::*;
     2 use fpnum::*;
     5 use integral_geometry::{
     3 use integral_geometry::{GridIndex, Point, Size};
     6     Point, Size, GridIndex
       
     7 };
       
     8 
     4 
     9 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
     5 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
    10 pub struct PhysicsData {
     6 pub struct PhysicsData {
    11     pub position: FPPoint,
     7     pub position: FPPoint,
    12     pub velocity: FPPoint,
     8     pub velocity: FPPoint,
    13 }
     9 }
    14 
    10 
    15 impl GearData for PhysicsData {}
    11 impl GearData for PhysicsData {}
       
    12 
       
    13 impl PhysicsData {
       
    14     pub fn new(position: FPPoint, velocity: FPPoint) -> Self {
       
    15         Self { position, velocity }
       
    16     }
       
    17 }
    16 
    18 
    17 pub struct DynamicPhysicsCollection {
    19 pub struct DynamicPhysicsCollection {
    18     gear_ids: Vec<GearId>,
    20     gear_ids: Vec<GearId>,
    19     positions: Vec<FPPoint>,
    21     positions: Vec<FPPoint>,
    20     velocities: Vec<FPPoint>,
    22     velocities: Vec<FPPoint>,
    23 impl DynamicPhysicsCollection {
    25 impl DynamicPhysicsCollection {
    24     fn new() -> Self {
    26     fn new() -> Self {
    25         Self {
    27         Self {
    26             gear_ids: Vec::new(),
    28             gear_ids: Vec::new(),
    27             positions: Vec::new(),
    29             positions: Vec::new(),
    28             velocities: Vec::new()
    30             velocities: Vec::new(),
    29         }
    31         }
    30     }
    32     }
    31 
    33 
    32     fn len(&self) -> usize {
    34     fn len(&self) -> usize {
    33         self.gear_ids.len()
    35         self.gear_ids.len()
    38         self.positions.push(physics.position);
    40         self.positions.push(physics.position);
    39         self.velocities.push(physics.velocity);
    41         self.velocities.push(physics.velocity);
    40     }
    42     }
    41 
    43 
    42     fn iter_pos_update(&mut self) -> impl Iterator<Item = (GearId, (&mut FPPoint, &FPPoint))> {
    44     fn iter_pos_update(&mut self) -> impl Iterator<Item = (GearId, (&mut FPPoint, &FPPoint))> {
    43         self.gear_ids.iter().cloned()
    45         self.gear_ids
    44             .zip(self.positions.iter_mut()
    46             .iter()
    45                 .zip(self.velocities.iter()))
    47             .cloned()
       
    48             .zip(self.positions.iter_mut().zip(self.velocities.iter()))
    46     }
    49     }
    47 }
    50 }
    48 
    51 
    49 pub struct StaticPhysicsCollection {
    52 pub struct StaticPhysicsCollection {
    50     gear_ids: Vec<GearId>,
    53     gear_ids: Vec<GearId>,
    51     positions: Vec<FPPoint>
    54     positions: Vec<FPPoint>,
    52 }
    55 }
    53 
    56 
    54 impl StaticPhysicsCollection {
    57 impl StaticPhysicsCollection {
    55     fn new() -> Self {
    58     fn new() -> Self {
    56         Self {
    59         Self {
    57             gear_ids: Vec::new(),
    60             gear_ids: Vec::new(),
    58             positions: Vec::new()
    61             positions: Vec::new(),
    59         }
    62         }
    60     }
    63     }
    61 
    64 
    62     fn push(&mut self, gear_id: GearId, physics: PhysicsData) {
    65     fn push(&mut self, gear_id: GearId, physics: PhysicsData) {
    63         self.gear_ids.push(gear_id);
    66         self.gear_ids.push(gear_id);
    68 pub struct PhysicsProcessor {
    71 pub struct PhysicsProcessor {
    69     dynamic_physics: DynamicPhysicsCollection,
    72     dynamic_physics: DynamicPhysicsCollection,
    70     static_physics: StaticPhysicsCollection,
    73     static_physics: StaticPhysicsCollection,
    71 
    74 
    72     physics_cleanup: Vec<GearId>,
    75     physics_cleanup: Vec<GearId>,
    73     position_updates: PositionUpdates
    76     position_updates: PositionUpdates,
    74 }
    77 }
    75 
    78 
    76 pub struct PositionUpdates {
    79 pub struct PositionUpdates {
    77     pub gear_ids: Vec<GearId>,
    80     pub gear_ids: Vec<GearId>,
    78     pub positions: Vec<FPPoint>
    81     pub positions: Vec<FPPoint>,
    79 }
    82 }
    80 
    83 
    81 impl PositionUpdates {
    84 impl PositionUpdates {
    82     pub fn new(capacity: usize) -> Self {
    85     pub fn new(capacity: usize) -> Self {
    83         Self {
    86         Self {
    96     pub fn new() -> Self {
    99     pub fn new() -> Self {
    97         PhysicsProcessor {
   100         PhysicsProcessor {
    98             dynamic_physics: DynamicPhysicsCollection::new(),
   101             dynamic_physics: DynamicPhysicsCollection::new(),
    99             static_physics: StaticPhysicsCollection::new(),
   102             static_physics: StaticPhysicsCollection::new(),
   100             physics_cleanup: Vec::new(),
   103             physics_cleanup: Vec::new(),
   101             position_updates: PositionUpdates::new(0)
   104             position_updates: PositionUpdates::new(0),
   102         }
   105         }
   103     }
   106     }
   104 
   107 
   105     pub fn process(&mut self, time_step: FPNum) -> &PositionUpdates {
   108     pub fn process(&mut self, time_step: FPNum) -> &PositionUpdates {
   106         for (gear_id, (pos, vel)) in self.dynamic_physics.iter_pos_update() {
   109         for (gear_id, (pos, vel)) in self.dynamic_physics.iter_pos_update() {