collide

package
v1.17.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: MIT Imports: 3 Imported by: 2 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 contract of a Space's collision engine: what it is told (Config), whom it reports to (Handler), and what a caller drives (Engine). Space.CollideEngine of package aabbworld builds one over a Space's CanCollide boxes.

Handler

One Engine.Tick talks to the Handler in a fixed order. Touch is asked once for every pair of boxes that overlap, with the penetration between them; the handler may confirm the pair as it is, refine the penetration (a round shape inside a square box touches later than the box does) or veto the pair for this tick. Contact is told each confirmed pair once, with the penetration it is being separated by. Moved is told every box the engine pushed, once, where it came to rest — already written where the Space reads it, so only a caller whose items are a copy writes it back to whatever owns it. A box pushed out through an open edge is not Moved; it is listed by Engine.Left until the next Tick, as a Leaver with the box it came to rest at, wholly past the edge, written where the Space reads it the same way.

Config

Reach is how far a box may travel in one tick, as a fraction of its shorter side; boxes whose reaches do not touch are never paired, so it bounds the work of a tick. Iterations caps the passes one Tick spends on chained overlaps: separating one pair may push a box into a third, which the next pass resolves.

Field

Config.Field is a SolidField, the solid ground of a world on a grid, which need not be boxes in the Space. In every pass, after the pairs, the engine asks it for the solid boxes around each movable box (CanCollide, not Static) that moved since it last asked, and pushes the box out of each it overlaps through the shallowest side marked open in FieldBox.Open — the shallowest side at all when none is — so a box sliding along a wall of many cells never catches on the seams between them. A field that is also a WallField hands the engine its walls at any angle besides, after the boxes: a FieldWall is a segment Half thick either side, and a box is pushed out of it square to it — pieces joined end to end slide a box along them as one wall — or round a free end (FieldWall.Open) where that is shorter. A Sensor is told but not pushed. A pushed box is Moved like any other. A Handler that is also a FieldHandler is asked TouchField and told ContactField once per entity and cell a tick, boxes and walls of one cell alike; any other Handler hears nothing of the ground.

Engine

An Engine serves one goroutine. Tick reads the boxes of its Space as they are now — the Space need only have been Rebuilt since the items changed — and leaves them pushed apart; the Space answers its next Query with the pushed boxes on its own.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config added in v1.5.0

type Config struct {
	// Reach is how far a box may travel in a tick, as a fraction of its shorter side.
	Reach float32
	// Iterations caps the passes one Tick spends on chained overlaps.
	Iterations int
	// Field is the solid ground the Engine pushes movable boxes out of in the same passes as the
	// pairs; nil is none.
	Field SolidField
}

Config is how far an Engine looks for pairs and how long it spends separating them.

type Ends added in v1.12.0

type Ends uint8

Ends is a set of the ends of a wall.

const (
	EndA Ends = 1 << iota
	EndB
)

type Engine added in v1.5.0

type Engine interface {
	// Tick separates every overlapping pair the Space holds now and reports as it goes; the
	// Space answers its next Query with the pushed boxes without another Rebuild.
	Tick()
	// Left is who the last Tick pushed out through an open edge, and where to; good until the
	// next Tick.
	Left() []Leaver
}

Engine finds and separates the overlapping CanCollide boxes of its Space, on one goroutine.

type FieldBox added in v1.9.0

type FieldBox struct {
	Box  geom.AABB
	Cell uint64
	Open Sides
}

FieldBox is one solid box of a SolidField: where it is, which cell of the ground it is, for the handler, and which of its sides face open ground — the only ways a box inside it is pushed out, so a box sliding along a wall of many cells never catches on the seams between them.

type FieldHandler added in v1.9.0

type FieldHandler interface {
	TouchField(id uid.UID64, cell uint64, pen geom.Vec) (geom.Vec, bool)
	ContactField(id uid.UID64, cell uint64, pen geom.Vec)
}

FieldHandler is a Handler also told about the solid ground: once per entity and cell a tick, TouchField asks whether they really touch, and by how much (false: not this tick), and ContactField tells the contact, with the push that separates the entity. A Handler that is not a FieldHandler is told nothing of the ground; the Engine pushes all the same.

type FieldWall added in v1.12.0

type FieldWall struct {
	A, B geom.Vec
	Half float32
	Cell uint64
	Open Ends
}

FieldWall is one wall of a WallField: Half thick either side of the segment from A to B, the cell of the ground it is, for the handler, and which of its ends stand free. A box is pushed out of it square to it — so pieces of wall joined end to end slide a box along them as one — and round a free end where that is the shorter way.

type Handler added in v1.5.0

type Handler interface {
	// Touch says whether the shapes in two overlapping boxes really touch, and by how much.
	Touch(a, b uid.UID64, pen geom.Vec) (geom.Vec, bool)
	// Contact is a pair Touch confirmed, with the penetration it is being separated by.
	Contact(a, b uid.UID64, pen geom.Vec)
	// Moved is a box the Engine pushed, where it came to rest.
	Moved(id uid.UID64, box plane.AABB)
}

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

type Leaver added in v1.10.0

type Leaver struct {
	ID  uid.UID64
	Box plane.AABB
}

Leaver is a box an Engine pushed out through an open edge, where it came to rest — wholly past the edge. It is not Moved: whoever owns the box writes it back from here.

type Sides added in v1.9.0

type Sides uint8

Sides is a set of the sides of a box.

const (
	Left Sides = 1 << iota
	Right
	Top
	Bottom
)

type SolidField added in v1.9.0

type SolidField interface {
	Solid(id uid.UID64, box geom.AABB, visit func(FieldBox) bool)
}

SolidField is the solid ground of a world laid out on a grid — walls, rock, whatever no one passes — as an Engine asks it, instead of being boxes in the Space. Solid calls visit with every solid box of the ground that box of entity id may overlap, for that entity (a wall may let a flyer over); visit returning false ends the walk. Boxes lie in the frame of the box asked about: across a wrapping seam, shifted to its side.

type WallField added in v1.12.0

type WallField interface {
	SolidField
	Walls(id uid.UID64, box geom.AABB, visit func(FieldWall) bool)
}

WallField is a SolidField with walls at any angle besides its boxes: Walls calls visit with every wall that box of entity id may overlap, for that entity, in the frame of the box asked about; visit returning false ends the walk. An Engine asks a SolidField that is one for its walls after its boxes.

Jump to

Keyboard shortcuts

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