quad

package
v0.0.0-...-73b4bbd Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2021 License: MIT Imports: 5 Imported by: 1

README

GoDoc

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrBoundsHeightMustBePowerOf2 = errors.New("bounds height must be a power of 2")
View Source
var ErrBoundsWidthMustBePowerOf2 = errors.New("bounds width must be a power of 2")
View Source
var ErrMaxSizeTooSmall = errors.New("max size must be > 1")

Guards against unspecified behavior if the maxSize is 1

Functions

func RunBroadPhaseOn

func RunBroadPhaseOn(
	q Quad,
	now stime.Time) (cgroups []*CollisionGroup, solved, unsolved CollisionGroupIndex)

Types

type Chunk

type Chunk struct {
	Bounds coord.Bounds

	Entities []entity.Entity
}

A group of entities within a bounding rectangle. A chunk is sent to the collision function that is implemented by the user of engine.

type Collision

type Collision struct {
	A, B entity.Entity
}

A collision between 2 entities because the entities bounds are overlapping. Intended to be solved by the user defined NarrowPhaseHandler.

func (Collision) Bounds

func (c Collision) Bounds() coord.Bounds

The bounds of A and B joined together.

func (Collision) IsSameAs

func (c Collision) IsSameAs(oc Collision) bool

Compares to Collisions and returns if they are representing the same collision.

type CollisionGroup

type CollisionGroup struct {
	// A slice of the all the entities that are in
	// the collisions of the group.
	Entities []entity.Entity

	// A slice of all the collisions in the group.
	Collisions []Collision
}

A group of collisions where each collision may have an effect on the others. A dependency tree should be created by the user to resolve the collisions in the correct order.

func (CollisionGroup) AddCollision

func (cg CollisionGroup) AddCollision(c Collision) CollisionGroup

Adds a collision to the group. Also adds the entities from the collision to the entities slice. Filters out collisions it already has and entities that are already in the entities slice.

func (CollisionGroup) Bounds

func (cg CollisionGroup) Bounds() coord.Bounds

func (CollisionGroup) CollisionIndex

func (cg CollisionGroup) CollisionIndex() CollisionIndex

type CollisionGroupIndex

type CollisionGroupIndex map[entity.Entity]*CollisionGroup

An entity may ONLY be assigned to 1 collision group. If an entity has collisions that are in separate collision groups, those groups must be merged. This rules make the collision group index possible.

type CollisionIndex

type CollisionIndex map[entity.Entity][]Collision

A collision index stores all the collisions an entity is involved in.

type Corner

type Corner coord.Quad

Represents one of the four quad corners

type InputPhaseHandler

type InputPhaseHandler interface {
	ApplyInputsTo(entity.Entity, stime.Time) []entity.Entity
}

2. Input Application Phase - User Defined

The input phase takes the user input and applies it. This application can modify the entities movement state or create new entities. The input phase should return a slice of entities that includes the entity that it was applying input to. This slice of entities can also include any entities that may have been created by the actor's input (using skills and spells, chat messages, etc). These new entities will be inserted into the quad tree.

type InputPhaseHandlerFn

type InputPhaseHandlerFn func(entity.Entity, stime.Time) []entity.Entity

Convenience type so input phase handlers can be written as closures or as functions.

func (InputPhaseHandlerFn) ApplyInputsTo

func (f InputPhaseHandlerFn) ApplyInputsTo(e entity.Entity, now stime.Time) []entity.Entity

type NarrowPhaseHandler

type NarrowPhaseHandler interface {
	ResolveCollisions(*CollisionGroup, stime.Time) (entities []entity.Entity, removed []entity.Entity)
}

4. Narrow Phase - User Defined

The narrow phase resolves all the collisions in a collision group. The phase handler should return 2 slices of entities. The first is the entities that still exist or have been created. The second is any entities that have been destroyed. The user implementation should also be where movement actions are accepted and an entities position is modified.

type NarrowPhaseHandlerFn

type NarrowPhaseHandlerFn func(*CollisionGroup, stime.Time) ([]entity.Entity, []entity.Entity)

Convenience type so narrow phase handlers can be written as closures or as functions.

func (NarrowPhaseHandlerFn) ResolveCollisions

func (f NarrowPhaseHandlerFn) ResolveCollisions(cgrp *CollisionGroup, now stime.Time) ([]entity.Entity, []entity.Entity)

type Quad

type Quad interface {
	Parent() Quad
	Child(Corner) Quad
	Children() []Quad

	Bounds() coord.Bounds

	// Mutators
	Insert(entity.Entity) Quad
	Remove(entity.Entity) Quad

	QueryCell(coord.Cell) []entity.Entity
	QueryBounds(coord.Bounds) []entity.Entity

	Chunk() Chunk
	// contains filtered or unexported methods
}

An interface used to abstract the implementation differences of a node and a leaf.

func New

func New(bounds coord.Bounds, maxSize int, entities []entity.Entity) (Quad, error)

func RunInputPhaseOn

func RunInputPhaseOn(
	q Quad,
	inputPhase InputPhaseHandler,
	now stime.Time) (Quad, []entity.Entity)

func RunNarrowPhaseOn

func RunNarrowPhaseOn(
	q Quad,
	cgroups []*CollisionGroup,
	narrowPhase NarrowPhaseHandler,
	now stime.Time) (Quad, []entity.Entity)

func RunPhasesOn

func RunPhasesOn(
	q Quad,
	updatePhase UpdatePhaseHandler,
	inputPhase InputPhaseHandler,
	narrowPhase NarrowPhaseHandler,
	now stime.Time) Quad

func RunUpdatePhaseOn

func RunUpdatePhaseOn(q Quad, updatePhase UpdatePhaseHandler, now stime.Time) (Quad, []entity.Entity, []entity.Entity)

type UpdatePhaseHandler

type UpdatePhaseHandler interface {
	Update(entity.Entity, stime.Time) entity.Entity
}

1. Update Position Phase - User Defined

This phase is for updating an entity's position if their previous movement has be completed on this tick. This must happen before the input application phase so the quad tree can be queried during that phase and the information won't be racy depending on the order that the input hase been applied. If update returns nil instead of the entity, it will be removed.

type UpdatePhaseHandlerFn

type UpdatePhaseHandlerFn func(entity.Entity, stime.Time) entity.Entity

Convenience type so input phase handlers can be written as closures or as functions.

func (UpdatePhaseHandlerFn) Update

Jump to

Keyboard shortcuts

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