avoid time multiplication when not skipping ticks
authoralfadur
Thu, 29 Aug 2019 00:20:41 +0300
changeset 15404 701ad89a9f2a
parent 15403 6ad92b6ac43c
child 15405 1c6d5656157c
avoid time multiplication when not skipping ticks
rust/hwphysics/src/common.rs
rust/hwphysics/src/lib.rs
rust/hwphysics/src/physics.rs
--- a/rust/hwphysics/src/common.rs	Thu Aug 29 00:08:03 2019 +0300
+++ b/rust/hwphysics/src/common.rs	Thu Aug 29 00:20:41 2019 +0300
@@ -20,7 +20,7 @@
 
     #[inline]
     pub fn to_fixed(self) -> FPNum {
-        FPNum::new(self.0 as i32, 1000)
+        FPNum::new(self.0 as i32, 1)
     }
 }
 
--- a/rust/hwphysics/src/lib.rs	Thu Aug 29 00:08:03 2019 +0300
+++ b/rust/hwphysics/src/lib.rs	Thu Aug 29 00:20:41 2019 +0300
@@ -53,7 +53,12 @@
     }
 
     pub fn step(&mut self, time_step: Millis, land: &Land2D<u32>) {
-        let updates = self.physics.process(&mut self.data, time_step);
+        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 collisions = self.collision.process(land, &updates);
         let events = self.time.process(time_step);
     }
--- a/rust/hwphysics/src/physics.rs	Thu Aug 29 00:08:03 2019 +0300
+++ b/rust/hwphysics/src/physics.rs	Thu Aug 29 00:20:41 2019 +0300
@@ -57,12 +57,31 @@
 
     pub fn new() -> Self {
         Self {
-            gravity: fp!(100),
+            gravity: fp!(1 / 10),
             position_updates: PositionUpdates::new(64),
         }
     }
 
-    pub fn process(&mut self, data: &mut GearDataManager, time_step: Millis) -> &PositionUpdates {
+    pub fn process_single_tick(&mut self, data: &mut GearDataManager) -> &PositionUpdates {
+        self.position_updates.clear();
+
+        data.iter_id(
+            |gear_id, (pos, vel): (&mut PositionData, &mut VelocityData)| {
+                let old_pos = pos.0;
+                vel.0 -= self.gravity;
+                pos.0 += vel.0;
+                self.position_updates.push(gear_id, &old_pos, &pos.0)
+            },
+        );
+
+        &self.position_updates
+    }
+
+    pub fn process_multiple_ticks(
+        &mut self,
+        data: &mut GearDataManager,
+        time_step: Millis,
+    ) -> &PositionUpdates {
         let fp_step = time_step.to_fixed();
         self.position_updates.clear();