From 724ebbda9943f25ca44a547f5b822ed6abe29547 Mon Sep 17 00:00:00 2001 From: RandomityGuy <31925790+RandomityGuy@users.noreply.github.com> Date: Sat, 28 Mar 2026 01:51:24 +0000 Subject: [PATCH] fix this broadphase crash --- src/Util.hx | 8 ++++++++ src/collision/GridBroadphase.hx | 8 ++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Util.hx b/src/Util.hx index 90c732db..1491d25a 100644 --- a/src/Util.hx +++ b/src/Util.hx @@ -30,6 +30,14 @@ class Util { return value; } + public static inline function imin(a:Int, b:Int) { + return a < b ? a : b; + } + + public static inline function imax(a:Int, b:Int) { + return a > b ? a : b; + } + public static inline function lerp(a:Float, b:Float, t:Float) { return a + (b - a) * t; } diff --git a/src/collision/GridBroadphase.hx b/src/collision/GridBroadphase.hx index 2921fe4e..1c776ef5 100644 --- a/src/collision/GridBroadphase.hx +++ b/src/collision/GridBroadphase.hx @@ -111,10 +111,10 @@ class GridBroadphase { var queryMinY = Math.max(object.boundingBox.yMin, bounds.yMin); var queryMaxX = Math.min(object.boundingBox.xMax, bounds.xMax); var queryMaxY = Math.min(object.boundingBox.yMax, bounds.yMax); - var xStart = Math.floor((queryMinX - bounds.xMin) / this.cellSize.x); - var yStart = Math.floor((queryMinY - bounds.yMin) / this.cellSize.y); - var xEnd = Math.floor((queryMaxX - bounds.xMin) / this.cellSize.x); - var yEnd = Math.floor((queryMaxY - bounds.yMin) / this.cellSize.y); + var xStart = Util.imax(0, Math.floor((queryMinX - bounds.xMin) / this.cellSize.x)); + var yStart = Util.imax(0, Math.floor((queryMinY - bounds.yMin) / this.cellSize.y)); + var xEnd = Util.imin(CELL_SIZE - 1, Math.floor((queryMaxX - bounds.xMin) / this.cellSize.x)); + var yEnd = Util.imin(CELL_SIZE - 1, Math.floor((queryMaxY - bounds.yMin) / this.cellSize.y)); var proxy = objectToProxy.get(object); if (proxy == null) { insert(object);