collision

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 8 Imported by: ? 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 collision detects overlaps between entity colliders and records them in Contacts. Collider supports local-space Box and Circle shapes, transformed by entity position and rotation (scale is ignored). Create colliders with NewCollider, attach them to entities with a core.Transform, and register NewSystem in the fixed schedule. Read overlap lifecycle from Contacts or listen for Entered and Exited. The package detects collisions but does not resolve them. The collision system updates Queries for raycasts against the same synchronized collider state.

Index

Constants

View Source
const SystemName = "engine.collision"

SystemName is the name castrum.New registers the collision system under in the fixed schedule.

Variables

This section is empty.

Functions

func Layers

func Layers(layers ...int) (uint32, error)

Layers returns the bitmask for the given layer indexes for Collider.Layers. For example, Layers(0, 2) includes layers 0 and 2. With no arguments it returns a zero bitmask, so the collider belongs to no layer.

It returns an error for an index outside 0-31.

func Mask

func Mask(layers ...int) (uint32, error)

Mask returns the bitmask for the given layer indexes for Collider.Mask. For example, Mask(0) listens only to layer 0. With no arguments it returns a zero bitmask, so the collider listens to no layers.

It returns an error for an index outside 0-31.

func NewSystem

func NewSystem() core.SystemFunc

NewSystem returns the collision detector, which records overlaps in Contacts.

castrum.New registers it under SystemName when configured with `castrum.WithCollision`; otherwise, register it directly and provide Queries and a signal.Hub to the world. It runs before gameplay systems in the fixed phase, so it reads transforms from the preceding tick and detects movement made during the current tick on the next one.

The system attaches Contacts to entities with both a Collider and a core.Transform, applies position and rotation but not scale, and retains existing pairs long enough to report when a contact ends.

Types

type Box

type Box struct {
	// Min is the rectangle's minimum local-space corner.
	Min geom.Vector2
	// Max is the rectangle's maximum local-space corner.
	Max geom.Vector2
}

Box is an axis-aligned rectangle in collider-local space. Both corners must be finite, and Min must be strictly below Max on both axes. Collider.Validate rejects invalid bounds rather than normalizing them.

type Circle

type Circle struct {
	// Center is the circle's center in collider-local space.
	Center geom.Vector2
	// Radius is the circle's radius.
	Radius float64
}

Circle is a circle in collider-local space. Its center must be finite and its radius finite and positive; Collider.Validate rejects invalid values.

type Collider

type Collider struct {
	// Shape is the collider's geometry in local space, before the
	// entity's position and rotation are applied. Supported shapes are
	// [Box] and [Circle].
	Shape ColliderShape
	// Offset shifts the shape from the entity's local origin, for
	// hitboxes that are not centered on it.
	//
	// For example, place a foot collider below a sprite or a sword
	// collider in front of its owner.
	Offset geom.Vector2
	// Layers is the bitmask of layers this collider belongs to:
	// bit i is layer i. A collider may occupy any combination of the
	// 32 layers. The zero value belongs to no layer, so it collides
	// with nothing.
	//
	// [NewCollider] places it on layer 0, and [Layers] builds the
	// bitmask from layer numbers.
	Layers uint32
	// Mask is the bitmask of layers this collider interacts with:
	// bit i is layer i. The zero value is an empty mask, which
	// collides with nothing.
	//
	// [NewCollider] opens it to every layer, and [Mask] builds the
	// bitmask from layer numbers.
	Mask uint32
	// Trigger marks contacts involving this collider as triggers.
	Trigger bool
	// Active controls whether the collider participates in detection.
	Active bool
}

Collider defines an entity's collision shape and interaction filter. An entity must also have a Transform to participate in detection. Each fixed tick, the collision system applies the transform's position and rotation, and records detected overlaps in the entity's Contacts.

The zero value has no shape and is not valid for spawning. Use NewCollider or provide a valid shape, then configure its filter and activation as needed.

func NewCollider

func NewCollider(shape ColliderShape) (Collider, error)

NewCollider returns an active collider on layer 0 that listens to every layer. It validates shape before returning; invalid shapes produce an error. Set the fields to customize its filter, offset, trigger behavior, or activation. Layers and Mask convert layer indexes to bitmasks.

func (Collider) CanCollideWith

func (c Collider) CanCollideWith(other Collider) bool

CanCollideWith reports whether the layer/mask configuration allows the two colliders to interact: each must listen to at least one layer the other sits on. A one-sided mask does not collide.

func (Collider) Validate

func (c Collider) Validate() error

Validate returns an error if the shape is unsupported or has invalid geometry, or if the offset is non-finite. It rejects rather than normalizes invalid geometry. Any layer and mask bitmasks are valid, including zero.

type ColliderShape

type ColliderShape interface {
	// contains filtered or unexported methods
}

ColliderShape is local-space geometry accepted by a Collider. The package provides Box and Circle; the interface is sealed to these built-in shapes.

type Contact

type Contact struct {
	// Other is the entity this contact is with.
	Other core.Entity
	// Point is the pair's representative contact point.
	Point geom.Vector2
	// Normal points from this collider toward Other. Moving this collider
	// by -Normal * Penetration separates the pair to touching.
	Normal geom.Vector2
	// Penetration is the overlap depth along the normal. Touching
	// shapes have zero penetration and still produce a contact.
	Penetration float64
	// Trigger is true if either collider in the pair is a trigger.
	Trigger bool
}

Contact describes one detected overlap between this collider and Contact.Other.

Each collider has its own record for a pair. Both records share the point and penetration, but their normals point in opposite directions. Point is representative of the overlap, not a contact manifold.

type Contacts

type Contacts struct {
	// Current is this tick's contacts, sorted by Other.ID().
	Current []Contact
	// Previous is the contacts of the tick before this one, sorted by Other.ID().
	Previous []Contact
}

Contacts is the collision system's per-entity snapshot of detected overlaps.

NewSystem writes it for entities with both a Collider and a core.Transform during fixed updates. Contacts.Current contains the contacts detected in the latest update; Contacts.Previous contains those from the update before it. Comparing their Contact.Other handles identifies contacts that began, continued, or ended:

enter: in Current, not in Previous
stay:  in both
exit:  in Previous, not in Current

Both slices are sorted by the ID of Contact.Other. For enter and exit edges, games can also consume Entered and Exited; Contacts remains the source of truth for current contacts and stay state. The slices are read-only and valid until the next collision update. Copy them to keep contact history across updates.

The collision system attaches and owns Contacts, removing it when either Collider or core.Transform is removed. Game code reads it but does not write or remove it.

type Entered added in v0.3.0

type Entered struct {
	// Entity is the collider whose Contacts gained the pair.
	Entity core.Entity
	// Other is the entity it began overlapping.
	Other core.Entity
	// Point is the pair's representative contact point.
	Point geom.Vector2
	// Normal points from Entity toward Other.
	Normal geom.Vector2
	// Penetration is the overlap depth along the normal.
	Penetration float64
	// Trigger is true if either collider in the pair is a trigger.
	Trigger bool
	// Tick is the fixed tick the pair began overlapping on.
	Tick uint64
}

Entered is emitted when this collider's Contacts.Current gains a contact during a fixed update.

The collision system emits one signal per perspective, so a pair produces two signals with opposing normals. Contact geometry matches the new record in Contacts.Current.

type Exited added in v0.3.0

type Exited struct {
	// Entity is the collider whose Contacts lost the pair.
	Entity core.Entity
	// Other is the entity it stopped overlapping.
	Other core.Entity
	// Tick is the fixed tick the pair ended on.
	Tick uint64
}

Exited is emitted when this collider's Contacts.Current loses a contact during a fixed update.

The collision system emits one signal per perspective, like Entered. Since the pair no longer has contact geometry, the signal carries only the entity handles and tick. A collider removed during the update does not emit its own exits; any remaining collider's signal reports the pair.

type Queries added in v0.4.0

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

Queries provides spatial queries over collider data synchronized by the collision system. The zero value is not ready for use; register a value returned by NewQueries with the world.

func NewQueries added in v0.4.0

func NewQueries() (*Queries, error)

NewQueries creates an empty spatial query resource for a world. Register the returned resource before running NewSystem.

func (*Queries) Raycast added in v0.4.0

func (q *Queries) Raycast(segment geom.Segment, filter RaycastFilter) (RaycastHit, bool)

Raycast returns the closest hit along segment among colliders whose Layers intersect filter.Mask. It reads the latest successful collision-system sync; before the first sync, it returns no hit.

func (*Queries) Tick added in v0.4.0

func (q *Queries) Tick() uint64

Tick reports the fixed tick of the latest successful broadphase sync. It is zero before the collision system's first successful update.

type RaycastFilter added in v0.4.0

type RaycastFilter struct {
	// Mask selects colliders whose Layers intersect this mask. Collider.Mask
	// does not filter raycasts; it controls pairwise collider interactions.
	Mask uint32
	// Exclude lists entities to ignore.
	Exclude []core.Entity
}

RaycastFilter selects target collider layers and entities to exclude.

type RaycastHit added in v0.4.0

type RaycastHit struct {
	// Entity identifies the entity whose collider was hit.
	Entity core.Entity
	// Collider is the collider data from the synchronized snapshot that produced the hit.
	Collider Collider
	// Point is the hit location in world coordinates.
	Point geom.Vector2
	// Normal is the hit surface normal in world coordinates.
	Normal geom.Vector2
	// Distance is the distance from the segment start to Point.
	Distance float64
}

RaycastHit describes the closest collider hit by a raycast.

Jump to

Keyboard shortcuts

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