rust/hwphysics/src/physics.rs
changeset 15780 f4b563a9ac5e
parent 15393 0ef770a40e75
child 15828 44b49f255e31
equal deleted inserted replaced
15778:50315e39d705 15780:f4b563a9ac5e
    65             wind: fp!(0),
    65             wind: fp!(0),
    66             position_updates: PositionUpdates::new(64),
    66             position_updates: PositionUpdates::new(64),
    67         }
    67         }
    68     }
    68     }
    69 
    69 
    70     pub fn process_single_tick(&mut self, data: &mut GearDataManager) -> &PositionUpdates {
    70     pub fn process(
    71         let gravity = FPPoint::unit_y() * self.gravity;
       
    72         let wind = FPPoint::unit_x() * self.wind;
       
    73 
       
    74         self.position_updates.clear();
       
    75 
       
    76         data.iter()
       
    77             .with_tags::<&AffectedByWind>()
       
    78             .run(|(vel,): (&mut VelocityData,)| {
       
    79                 vel.0 += wind;
       
    80             });
       
    81 
       
    82         data.iter().run_id(
       
    83             |gear_id, (pos, vel): (&mut PositionData, &mut VelocityData)| {
       
    84                 let old_pos = pos.0;
       
    85                 vel.0 += gravity;
       
    86                 pos.0 += vel.0;
       
    87                 self.position_updates.push(gear_id, &old_pos, &pos.0)
       
    88             },
       
    89         );
       
    90 
       
    91         &self.position_updates
       
    92     }
       
    93 
       
    94     pub fn process_multiple_ticks(
       
    95         &mut self,
    71         &mut self,
    96         data: &mut GearDataManager,
    72         data: &mut GearDataManager,
    97         time_step: Millis,
    73         time_step: Millis,
    98     ) -> &PositionUpdates {
    74     ) -> &PositionUpdates {
    99         let fp_step = time_step.to_fixed();
    75         if time_step == Millis::new(1) {
       
    76             self.process_impl::<true>(data, time_step)
       
    77         } else {
       
    78             self.process_impl::<false>(data, time_step)
       
    79         }
       
    80     }
       
    81 
       
    82     fn process_impl<const SINGLE_TICK: bool>(
       
    83         &mut self,
       
    84         data: &mut GearDataManager,
       
    85         time_step: Millis,
       
    86     ) -> &PositionUpdates {
       
    87         let fp_step = if SINGLE_TICK {
       
    88             fp!(1)
       
    89         } else {
       
    90             time_step.to_fixed()
       
    91         };
   100         let gravity = FPPoint::unit_y() * (self.gravity * fp_step);
    92         let gravity = FPPoint::unit_y() * (self.gravity * fp_step);
   101         let wind = FPPoint::unit_x() * (self.wind * fp_step);
    93         let wind = FPPoint::unit_x() * (self.wind * fp_step);
   102 
    94 
   103         self.position_updates.clear();
    95         self.position_updates.clear();
   104 
    96 
   110 
   102 
   111         data.iter().run_id(
   103         data.iter().run_id(
   112             |gear_id, (pos, vel): (&mut PositionData, &mut VelocityData)| {
   104             |gear_id, (pos, vel): (&mut PositionData, &mut VelocityData)| {
   113                 let old_pos = pos.0;
   105                 let old_pos = pos.0;
   114                 vel.0 += gravity;
   106                 vel.0 += gravity;
   115                 pos.0 += vel.0 * fp_step;
   107                 pos.0 += if SINGLE_TICK { vel.0 } else { vel.0 * fp_step };
   116                 self.position_updates.push(gear_id, &old_pos, &pos.0)
   108                 self.position_updates.push(gear_id, &old_pos, &pos.0)
   117             },
   109             },
   118         );
   110         );
   119 
   111 
   120         &self.position_updates
   112         &self.position_updates