Skip to content

Commit 70a098b

Browse files
author
Mark Kilfoil
committed
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.
1 parent 109f31c commit 70a098b

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

Source/MASFlightComputerProxy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public int Compare(Vessel a, Vessel b)
259259
{
260260
float distA = Vector3.SqrMagnitude(a.GetTransform().position - vesselPosition);
261261
float distB = Vector3.SqrMagnitude(b.GetTransform().position - vesselPosition);
262-
return (int)(distA - distB);
262+
return distA.CompareTo(distB);
263263
}
264264
}
265265

0 commit comments

Comments
 (0)