From 074b57f76d3ab949fbdd4ce9af527a8a5367819c Mon Sep 17 00:00:00 2001 From: Josh Deprez Date: Fri, 6 Aug 2021 11:39:19 +1000 Subject: [PATCH] more efficient tile collision testing? --- engine/tiles.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/engine/tiles.go b/engine/tiles.go index e5c2861..9d19e19 100644 --- a/engine/tiles.go +++ b/engine/tiles.go @@ -31,10 +31,17 @@ func (t *Tilemap) CollidesWith(r image.Rectangle) bool { if t.Ersatz { return false } - // TODO: optimise? - for j, row := range t.Map { - for i, tile := range row { - if tile == nil { + + // If we round down r.Min, and round up r.Max, to the nearest tile + // coordinates, that gives the full range of tiles to test. + sm1 := t.TileSize - 1 + min := r.Min.Div(t.TileSize) + max := r.Max.Add(image.Pt(sm1, sm1)).Div(t.TileSize) + + for j := min.Y; j <= max.Y && j < len(t.Map); j++ { + row := t.Map[j] + for i := min.X; i <= max.X && i < len(row); i++ { + if row[i] == nil { continue } if r.Overlaps(image.Rect(i*t.TileSize, j*t.TileSize, (i+1)*t.TileSize, (j+1)*t.TileSize)) {