more efficient tile collision testing?

This commit is contained in:
Josh Deprez 2021-08-06 11:39:19 +10:00 committed by Josh Deprez
parent cb157a9503
commit 074b57f76d

View file

@ -31,10 +31,17 @@ func (t *Tilemap) CollidesWith(r image.Rectangle) bool {
if t.Ersatz { if t.Ersatz {
return false return false
} }
// TODO: optimise?
for j, row := range t.Map { // If we round down r.Min, and round up r.Max, to the nearest tile
for i, tile := range row { // coordinates, that gives the full range of tiles to test.
if tile == nil { 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 continue
} }
if r.Overlaps(image.Rect(i*t.TileSize, j*t.TileSize, (i+1)*t.TileSize, (j+1)*t.TileSize)) { if r.Overlaps(image.Rect(i*t.TileSize, j*t.TileSize, (i+1)*t.TileSize, (j+1)*t.TileSize)) {