rust/hwphysics/src/physics.rs
changeset 14179 abbb74b9cb62
parent 14178 a4c17cfaa4c9
child 14716 8e74d4eb89f5
equal deleted inserted replaced
14178:a4c17cfaa4c9 14179:abbb74b9cb62
     1 use crate::{
     1 use crate::{
     2     common::GearId
     2     common::{GearId, GearData, GearDataProcessor}
     3 };
     3 };
     4 use fpnum::*;
     4 use fpnum::*;
     5 use integral_geometry::{
     5 use integral_geometry::{
     6     Point, Size, GridIndex
     6     Point, Size, GridIndex
     7 };
     7 };
    10 pub struct PhysicsData {
    10 pub struct PhysicsData {
    11     pub position: FPPoint,
    11     pub position: FPPoint,
    12     pub velocity: FPPoint,
    12     pub velocity: FPPoint,
    13 }
    13 }
    14 
    14 
       
    15 impl GearData for PhysicsData {}
    15 
    16 
    16 pub struct DynamicPhysicsCollection {
    17 pub struct DynamicPhysicsCollection {
    17     gear_ids: Vec<GearId>,
    18     gear_ids: Vec<GearId>,
    18     positions: Vec<FPPoint>,
    19     positions: Vec<FPPoint>,
    19     velocities: Vec<FPPoint>,
    20     velocities: Vec<FPPoint>,
    20 }
    21 }
    21 
    22 
    22 impl DynamicPhysicsCollection {
    23 impl DynamicPhysicsCollection {
       
    24     fn new() -> Self {
       
    25         Self {
       
    26             gear_ids: Vec::new(),
       
    27             positions: Vec::new(),
       
    28             velocities: Vec::new()
       
    29         }
       
    30     }
       
    31 
    23     fn len(&self) -> usize {
    32     fn len(&self) -> usize {
    24         self.gear_ids.len()
    33         self.gear_ids.len()
    25     }
    34     }
    26 
    35 
    27     fn push(&mut self, id: GearId, physics: PhysicsData) {
    36     fn push(&mut self, id: GearId, physics: PhysicsData) {
    41     gear_ids: Vec<GearId>,
    50     gear_ids: Vec<GearId>,
    42     positions: Vec<FPPoint>
    51     positions: Vec<FPPoint>
    43 }
    52 }
    44 
    53 
    45 impl StaticPhysicsCollection {
    54 impl StaticPhysicsCollection {
       
    55     fn new() -> Self {
       
    56         Self {
       
    57             gear_ids: Vec::new(),
       
    58             positions: Vec::new()
       
    59         }
       
    60     }
       
    61 
    46     fn push(&mut self, gear_id: GearId, physics: PhysicsData) {
    62     fn push(&mut self, gear_id: GearId, physics: PhysicsData) {
    47         self.gear_ids.push(gear_id);
    63         self.gear_ids.push(gear_id);
    48         self.positions.push(physics.position);
    64         self.positions.push(physics.position);
    49     }
    65     }
    50 }
    66 }
    52 pub struct PhysicsProcessor {
    68 pub struct PhysicsProcessor {
    53     dynamic_physics: DynamicPhysicsCollection,
    69     dynamic_physics: DynamicPhysicsCollection,
    54     static_physics: StaticPhysicsCollection,
    70     static_physics: StaticPhysicsCollection,
    55 
    71 
    56     physics_cleanup: Vec<GearId>,
    72     physics_cleanup: Vec<GearId>,
    57     position_updates: PositionUpdate
    73     position_updates: PositionUpdates
    58 }
    74 }
    59 
    75 
    60 pub struct PositionUpdate {
    76 pub struct PositionUpdates {
    61     pub gear_ids: Vec<GearId>,
    77     pub gear_ids: Vec<GearId>,
    62     pub positions: Vec<FPPoint>
    78     pub positions: Vec<FPPoint>
    63 }
    79 }
    64 
    80 
    65 impl PositionUpdate {
    81 impl PositionUpdates {
    66     pub fn new(capacity: usize) -> Self {
    82     pub fn new(capacity: usize) -> Self {
    67         Self {
    83         Self {
    68             gear_ids: Vec::with_capacity(capacity),
    84             gear_ids: Vec::with_capacity(capacity),
    69             positions: Vec::with_capacity(capacity),
    85             positions: Vec::with_capacity(capacity),
    70         }
    86         }
    75         self.positions.push(*position);
    91         self.positions.push(*position);
    76     }
    92     }
    77 }
    93 }
    78 
    94 
    79 impl PhysicsProcessor {
    95 impl PhysicsProcessor {
    80     pub fn process(&mut self, time_step: FPNum) -> &PositionUpdate {
    96     pub fn new() -> Self {
       
    97         PhysicsProcessor {
       
    98             dynamic_physics: DynamicPhysicsCollection::new(),
       
    99             static_physics: StaticPhysicsCollection::new(),
       
   100             physics_cleanup: Vec::new(),
       
   101             position_updates: PositionUpdates::new(0)
       
   102         }
       
   103     }
       
   104 
       
   105     pub fn process(&mut self, time_step: FPNum) -> &PositionUpdates {
    81         for (gear_id, (pos, vel)) in self.dynamic_physics.iter_pos_update() {
   106         for (gear_id, (pos, vel)) in self.dynamic_physics.iter_pos_update() {
    82             *pos += *vel * time_step;
   107             *pos += *vel * time_step;
    83             if !vel.is_zero() {
   108             if !vel.is_zero() {
    84                 self.position_updates.push(gear_id, pos)
   109                 self.position_updates.push(gear_id, pos)
    85             } else {
   110             } else {
    95         } else {
   120         } else {
    96             self.dynamic_physics.push(gear_id, physics_data);
   121             self.dynamic_physics.push(gear_id, physics_data);
    97         }
   122         }
    98     }
   123     }
    99 }
   124 }
       
   125 
       
   126 impl GearDataProcessor<PhysicsData> for PhysicsProcessor {
       
   127     fn add(&mut self, gear_id: GearId, gear_data: PhysicsData) {
       
   128         if gear_data.velocity.is_zero() {
       
   129             self.static_physics.push(gear_id, gear_data);
       
   130         } else {
       
   131             self.dynamic_physics.push(gear_id, gear_data);
       
   132         }
       
   133     }
       
   134 }