From 41f7bcf391c03d5459fd90e43bbdd9ca49e94c44 Mon Sep 17 00:00:00 2001 From: Niladri Das Date: Sat, 3 Jan 2026 08:37:03 +0530 Subject: [PATCH 1/2] fix: prevent nan in collisions by checking minimum distance Add epsilon check to avoid division by very small distance in handle_collisions, preventing potential NaN velocities and data corruption. --- engine/physics/physics_engine.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engine/physics/physics_engine.rs b/engine/physics/physics_engine.rs index 45bd98d..290196d 100644 --- a/engine/physics/physics_engine.rs +++ b/engine/physics/physics_engine.rs @@ -66,7 +66,7 @@ impl PhysicsEngine { let rad2 = self.objects[j].radius; let diff = pos1 - pos2; let distance = diff.norm(); - if distance < rad1 + rad2 && distance > 0.0 { + if distance < rad1 + rad2 && distance > 1e-6 { // Elastic collision in 2D let m1 = self.objects[i].mass; let m2 = self.objects[j].mass; From 8f5eb34f5172999400073121b01cde10f38cd142 Mon Sep 17 00:00:00 2001 From: Niladri Das Date: Sat, 3 Jan 2026 08:42:40 +0530 Subject: [PATCH 2/2] fix: enable collisions and epsilon Uncomment handle_collisions call in simulate. Define MIN_DISTANCE_EPSILON constant. --- engine/physics/physics_engine.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/engine/physics/physics_engine.rs b/engine/physics/physics_engine.rs index 290196d..34e436a 100644 --- a/engine/physics/physics_engine.rs +++ b/engine/physics/physics_engine.rs @@ -1,5 +1,8 @@ use nalgebra::Vector2; +/// Minimum distance threshold to avoid division by very small values in collision calculations +const MIN_DISTANCE_EPSILON: f32 = 1e-6; + #[derive(Clone, Debug)] pub struct Object { pub position: Vector2, @@ -66,7 +69,7 @@ impl PhysicsEngine { let rad2 = self.objects[j].radius; let diff = pos1 - pos2; let distance = diff.norm(); - if distance < rad1 + rad2 && distance > 1e-6 { + if distance < rad1 + rad2 && distance > MIN_DISTANCE_EPSILON { // Elastic collision in 2D let m1 = self.objects[i].mass; let m2 = self.objects[j].mass; @@ -123,7 +126,7 @@ impl PhysicsEngine { } // Handle object-object collisions - // self.handle_collisions(); + self.handle_collisions(); for obj in &self.objects { log::info!(