rust/hwphysics/src/physics.rs
changeset 15393 0ef770a40e75
parent 15392 b387a51705ac
child 15780 f4b563a9ac5e
equal deleted inserted replaced
15392:b387a51705ac 15393:0ef770a40e75
     9 pub struct PositionData(pub FPPoint);
     9 pub struct PositionData(pub FPPoint);
    10 
    10 
    11 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
    11 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
    12 #[repr(transparent)]
    12 #[repr(transparent)]
    13 pub struct VelocityData(pub FPPoint);
    13 pub struct VelocityData(pub FPPoint);
       
    14 
       
    15 pub struct AffectedByWind;
    14 
    16 
    15 pub struct PositionUpdates {
    17 pub struct PositionUpdates {
    16     pub gear_ids: Vec<GearId>,
    18     pub gear_ids: Vec<GearId>,
    17     pub shifts: Vec<(FPPoint, FPPoint)>,
    19     pub shifts: Vec<(FPPoint, FPPoint)>,
    18 }
    20 }
    44     }
    46     }
    45 }
    47 }
    46 
    48 
    47 pub struct PhysicsProcessor {
    49 pub struct PhysicsProcessor {
    48     gravity: FPNum,
    50     gravity: FPNum,
       
    51     wind: FPNum,
    49     position_updates: PositionUpdates,
    52     position_updates: PositionUpdates,
    50 }
    53 }
    51 
    54 
    52 impl PhysicsProcessor {
    55 impl PhysicsProcessor {
    53     pub fn register_components(data: &mut GearDataManager) {
    56     pub fn register_components(data: &mut GearDataManager) {
    54         data.register::<PositionData>();
    57         data.register::<PositionData>();
    55         data.register::<VelocityData>();
    58         data.register::<VelocityData>();
       
    59         data.register::<AffectedByWind>();
    56     }
    60     }
    57 
    61 
    58     pub fn new() -> Self {
    62     pub fn new() -> Self {
    59         Self {
    63         Self {
    60             gravity: fp!(1 / 10),
    64             gravity: fp!(1 / 10),
       
    65             wind: fp!(0),
    61             position_updates: PositionUpdates::new(64),
    66             position_updates: PositionUpdates::new(64),
    62         }
    67         }
    63     }
    68     }
    64 
    69 
    65     pub fn process_single_tick(&mut self, data: &mut GearDataManager) -> &PositionUpdates {
    70     pub fn process_single_tick(&mut self, data: &mut GearDataManager) -> &PositionUpdates {
       
    71         let gravity = FPPoint::unit_y() * self.gravity;
       
    72         let wind = FPPoint::unit_x() * self.wind;
       
    73 
    66         self.position_updates.clear();
    74         self.position_updates.clear();
       
    75 
       
    76         data.iter()
       
    77             .with_tags::<&AffectedByWind>()
       
    78             .run(|(vel,): (&mut VelocityData,)| {
       
    79                 vel.0 += wind;
       
    80             });
    67 
    81 
    68         data.iter().run_id(
    82         data.iter().run_id(
    69             |gear_id, (pos, vel): (&mut PositionData, &mut VelocityData)| {
    83             |gear_id, (pos, vel): (&mut PositionData, &mut VelocityData)| {
    70                 let old_pos = pos.0;
    84                 let old_pos = pos.0;
    71                 vel.0 -= self.gravity;
    85                 vel.0 += gravity;
    72                 pos.0 += vel.0;
    86                 pos.0 += vel.0;
    73                 self.position_updates.push(gear_id, &old_pos, &pos.0)
    87                 self.position_updates.push(gear_id, &old_pos, &pos.0)
    74             },
    88             },
    75         );
    89         );
    76 
    90 
    81         &mut self,
    95         &mut self,
    82         data: &mut GearDataManager,
    96         data: &mut GearDataManager,
    83         time_step: Millis,
    97         time_step: Millis,
    84     ) -> &PositionUpdates {
    98     ) -> &PositionUpdates {
    85         let fp_step = time_step.to_fixed();
    99         let fp_step = time_step.to_fixed();
       
   100         let gravity = FPPoint::unit_y() * (self.gravity * fp_step);
       
   101         let wind = FPPoint::unit_x() * (self.wind * fp_step);
       
   102 
    86         self.position_updates.clear();
   103         self.position_updates.clear();
       
   104 
       
   105         data.iter()
       
   106             .with_tags::<&AffectedByWind>()
       
   107             .run(|(vel,): (&mut VelocityData,)| {
       
   108                 vel.0 += wind;
       
   109             });
    87 
   110 
    88         data.iter().run_id(
   111         data.iter().run_id(
    89             |gear_id, (pos, vel): (&mut PositionData, &mut VelocityData)| {
   112             |gear_id, (pos, vel): (&mut PositionData, &mut VelocityData)| {
    90                 let old_pos = pos.0;
   113                 let old_pos = pos.0;
    91                 vel.0 -= self.gravity * fp_step;
   114                 vel.0 += gravity;
    92                 pos.0 += vel.0 * fp_step;
   115                 pos.0 += vel.0 * fp_step;
    93                 self.position_updates.push(gear_id, &old_pos, &pos.0)
   116                 self.position_updates.push(gear_id, &old_pos, &pos.0)
    94             },
   117             },
    95         );
   118         );
    96 
   119