add const generics to physics step
authoralfadur
Wed, 24 Mar 2021 00:11:49 +0300
changeset 15780 f4b563a9ac5e
parent 15778 50315e39d705
child 15781 b836876d7b2e
add const generics to physics step
rust/hwphysics/src/common.rs
rust/hwphysics/src/lib.rs
rust/hwphysics/src/physics.rs
--- a/rust/hwphysics/src/common.rs	Mon Mar 15 15:57:19 2021 -0400
+++ b/rust/hwphysics/src/common.rs	Wed Mar 24 00:11:49 2021 +0300
@@ -9,12 +9,12 @@
 
 impl Millis {
     #[inline]
-    pub fn new(value: u32) -> Self {
+    pub const fn new(value: u32) -> Self {
         Self(value)
     }
 
     #[inline]
-    pub fn get(self) -> u32 {
+    pub const fn get(self) -> u32 {
         self.0
     }
 
--- a/rust/hwphysics/src/lib.rs	Mon Mar 15 15:57:19 2021 -0400
+++ b/rust/hwphysics/src/lib.rs	Wed Mar 24 00:11:49 2021 +0300
@@ -48,12 +48,7 @@
     }
 
     pub fn step(&mut self, time_step: Millis, land: &Land2D<u32>) {
-        let updates = if time_step == Millis::new(1) {
-            self.physics.process_single_tick(&mut self.data)
-        } else {
-            self.physics
-                .process_multiple_ticks(&mut self.data, time_step)
-        };
+        let updates = self.physics.process(&mut self.data, time_step);
         let collisions = self.collision.process(land, &updates);
     }
 
--- a/rust/hwphysics/src/physics.rs	Mon Mar 15 15:57:19 2021 -0400
+++ b/rust/hwphysics/src/physics.rs	Wed Mar 24 00:11:49 2021 +0300
@@ -67,36 +67,28 @@
         }
     }
 
-    pub fn process_single_tick(&mut self, data: &mut GearDataManager) -> &PositionUpdates {
-        let gravity = FPPoint::unit_y() * self.gravity;
-        let wind = FPPoint::unit_x() * self.wind;
-
-        self.position_updates.clear();
-
-        data.iter()
-            .with_tags::<&AffectedByWind>()
-            .run(|(vel,): (&mut VelocityData,)| {
-                vel.0 += wind;
-            });
-
-        data.iter().run_id(
-            |gear_id, (pos, vel): (&mut PositionData, &mut VelocityData)| {
-                let old_pos = pos.0;
-                vel.0 += gravity;
-                pos.0 += vel.0;
-                self.position_updates.push(gear_id, &old_pos, &pos.0)
-            },
-        );
-
-        &self.position_updates
-    }
-
-    pub fn process_multiple_ticks(
+    pub fn process(
         &mut self,
         data: &mut GearDataManager,
         time_step: Millis,
     ) -> &PositionUpdates {
-        let fp_step = time_step.to_fixed();
+        if time_step == Millis::new(1) {
+            self.process_impl::<true>(data, time_step)
+        } else {
+            self.process_impl::<false>(data, time_step)
+        }
+    }
+
+    fn process_impl<const SINGLE_TICK: bool>(
+        &mut self,
+        data: &mut GearDataManager,
+        time_step: Millis,
+    ) -> &PositionUpdates {
+        let fp_step = if SINGLE_TICK {
+            fp!(1)
+        } else {
+            time_step.to_fixed()
+        };
         let gravity = FPPoint::unit_y() * (self.gravity * fp_step);
         let wind = FPPoint::unit_x() * (self.wind * fp_step);
 
@@ -112,7 +104,7 @@
             |gear_id, (pos, vel): (&mut PositionData, &mut VelocityData)| {
                 let old_pos = pos.0;
                 vel.0 += gravity;
-                pos.0 += vel.0 * fp_step;
+                pos.0 += if SINGLE_TICK { vel.0 } else { vel.0 * fp_step };
                 self.position_updates.push(gear_id, &old_pos, &pos.0)
             },
         );