collide

package
v1.21.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package collide is the engine behind the public collide contract: it finds the overlapping items of one grid, separates them and reports as it goes.

Engine

An Engine is built by New over a Grid and its Surface, reporting to a Handler. Each Engine.Tick takes the grid's items as they are, asks the grid for every pair within reach, hands the pairs to a Solver, and afterwards reports each pushed item once — as Moved, or as Left, with the box it came to rest at, when the push took it wholly out through an open edge. It then tells the grid its cells are behind, so the next Query sees the pushed boxes. A still item a pair meets joins the batch past the moving ones, Static, the first time.

Solver

The Solver holds a batch of Pair values — two positions among the items and flags saying which side is StaticA or StaticB and whether the pair is a Sensor pair that is only ever reported. Solver.Solve sweeps the batch for up to a number of passes: each overlapping pair is put to the Touch callback once, and a confirmed pair is pushed apart along its penetration, the whole way by the non-static side or shared as Touch said, half each without one. A pair is measured again only when one of its boxes has moved since, so a settled batch costs one pass. Solver.VisitMoved then names every item a pass shifted.

Handler

Handler mirrors the public collide.Handler method for method. It is declared again here because an internal package does not import the public one; the public Space passes its handler straight through.

Index

Constants

View Source
const (
	StaticA uint8 = 1 << iota
	StaticB
	Sensor
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Engine added in v1.5.0

type Engine struct {
	// contains filtered or unexported fields
}

Engine finds and separates the overlapping items of one grid; one Engine serves one goroutine.

func New added in v1.5.0

func New(grid *spatial.Grid, surface *iplane.Surface, handler Handler, reach float32, iterations int, field pcollide.SolidField) *Engine

New builds an Engine over the CanCollide items of grid, reporting to handler; field, when not nil, is solid ground the movable items are pushed out of.

func (*Engine) Left added in v1.5.0

func (e *Engine) Left() []pcollide.Leaver

Left is who the last Tick pushed out through an open edge, and where to; good until the next Tick.

func (*Engine) Tick added in v1.5.0

func (e *Engine) Tick()

Tick separates every overlapping pair the grid holds now and reports as it goes, pushing the boxes where they lie. The grid answers its next Query with them on its own; no Rebuild is owed.

type Field added in v1.9.0

type Field struct {
	Solid   pcollide.SolidField
	Walls   pcollide.WallField // the Solid's walls, when it has any
	Touch   func(item int32, cell uint64, pen geom.Vec) (geom.Vec, bool)
	Contact func(item int32, cell uint64, pen geom.Vec)
}

Field is how a solve meets the solid ground: the boxes round an item, and whom to tell of them.

type Handler added in v1.5.0

type Handler interface {
	Touch(a, b uid.UID64, pen geom.Vec) (geom.Vec, float32, bool)
	Contact(a, b uid.UID64, pen geom.Vec)
	Moved(id uid.UID64, box plane.AABB)
}

Handler is told what an Engine finds: each overlapping pair once, and every box it pushed.

type Pair

type Pair struct {
	A, B  int32
	Flags uint8
}

Pair is two items of a batch that may be in contact, with the flags the solver separates them by.

type Solver

type Solver struct {
	// contains filtered or unexported fields
}

Solver is the state and the algorithm behind collide.Engine, working on the items of a Grid.

func (*Solver) Add

func (s *Solver) Add(p Pair) int

Add enters a pair into the batch and returns its index.

func (*Solver) Fit added in v1.21.0

func (s *Solver) Fit(items int)

Fit widens the batch to items entries, for entries added since Reset.

func (*Solver) Pair

func (s *Solver) Pair(i int) *Pair

Pair is the i-th pair of the batch.

func (*Solver) Reset

func (s *Solver) Reset(items int)

Reset empties the Solver for a new batch of at most items entries, keeping the memory.

func (*Solver) Solve

func (s *Solver) Solve(
	items []spatial.Entry, surface *iplane.Surface, iterations int,
	touch Touch, onContact func(i int, pen geom.Vec), field *Field,
)

Solve reports each overlapping pair once and pushes it apart, and every movable item out of the field's solid ground when there is one, in up to iterations passes.

func (*Solver) VisitMoved

func (s *Solver) VisitMoved(fn func(item int32))

VisitMoved calls fn for every item the solver pushed, once per pair it was pushed in.

type Touch added in v1.5.0

type Touch func(i int, pen geom.Vec) (geom.Vec, float32, bool)

Touch is asked once per pair, when its boxes first overlap, for the push and the part of it A takes; false drops the pair for the tick.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL