GitHubGet started
2D Systems

spatial

game/spatial finds the 2D objects near an area or inside the view without testing every one: broad-phase spatial indices (Hash, Quadtree) and viewport sprite culling (CullBatch, BatchCuller, Cull), allocation-free once warm. The object pools are in mempool.

import "github.com/nsgo-dev/game/pkg/spatial"

2D spatial indices (Hash, Quadtree)

Replace O(n²) scans with O(k) area queries, point queries, and collision pair generation. Both indices reuse internal buckets/nodes and accept caller-owned output slices so steady-state queries allocate zero bytes:

grid := spatial.NewHash[uint32](64) // cell size ~1-2x typical entity size
grid.Insert(id, entityBounds)
grid.Update(id, movedBounds)

// Zero-alloc area query reusing caller slice:
hits = grid.Query(cameraView, hits[:0])

// Deduplicated overlapping broad-phase pairs:
grid.QueryPairs(func(a, b uint32) {
	resolveNarrowPhase(a, b)
})

Use Hash[ID] for uniform entity sizes and unbounded or scrolling worlds; use NewQuadtree[ID](worldBounds, maxItems, maxDepth) when entities vary widely in size or cluster densely inside a known world rectangle. Both expose Insert, Update, Remove, Clear, Len, Query, and QueryPoint.

Viewport culling (CullBatch, BatchCuller, Cull)

Skip drawing off-screen sprites before submitting draw commands to core.Drawing:

var culler spatial.BatchCuller

// Inside Canvas.Draw:
culler.Draw(spriteBatch, cameraViewBounds, d)
  • BatchCuller.Draw(batch, view, d) reuses an internal scratch sprite.Batch for zero per-frame allocations (use the one-liner spatial.CullBatch(batch, view, d) when convenience matters more than reusing scratch memory).
  • Use spatial.CullSprites(view, src, dst[:0]), spatial.SpriteBounds(s), spatial.Intersects(a, b), or generic spatial.Cull(view, items, boundsOf, dst[:0]) to cull arbitrary world objects.

Circle queries

Hash.QueryCircle and Quadtree.QueryCircle append entries whose bounds intersect a circle, in each index's deterministic traversal order. Reuse the output slice across frames:

hits = grid.QueryCircle(playerPos, 96, hits[:0])

The broad-phase first visits the circle's bounding cells or quadrants, then checks the closest point on each entry's bounds. Negative, non-finite and empty circles return the output unchanged.

Nearest entries

Hash and Quadtree measure distance from the query point to the closest point on each entry's bounds. Nearest returns the ID, distance in points, and whether it found a match. KNearest appends up to k IDs in distance order. The filter may be nil to accept every ID. maxDist is inclusive, 0 finds entries that touch the point, and positive infinity removes the distance limit.

target, distance, ok := grid.Nearest(playerPos, 96, isEnemy)
allies = grid.KNearest(playerPos, 4, 64, isAlly, allies[:0])

Equal distances favor earlier insertions. Updating an entry keeps its place in that order. Removing and reinserting it gives it a new place. Reuse the output buffer for zero-allocation queries after warm-up. A filter must leave the queried index alone during the call.

Edit this page