From 0c1d71072fc0d976f7225125652192aac716d7f3 Mon Sep 17 00:00:00 2001 From: Mark Kilfoil Date: Sat, 4 Jul 2026 23:25:42 -0300 Subject: [PATCH] Fix VesselDistanceComparer throwing ArgumentException when vessel position is NaN Array.Sort() requires a consistent IComparer. The original implementation casts float subtraction to int: (int)(distA - distB). This produces inconsistent results when any vessel's transform position contains NaN (NaN - NaN = NaN; (int)NaN = 0 regardless of operand order), which violates the sort contract and causes an ArgumentException from Array.Sort() on every FixedUpdate. Also fixes a secondary issue where very large squared distances (> int.MaxValue) could overflow and produce incorrect ordering. Fix: replace (int)(distA - distB) with distA.CompareTo(distB), which handles NaN and large values correctly. --- Source/MASFlightComputerProxy.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/MASFlightComputerProxy.cs b/Source/MASFlightComputerProxy.cs index 0d752431..257d53ca 100644 --- a/Source/MASFlightComputerProxy.cs +++ b/Source/MASFlightComputerProxy.cs @@ -259,7 +259,7 @@ public int Compare(Vessel a, Vessel b) { float distA = Vector3.SqrMagnitude(a.GetTransform().position - vesselPosition); float distB = Vector3.SqrMagnitude(b.GetTransform().position - vesselPosition); - return (int)(distA - distB); + return distA.CompareTo(distB); } }