Documentation
¶
Overview ¶
Package index is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.
Package index provides a headless, float64 spatial index. It has no dependency on NSGo's UI runtime, so simulations and servers can use it directly.
Index ¶
- func Intersects(a, b Bounds) bool
- type Bounds
- type Hash
- func (sh *Hash[ID]) CellSize() float64
- func (sh *Hash[ID]) Clear()
- func (sh *Hash[ID]) Insert(id ID, bounds Bounds)
- func (sh *Hash[ID]) KNearest(p Point, k int, maxDist float64, accept func(ID) bool, out []ID) []ID
- func (sh *Hash[ID]) Len() int
- func (sh *Hash[ID]) Nearest(p Point, maxDist float64, accept func(ID) bool) (ID, float64, bool)
- func (sh *Hash[ID]) Query(area Bounds, out []ID) []ID
- func (sh *Hash[ID]) QueryCircle(center Point, radius float64, out []ID) []ID
- func (sh *Hash[ID]) QueryPairs(fn func(a, b ID))
- func (sh *Hash[ID]) QueryPoint(p Point, out []ID) []ID
- func (sh *Hash[ID]) Remove(id ID)
- func (sh *Hash[ID]) Update(id ID, bounds Bounds)
- type Point
- type Quadtree
- func (qt *Quadtree[ID]) Clear()
- func (qt *Quadtree[ID]) Insert(id ID, bounds Bounds)
- func (qt *Quadtree[ID]) KNearest(p Point, k int, maxDist float64, accept func(ID) bool, out []ID) []ID
- func (qt *Quadtree[ID]) Len() int
- func (qt *Quadtree[ID]) Nearest(p Point, maxDist float64, accept func(ID) bool) (ID, float64, bool)
- func (qt *Quadtree[ID]) Query(area Bounds, out []ID) []ID
- func (qt *Quadtree[ID]) QueryCircle(center Point, radius float64, out []ID) []ID
- func (qt *Quadtree[ID]) QueryPoint(p Point, out []ID) []ID
- func (qt *Quadtree[ID]) Remove(id ID)
- func (qt *Quadtree[ID]) Update(id ID, bounds Bounds)
- type Size
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Intersects ¶
Intersects reports whether rectangles overlap with positive area.
Types ¶
type Hash ¶
type Hash[ID comparable] struct { // contains filtered or unexported fields }
Hash is a uniform 2D grid broad-phase index for fast area queries, point queries, and collision pair generation.
Clear retains all bucket slices and map tables so per-frame rebuilds (Clear + Insert + Query) run with 0 allocations in steady state. Query and QueryPairs iterate cells in deterministic row-major order without iterating over Go maps, deduplicating multi-cell entries via generation counters.
Example ¶
package main
import (
"fmt"
"github.com/nsgo-dev/game/pkg/spatial/index"
)
func main() {
grid := index.NewHash[string](16)
grid.Insert("player", index.Bounds{Origin: index.Point{X: 1, Y: 2}, Size: index.Size{Width: 4, Height: 4}})
hits := grid.Query(index.Bounds{Origin: index.Point{}, Size: index.Size{Width: 8, Height: 8}}, nil)
fmt.Println(hits)
}
Output: [player]
func NewHash ¶
func NewHash[ID comparable](cellSize float64) *Hash[ID]
NewHash returns a Hash with uniform square cells of cellSize units. If cellSize <= 0 or non-finite, it defaults to 64.
func (*Hash[ID]) Clear ¶
func (sh *Hash[ID]) Clear()
Clear removes all items from the spatial hash while keeping all bucket slice capacities and internal map tables for zero-allocation reuse on subsequent frames.
func (*Hash[ID]) Insert ¶
Insert adds id with bounds to the spatial hash. If id is already present, its bounds are updated in place.
func (*Hash[ID]) KNearest ¶
KNearest appends up to k accepted IDs ordered by distance to p, with ties in insertion order. The distance and filter rules are those of Nearest. Reuse out and call with the same k to avoid allocations after warming up.
func (*Hash[ID]) Nearest ¶
Nearest returns the closest accepted ID and its distance to p, measured to the entry's bounds. Equal distances favor earlier insertions. A nil accept accepts every ID. maxDist is inclusive and may be +Inf. The second return value is zero when no entry matches. accept must not access this index.
func (*Hash[ID]) Query ¶
Query appends all IDs whose bounds intersect area to out and returns the extended slice. Multi-cell entries are reported once in deterministic order. Pass out[:0] to reuse a buffer across frames with 0 allocations.
func (*Hash[ID]) QueryCircle ¶
QueryCircle appends all IDs whose bounds intersect the circle centered at center with radius. Results follow a deterministic order.
func (*Hash[ID]) QueryPairs ¶
func (sh *Hash[ID]) QueryPairs(fn func(a, b ID))
QueryPairs invokes fn once for every unordered pair (a, b) of distinct intersecting items in the spatial hash, without duplicates and with zero allocations. Callers must not mutate the spatial hash during fn.
func (*Hash[ID]) QueryPoint ¶
QueryPoint appends all IDs whose bounds contain p to out and returns the extended slice.
type Point ¶
type Point struct{ X, Y float64 }
Point is a two-dimensional position in simulation coordinates.
type Quadtree ¶
type Quadtree[ID comparable] struct { // contains filtered or unexported fields }
Quadtree is an adaptive 2D spatial index that recursively partitions a world rectangle into four quadrants.
Each item is placed in the deepest quadrant that completely contains its bounds, so straddling and out-of-world items remain in their parent (or root) without duplication. Internal nodes are stored in a flat pooled slice so Clear + Insert + Query runs with 0 allocations in steady state.
func NewQuadtree ¶
func NewQuadtree[ID comparable](world Bounds, maxItems, maxDepth int) *Quadtree[ID]
NewQuadtree returns a Quadtree covering world. When a leaf exceeds maxItems and its depth is below maxDepth, it splits into four equal quadrants. Non-positive maxItems or maxDepth default to 8.
func (*Quadtree[ID]) Clear ¶
func (qt *Quadtree[ID]) Clear()
Clear removes all items and collapses all subdivisions while keeping pooled node and slice capacities for zero-allocation reuse.
func (*Quadtree[ID]) Insert ¶
Insert adds id with bounds to the quadtree. If id is already present, its bounds are replaced.
func (*Quadtree[ID]) KNearest ¶
func (qt *Quadtree[ID]) KNearest(p Point, k int, maxDist float64, accept func(ID) bool, out []ID) []ID
KNearest appends up to k accepted IDs ordered by distance to p, with ties in insertion order. The distance and filter rules are those of Nearest. Reuse out and call with the same k to avoid allocations after warming up.
func (*Quadtree[ID]) Nearest ¶
Nearest returns the closest accepted ID and its distance to p. Its bounds, tie-breaking, and filter rules are the same as Hash.Nearest.
func (*Quadtree[ID]) Query ¶
Query appends all IDs whose bounds intersect area to out and returns the extended slice. Pass out[:0] to reuse a buffer with 0 allocations.
func (*Quadtree[ID]) QueryCircle ¶
QueryCircle appends all IDs whose bounds intersect the circle centered at center with radius. Results follow the quadtree's deterministic traversal.
func (*Quadtree[ID]) QueryPoint ¶
QueryPoint appends all IDs whose bounds contain p to out and returns the extended slice.