clear intermediate result structures between iterations
authoralfadur
Thu, 25 Jul 2019 16:29:14 +0300
changeset 15287 b58f98bbc120
parent 15286 07e909ba4203
child 15288 22f2fd8a3d2c
clear intermediate result structures between iterations
rust/hwphysics/src/collision.rs
rust/hwphysics/src/lib.rs
rust/hwphysics/src/physics.rs
--- a/rust/hwphysics/src/collision.rs	Thu Jul 25 15:18:00 2019 +0200
+++ b/rust/hwphysics/src/collision.rs	Thu Jul 25 16:29:14 2019 +0300
@@ -100,6 +100,11 @@
         self.pairs.push((contact_gear_id1, contact_gear_id2));
         self.positions.push(fppoint_round(&position));
     }
+
+    pub fn clear(&mut self) {
+        self.pairs.clear();
+        self.positions.clear()
+    }
 }
 
 impl CollisionProcessor {
@@ -111,10 +116,16 @@
         }
     }
 
-    pub fn process(&mut self, land: &Land2D<u32>, updates: &crate::physics::PositionUpdates) {
+    pub fn process(
+        &mut self,
+        land: &Land2D<u32>,
+        updates: &crate::physics::PositionUpdates,
+    ) -> &DetectedCollisions {
+        self.detected_collisions.clear();
         for (id, old_position, new_position) in updates.iter() {
             self.grid.update_position(id, old_position, new_position)
         }
+
         self.grid.check_collisions(&mut self.detected_collisions);
 
         for (gear_id, collision) in self.enabled_collisions.iter() {
@@ -127,6 +138,8 @@
                     .push(gear_id, None, &collision.bounds.center)
             }
         }
+
+        &self.detected_collisions
     }
 }
 
--- a/rust/hwphysics/src/lib.rs	Thu Jul 25 15:18:00 2019 +0200
+++ b/rust/hwphysics/src/lib.rs	Thu Jul 25 16:29:14 2019 +0300
@@ -48,7 +48,7 @@
 
     pub fn step(&mut self, time_step: FPNum, land: &Land2D<u32>) {
         let updates = self.physics.process(time_step);
-        self.collision.process(land, &updates);
+        let collision = self.collision.process(land, &updates);
     }
 
     pub fn add_gear_data<T>(&mut self, gear_id: GearId, data: T)
--- a/rust/hwphysics/src/physics.rs	Thu Jul 25 15:18:00 2019 +0200
+++ b/rust/hwphysics/src/physics.rs	Thu Jul 25 16:29:14 2019 +0300
@@ -101,6 +101,11 @@
             .zip(self.shifts.iter())
             .map(|(id, (from, to))| (id, from, to))
     }
+
+    pub fn clear(&mut self) {
+        self.gear_ids.clear();
+        self.shifts.clear();
+    }
 }
 
 impl PhysicsProcessor {
@@ -114,6 +119,7 @@
     }
 
     pub fn process(&mut self, time_step: FPNum) -> &PositionUpdates {
+        self.position_updates.clear();
         for (gear_id, (pos, vel)) in self.dynamic_physics.iter_pos_update() {
             let old_pos = *pos;
             *pos += *vel * time_step;