collision

package
v0.22.1 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: MIT Imports: 29 Imported by: 1 module, 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 collision detects overlaps between world entities each tick and records what each struck on its Collider. An entity takes part while it carries Collider; one also carrying Physics is pushed apart and bounces. Reactions are rules (act.Branch) of a Meeting, a pair, or of a Struck, obeyed by the roles its entities play (world.Roles).

Plugin and its system

Plugin is built over a world plugin and motion (NewPlugin) and installed with Use; it runs one collision system a tick over the world's space. A Collider counts from the tick it is carried; one that loses its Collider meets nobody from the next. Whoever is pushed out through an open edge is motion's leaver (motion.Write.Leave). Only what is within two levels of the finest level any observer sees collides, every step (lod.Contact): farther off a box meets none, keeping no contacts. Nor does one travelling the abstract graph (sector.Abstract): it pushes, is pushed by and rests against nothing.

Solid ground

Given a Field (the board's Solid cells, board.Plugin.WithCollision or Plugin.WithField) the system also pushes every movable collider out of the solid ground on its world.Layers and, with heights, in its Band. A contact with the ground bounces off it as off an infinite mass and is recorded as a Contact with Terrain set and the Cell; a sensor is told and never pushed. A pair parted that still touches stays met, a Contact at rest (Resting) on both sides, leaving by the face they touch at, with no impulse and no strike, until the two come apart. A unit a hand drives (steering.Driven, the keys held or not) is never turned by a wall, only slowed. What an impulse takes from a steered entity's speed stays taken: its steering.Course goes on from what is left.

A push apart never puts a collider further over ground that does not take it (water to a walker, a hole; Field.Overhang): that side holds where it is and bounces as off the ground, the other goes the whole way. Ground turning to water under an entity is no push: it stays, fallen in.

Heights

In a world with heights (world.Config.Heights) a pair meets only where the [Band]s the two span (BandOf a Z) share a stretch, and the ground stops an entity only in a solid cell whose own band meets the entity's. Whatever says no height spans Everywhere and meets everything, as Layers 0 meets every plane; two bands meeting only at an edge do not meet. A band carries the entity's step: a deck's slab strikes only what of the band lies over it (Band.Above), so a walker steps onto a deck within its reach and passes under one it fits under. A field may hold walls besides its boxes (Field.Walls): railings along a bridge, a band's slab. In a flat world every band is Everywhere.

Swept entities

An entity that moves itself further in a step than the world's cap allows (a shot) carries a Sweep: where its centre was as the step began, its box where it ended. For the tick the space holds the whole stretch, every pair and solid box on it is refined to the segment of the step, and the nearest contact alone is kept: one hit a step, its Contacts saying where along the step it lies (Along) and that it was only detected (Sensed). A swept entity is a sensor whatever its Physics, two swept pass through each other, and a sweep passes through the one it Ignores. A wrapping world refuses a Sweep.

Collider and Physics

Collider is all it takes to take part; it also holds what the entity struck the tick before (Collider.Contacts, at most MaxContacts). Two colliders touch only where their world.Layers meet. Physics makes an entity pushed out of overlaps and bouncing, by Mass (non-positive weighs the box's area, +Inf is a wall) and Restitution; one without Physics is only ever detected. A push apart is shared by weight, the lighter side moved the further, a wall and a side held over ground that does not take it not at all. Collision brings a Collider and a Physics to every unit through the world's roster; a game drops the Physics of a unit nothing pushes with comp.Without.

Meeting and Struck

A rule of a Meeting (When: cond.Other.If(role)) is handed one per confirmed contact between two entities, seen from Self; a rule of a Struck one per entity that struck something the tick before. Plugin.WithStats counts the contacts into a ContactStats, Plugin.WithLog writes a line for each.

Vocabulary

Moments, for a role's rules: Meeting (a pair, host.Met; its word Facing), Struck (one that struck something the tick before). Knobs, for an effect's Alter: Physics. Commands: Boxes (a player's, a look: every entity's box in red, its base under it, its solid over the figures in a world with heights, or none; BoxesShown; the settings screen alone). Settings (Settings, saved with the game, in Resources): the boxes shown.

Index

Constants

View Source
const MaxContacts = 8

MaxContacts caps how many contacts one entity records in a tick; extras are still separated, bounced and reported to rules.

Variables

View Source
var Everywhere = Band{Bottom: f32.Inf(-1), Top: f32.Inf(1)}

Everywhere is the band of every height.

Functions

func Facing added in v0.3.0

func Facing(within float32) cond.Word[Meeting]

Facing is the word of a meeting in which Self is turned towards Other, within the angle in radians of the way to it.

Types

type Band added in v0.3.0

type Band struct{ Bottom, Top, Step float32 }

Band is a stretch of height, Bottom to Top, an entity's or a solid cell's; Step is how high an entity steps up. Whatever does not say how high it is spans Everywhere.

func BandOf added in v0.3.0

func BandOf(z *world.Z) Band

BandOf is the band an entity's Z spans, its bottom to its top; Everywhere for no Z or no Height.

func (Band) Above added in v0.3.0

func (b Band) Above() Band

Above is the band over b's step: what of it a deck's slab strikes.

func (Band) Meets added in v0.3.0

func (b Band) Meets(o Band) bool

Meets reports whether the two bands share a stretch of height; two meeting only at an edge do not (a crate on a platform).

type Boxes added in v0.3.0

type Boxes struct{ Shown BoxesShown }

Boxes has Shown drawn of every entity's box (Settings.Boxes): a look for finding out why things touch or miss. The settings screen alone.

func (Boxes) Command added in v0.3.0

func (Boxes) Command()

type BoxesShown added in v0.3.0

type BoxesShown uint8

BoxesShown is what is drawn of the entities' boxes.

const (
	// NoBoxes draws none.
	NoBoxes BoxesShown = iota
	// Bases outlines every entity's box where it stands, under its figure.
	Bases
	// Solids draws the edges of every entity's box from its altitude to its top, over every
	// figure and under the ground, in a world with heights.
	Solids
)

type Collider

type Collider struct {
	Struck      [MaxContacts]Contact
	StruckCount uint8
}

Collider makes an entity take part in collision detection and holds what it struck the tick before. Two colliders touch only where their world.Layers meet.

func (*Collider) Contacts

func (c *Collider) Contacts() []Contact

Contacts is what this entity struck the tick before, in the order confirmed.

type Contact

type Contact struct {
	Other   uid.UID64
	Impact  float32
	Normal  geom.Vec
	Terrain bool
	Cell    uint64
	Along   float32
	Sensed  bool
	Resting bool
}

Contact is one confirmed contact: who it was against (the solid ground with Terrain set and its Cell), the impulse exchanged and the way this entity leaves the other. Along is where along a swept entity's step it lies, 0 to 1 (1 with none swept); Sensed that it was only detected; Resting that the two, parted before, still touch: no push, no strike.

type ContactStats added in v0.3.0

type ContactStats struct {
	Counter int
}

ContactStats is the running total of contacts collision counts, given with WithStats. It only grows; Reporter works a rate out of it.

func (*ContactStats) Reporter added in v0.3.0

func (s *ContactStats) Reporter(tps *int) render.Reporter

Reporter is the stats' lines for the telemetry panel: contacts a second and, at the tick rate tps points to, contacts a tick.

type Field added in v0.3.0

type Field interface {
	Solid(layers world.Layers, band Band, box geom.AABB, visit func(FieldBox) bool)
	Walls(layers world.Layers, band Band, box geom.AABB, visit func(collide.FieldWall) bool)
	Overhang(layers world.Layers, box geom.AABB) float32
}

Field is the solid ground of a world, as the board gives it (Plugin.WithField). Solid and Walls visit every solid box and wall an entity on layers spanning band may overlap within box (false from visit ends the walk); Overhang is how much of box, as area, lies over ground that does not take the entity.

type FieldBox added in v0.3.0

type FieldBox = collide.FieldBox

FieldBox is one solid box of a Field.

type Meeting

type Meeting struct {
	Self, Other uid.UID64
	Impact      float32
	Normal      geom.Vec
	Heading     geom.Vec
	Toward      geom.Vec
}

Meeting is one confirmed contact as seen from Self: who it met, the impulse exchanged (zero when only detected), the way Self left Other, the way Self faces and the way from its middle to Other's.

func (Meeting) Who added in v0.3.0

func (m Meeting) Who() uid.UID64

Who is the entity that met the other.

func (Meeting) Whom added in v0.3.0

func (m Meeting) Whom(each func(uid.UID64))

Whom tells each the other one.

type Physics

type Physics struct {
	// Mass is how hard the entity is to shift; non-positive weighs its box's area in square world
	// units, +Inf is a wall.
	Mass float32
	// Restitution is the share of approach speed given back, 0 to 1; a pair uses the lower one.
	Restitution float32
}

Physics makes an entity take part in the physical side of a contact: pushed apart, bouncing. The zero value weighs as much as its box is wide by deep and does not bounce.

func (Physics) Knob added in v0.3.0

func (Physics) Knob()

Knob: an effect may turn it (effect.Knob).

type Plugin

type Plugin struct {
	*world.Self

	Res Resources
	// contains filtered or unexported fields
}

Plugin wires collision into a Stage over the world's space.

func NewPlugin

func NewPlugin(worldPlugin *world.Plugin, m *motion.Plugin) *Plugin

NewPlugin builds the collision plugin over worldPlugin's space, its leavers motion's.

func (*Plugin) DefaultBindings added in v0.3.0

func (p *Plugin) DefaultBindings() []control.Binding

DefaultBindings are none: the boxes are the settings screen's.

func (*Plugin) EventHandler

func (p *Plugin) EventHandler() control.EventHandler

EventHandler is nil: collision reads no input.

func (*Plugin) Install

func (p *Plugin) Install(ctx plugin.Installer) error

func (*Plugin) KindTemplates added in v0.3.0

func (p *Plugin) KindTemplates(r *kind.Roster)

KindTemplates is what collision asks of the kinds a game defines: every unit a collider of the physics' defaults.

func (*Plugin) Name

func (p *Plugin) Name() string

func (*Plugin) Queues added in v0.3.0

func (p *Plugin) Queues() []command.CommandQueue

Queues are where Boxes lands.

func (*Plugin) Read added in v0.4.0

func (p *Plugin) Read(int) *Read

Read reads the boxes' drawing on worker w of the game's pool, 0 the game's own goroutine.

func (*Plugin) Restore added in v0.3.0

func (p *Plugin) Restore()

Restore takes the Settings loaded.

func (*Plugin) RunPlan

func (p *Plugin) RunPlan(ctx goke.RunCtx, d time.Duration)

RunPlan runs the collisions in the simulation; call it after motion's RunPlan.

func (*Plugin) Serializable

func (p *Plugin) Serializable() plugin.Serializable

Serializable is collision's Settings, kept whole; its components go with the entities.

func (*Plugin) Settings added in v0.3.0

func (p *Plugin) Settings() []control.Setting

Settings is the one setting, what is drawn of the entities' boxes (plugin.Tunable).

func (*Plugin) WithField added in v0.3.0

func (p *Plugin) WithField(f Field) *Plugin

WithField makes f the solid ground colliders are pushed out of; call before Use.

func (*Plugin) WithLog added in v0.3.0

func (p *Plugin) WithLog(l *log.Logger) *Plugin

WithLog has collision write a line to l for every contact between entities; call before Use.

func (*Plugin) WithStats added in v0.3.0

func (p *Plugin) WithStats(s *ContactStats) *Plugin

WithStats has collision count every contact into s; call before Use.

func (*Plugin) Write added in v0.4.0

func (p *Plugin) Write() *Write

Write changes the boxes' drawing, on the game's goroutine.

type Read added in v0.4.0

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

Read reads the boxes' drawing, the same on every goroutine.

func (*Read) BoxesShown added in v0.4.0

func (r *Read) BoxesShown() BoxesShown

BoxesShown is what is drawn of the entities' boxes.

type Resources added in v0.3.0

type Resources struct {
	Settings Settings
}

Resources is what collision publishes: its Settings.

type Settings added in v0.3.0

type Settings struct {
	Boxes BoxesShown
}

Settings is what collision lets the players adjust, saved with the game.

type Struck

type Struck struct {
	ID       uid.UID64
	Contacts []Contact
}

Struck is an entity that struck something the tick before, and what it struck. An entity that struck nothing is not told.

func (Struck) Who added in v0.3.0

func (s Struck) Who() uid.UID64

Who is the entity that struck.

type Sweep added in v0.3.0

type Sweep struct {
	From     geom.Vec
	Ignore   uid.UID64
	Ignoring bool
}

Sweep marks an entity that moves itself further in a step than the world's cap: From is where its centre was as the step began, its box where it ended, and the nearest contact along the step alone is kept. Ignore, when Ignoring, is the one entity the sweep passes through.

type Write added in v0.4.0

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

Write changes the boxes' drawing, on the game's goroutine.

func (*Write) ShowBoxes added in v0.4.0

func (w *Write) ShowBoxes(shown BoxesShown)

ShowBoxes has the entities' boxes drawn as shown says (Settings.Boxes), as the game starts.

Directories

Path Synopsis
internal
collisiontest
Package collisiontest is what the collision's tests share: an installer and a world with motion and collision, stepped as a game steps it.
Package collisiontest is what the collision's tests share: an installer and a world with motion and collision, stepped as a game steps it.
response
Package response is how the collision answers a contact, on numbers alone: the impulse two sides trade along the contact's normal (Exchange) and the footing that keeps a side pushed apart off ground that does not take it (Footing, Worse).
Package response is how the collision answers a contact, on numbers alone: the impulse two sides trade along the contact's normal (Exchange) and the footing that keeps a side pushed apart off ground that does not take it (Footing, Worse).

Jump to

Keyboard shortcuts

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