improve stability for checks
authoralfadur
Sat, 13 Oct 2018 23:49:23 +0300
changeset 13894 9954f9b800f5
parent 13893 7163f5ec3ce2
child 13895 353c999b1558
improve stability for checks
hedgewars/uGearsUtils.pas
--- a/hedgewars/uGearsUtils.pas	Sat Oct 13 23:08:15 2018 +0300
+++ b/hedgewars/uGearsUtils.pas	Sat Oct 13 23:49:23 2018 +0300
@@ -1057,14 +1057,15 @@
 function CheckGearNearImpl(Kind: TGearType; X, Y: hwFloat; rX, rY: LongInt, exclude: PGear): PGear;
 begin
     var t: PGear;
-    width, halfWidth, dX, dY: hwFloat;
+    width, dX, dY: hwFloat;
+    bound: LongInt;
     isHit: Boolean;
 begin
     t:= GearsList;
+    bound:= _1_5 * int2hwFloat(max(rX, rY));
     rX:= sqr(rX);
     rY:= sqr(rY);
     width:= int2hwFloat(RightX - LeftX);
-    halfWidth:= width / _2;
 
     while t <> nil do
     begin
@@ -1072,13 +1073,16 @@
         begin
             dX := X - t^.X;
             dY := Y - t^.Y;
-            isHit := not ((hwSqr(dX) / rX + hwSqr(dY) / rY) > _1);
+            isHit := (hwAbs(dX) + hwAbs(dY) < bound)
+                and (not ((hwSqr(dX) / rX + hwSqr(dY) / rY) > _1));
 
             if (not isHit) and (WorldEdge = weWrap) then
             begin
-                if (dX > halfWidth) and (not ((hwSqr(dX - width) / rX + hwSqr(dY) / rY) > _1)) then
+                if (hwAbs(dX - width) + hwAbs(dY) < bound)
+                    and (not ((hwSqr(dX - width) / rX + hwSqr(dY) / rY) > _1)) then
                     isHit := true
-                else if (dX < -halfWidth) and (not ((hwSqr(dX + width) / rX + hwSqr(dY) / rY) > _1)) then
+                else if (hwAbs(dX + width) + hwAbs(dY) < bound)
+                    and (not ((hwSqr(dX + width) / rX + hwSqr(dY) / rY) > _1)) then
                     isHit := true
             end;