collision

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2018 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package collision provides collision tree and space structures, raycasting utilities and hit detection functions on spaces.

Index

Constants

View Source
const (
	Start = "CollisionStart"
	Stop  = "CollisionStop"
)

CollisionStart/Stop: when a PhaseCollision entity starts/stops touching some label. Payload: (Label) the label the entity has started/stopped touching

View Source
const (
	NONE = iota
	CID
	PID
)

ID Types constant

Variables

View Source
var (
	// DefaultMaxChildren is the maximum number of children allowed
	// on a node in the collision tree when NewTree is called without
	// a maximum number of children.
	DefaultMaxChildren = 40
	// DefaultMinChildren is the minimum number of children allowed
	// on a node in the collision tree when NewTree is called without
	// a minimum number of children.
	DefaultMinChildren = 20
)

Functions

func Add

func Add(sps ...*Space)

Add adds a set of spaces to the rtree

func Attach

func Attach(v physics.Vector, s *Space, offsets ...float64) error

Attach attaches v to the given space with optional x,y offsets. See AttachSpace. Attach binds attachSpaceEnter at priority -1. This means that attachSpaceEnter, which updates the collision space for an AttachSpace composed entity, will be called after all EnterFrame bindings that are bound with .Bind(), but before those that are called with .BindPriority(... -2).

func CallOnHits

func CallOnHits(s *Space, m map[Label]OnHit, doneCh chan bool)

CallOnHits will send a signal to the passed in channel when it has completed all collision functions in the hitmap. It acts on DefTree. Todo: Change that

func Clear

func Clear()

Clear resets the default tree's contents

func Detach

func Detach(s *Space) error

Detach removes the attachSpaceEnter binding from an entity composed with AttachSpace

func NewRect

func NewRect(x, y, w, h float64) *rtreego.Rect

NewRect is a wrapper around rtreego.NewRect, casting the given x,y to an rtreego.Point. Used to not expose rtreego.Point to the user. Invalid widths and heights are converted to be valid. If zero width or height is given, it is replaced with 1. If a negative width or height is given, the rectangle is shifted to the left or up by that negative dimension and the dimension is made positive.

func OnIDs

func OnIDs(fn func(int, int)) func(s, s2 *Space)

OnIDs converts a function on two CIDs to an OnHit

func PhaseCollision

func PhaseCollision(s *Space, trees ...*Tree) error

PhaseCollision binds to the entity behind the space's CID so that it will receive CollisionStart and CollisionStop events, appropriately when entities begin to collide or stop colliding with the space.

func Remove

func Remove(sps ...*Space)

Remove removes a space from the rtree

func ShiftSpace

func ShiftSpace(x, y float64, s *Space) error

ShiftSpace adds x and y to a space and updates its position in the collision rtree that should not be a package global

func UpdateSpace

func UpdateSpace(x, y, w, h float64, s *Space) error

UpdateSpace resets a space's location to a given rtreego.Rect. This is not an operation on a space because a space can exist in multiple rtrees.

Types

type AttachSpace

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

An AttachSpace is a composable struct that provides attachment functionality for entities. An entity with AttachSpace can have its associated space passed into Attach with the vector the space should be attached to. Example usage: Any moving character with a collision space. When moving the character around by the vector passed in to Attach, the space will move with it.

type Filter

type Filter func([]*Space) []*Space

A Filter will take a set of collision spaces and return the subset that match some requirement

func FirstLabel

func FirstLabel(ls ...Label) Filter

FirstLabel returns the first space that has a label in the input, or nothing

func With

func With(keepFn func(*Space) bool) Filter

With will filter spaces so that only those returning true from the input keepFn will be in the output

func WithLabels

func WithLabels(ls ...Label) Filter

WithLabels will only return spaces with a label in the input

func Without

func Without(tossFn func(*Space) bool) Filter

Without will filter spaces so that no spaces returning true from the input tossFn will be in the output

func WithoutCIDs

func WithoutCIDs(cids ...event.CID) Filter

WithoutCIDs will return no spaces with a CID in the input

func WithoutLabels

func WithoutLabels(ls ...Label) Filter

WithoutLabels will return no spaces with a label in the input

type Label

type Label int

Label is used to store type information for a given space

const (
	// NilLabel is used internally for spaces that are otherwise not
	// given labels.
	NilLabel Label = -1
)

type OnHit

type OnHit func(s, s2 *Space)

An OnHit is a function which takes in two spaces

type Phase

type Phase struct {
	OnCollisionS *Space

	// If allocating maps becomes an issue
	// we can have two constant maps that we
	// switch between on alternating frames
	Touching map[Label]bool
	// contains filtered or unexported fields
}

A Phase is a struct that other structs who want to use PhaseCollision should be composed of

type Point

type Point struct {
	physics.Vector
	Zone *Space
}

A Point is a specific point where collision occurred and a zone to identify what was collided with.

func ConeCast

func ConeCast(x, y, angle, angleWidth, rays, length float64) (points []Point)

ConeCast repeatedly calls RayCast in a cone shape ConeCast advances COUNTER-CLOCKWISE

func ConeCastSingle

func ConeCastSingle(x, y, angle, angleWidth, rays, length float64, invalidIDS []event.CID) (points []Point)

ConeCastSingle repeatedly calls RayCastSignle in a cone shape

func ConeCastSingleLabels

func ConeCastSingleLabels(x, y, angle, angleWidth, rays, length float64, labels ...Label) (points []Point)

ConeCastSingleLabels repeatedly calls RayCastSingleLabels in a cone shape

func ConeCastSinglePiercing added in v1.5.0

func ConeCastSinglePiercing(x, y, angle, angleWidth, rays, length float64, pierceCount int, labels ...Label) (points []Point)

ConeCastSingleLabels repeatedly calls RayCastLabelsPiercing in a cone shape

func NewPoint

func NewPoint(s *Space, x, y float64) Point

NewPoint creates a new point

func NilPoint

func NilPoint() Point

NilPoint returns a Point representing no collision

func RayCast

func RayCast(x, y, degrees, length float64) []Point

RayCast returns the set of points where a line from x,y going at a certain angle, for a certain length, intersects with existing rectangles in the rtree. It converts the ray into a series of points which are themselves used to check collision at a miniscule width and height.

func RayCastLabelsPiercing added in v1.5.0

func RayCastLabelsPiercing(x, y, degrees, length float64, pierceCount int, labels ...Label) Point

RayCastLabelsPiercing disregards "pierceCount" first results of RayCastSingleLabels

func RayCastSingle

func RayCastSingle(x, y, degrees, length float64, invalidIDS []event.CID) Point

RayCastSingle acts as RayCast, but it returns only the first collision that the generated ray intersects, ignoring entities in the given invalidIDs list. Example Use case: shooting a bullet, hitting the first thing that isn't yourself.

func RayCastSingleIgnore

func RayCastSingleIgnore(x, y, degrees, length float64, invalidIDS []event.CID, labels ...Label) Point

RayCastSingleIgnore is just like ignore labels but also ignores certain caller ids

func RayCastSingleIgnoreLabels

func RayCastSingleIgnoreLabels(x, y, degrees, length float64, labels ...Label) Point

RayCastSingleIgnoreLabels is the opposite of Labels, in that it will return the first collision point that is not contained in the set of ignore labels

func RayCastSingleIgnorePiercing added in v1.5.0

func RayCastSingleIgnorePiercing(x, y, degrees, length float64, pierceCount int, invalidIDS []event.CID, labels ...Label) Point

RayCastSingleIgnorePiercing disregards "pierceCount" first results of RayCastSingleIgnore

func RayCastSingleLabels

func RayCastSingleLabels(x, y, degrees, length float64, labels ...Label) Point

RayCastSingleLabels acts like RayCastSingle, but only returns elements that match one of the input labels

func (Point) IsNil

func (cp Point) IsNil() bool

IsNil returns whether the underlying zone of a Point is nil

type ReactiveSpace

type ReactiveSpace struct {
	*Space
	// contains filtered or unexported fields
}

ReactiveSpace is a space that keeps track of a map of collision events

func NewEmptyReactiveSpace

func NewEmptyReactiveSpace(s *Space) *ReactiveSpace

NewEmptyReactiveSpace returns a reactive space with no onHit mapping

func NewReactiveSpace

func NewReactiveSpace(s *Space, onHits map[Label]OnHit) *ReactiveSpace

NewReactiveSpace creates a reactive space, please don't send in a nil map to this, use NewEmptyReactiveSpace instead

func (*ReactiveSpace) Add

func (rs *ReactiveSpace) Add(i Label, oh OnHit)

Add adds a mapping to a reactive spaces' onhit map

func (*ReactiveSpace) CallOnHits

func (rs *ReactiveSpace) CallOnHits() chan bool

CallOnHits calls CallOnHits on the underlying space of a reactive space with the reactive spaces' map of collision events, and returns the channel it will send the done signal from.

func (*ReactiveSpace) Clear

func (rs *ReactiveSpace) Clear()

Clear resets a reactive space's onhit map

func (*ReactiveSpace) Remove

func (rs *ReactiveSpace) Remove(i Label)

Remove removes a mapping from a reactive spaces' onhit map

type Space

type Space struct {
	Location *rtreego.Rect
	// A label can store type information.
	// Recommended to use with an enum.
	Label Label
	// A CID can be used to get the exact
	// entity which this rectangle belongs to.
	CID event.CID
	// Type represents which ID space the above ID
	// corresponds to.
	Type int
}

A Space is a rectangle with a couple of ways of identifying an underlying object.

func HitLabel

func HitLabel(sp *Space, labels ...Label) *Space

HitLabel acts like hits, but reutrns the first space within hits that matches one of the input labels

func Hits

func Hits(sp *Space) []*Space

Hits returns the set of spaces which are colliding with the passed in space.

func NewFullSpace

func NewFullSpace(x, y, w, h float64, l Label, cID event.CID) *Space

NewFullSpace returns a space with both a label and a caller id

func NewLabeledSpace

func NewLabeledSpace(x, y, w, h float64, l Label) *Space

NewLabeledSpace returns a space with an associated integer label

func NewSpace

func NewSpace(x, y, w, h float64, cID event.CID) *Space

NewSpace returns a space with an associated caller id

func NewUnassignedSpace

func NewUnassignedSpace(x, y, w, h float64) *Space

NewUnassignedSpace returns a space that just has a rectangle

func (*Space) Above

func (s *Space) Above(other *Space) float64

Above returns how much above this space another space is Important note: (10,10) is Above (10,20), because in oak's display, lower y values are higher than higher y values.

func (*Space) Below

func (s *Space) Below(other *Space) float64

Below returns how much below this space another space is, Equivalent to -1 * Above

func (*Space) Bounds

func (s *Space) Bounds() *rtreego.Rect

Bounds satisfies the rtreego.Spatial interface.

func (*Space) Contains

func (s *Space) Contains(other *Space) bool

Contains returns whether this space contains other

func (*Space) GetCenter

func (s *Space) GetCenter() (float64, float64)

GetCenter returns the center point of the space

func (*Space) GetH

func (s *Space) GetH() float64

GetH returns a space's height (upper y - lower y)

func (*Space) GetPos

func (s *Space) GetPos() (float64, float64)

GetPos returns both y and x

func (*Space) GetW

func (s *Space) GetW() float64

GetW returns a space's width (rightmost x - leftmost x)

func (*Space) GetX

func (s *Space) GetX() float64

GetX returns a space's x position (leftmost)

func (*Space) GetY

func (s *Space) GetY() float64

GetY returns a space's y position (upmost)

func (*Space) LeftOf

func (s *Space) LeftOf(other *Space) float64

LeftOf returns how far to the left other is of this space

func (*Space) Overlap

func (s *Space) Overlap(other *Space) (xOver, yOver float64)

Overlap returns how much this space overlaps with another space

func (*Space) OverlapVector

func (s *Space) OverlapVector(other *Space) physics.Vector

OverlapVector returns Overlap as a vector

func (*Space) RightOf

func (s *Space) RightOf(other *Space) float64

RightOf returns how far to the right other is of this space. Equivalent to -1 * LeftOf

func (*Space) SetDim

func (s *Space) SetDim(w, h float64)

SetDim sets the dimensions of the space in the legacy rtree

func (*Space) String

func (s *Space) String() string

func (*Space) SubtractRect

func (s *Space) SubtractRect(x2, y2, w2, h2 float64) []*Space

SubtractRect removes a rectangle from this rectangle and returns the rectangles remaining after the portion has been removed. The input x,y is relative to the original space: Example: removing 1,1 from 10,10 -> 12,12 is OK, but removing 11,11 from 10,10 -> 12,12 will not act as expected.

func (*Space) Update

func (s *Space) Update(x, y, w, h float64) error

Update updates this space with the legacy rtree

func (*Space) UpdateLabel

func (s *Space) UpdateLabel(classtype Label)

UpdateLabel changes the label behind this space and resets it in the legacy rtree

type Tree

type Tree struct {
	*rtreego.Rtree
	sync.Mutex
	// contains filtered or unexported fields
}

A Tree provides a space for managing collisions between rectangles

var (
	DefTree *Tree
)

There's a default collision tree you can access via collision.func as opposed to tree.func. This is considered a legacy set of features, because the benefit to the API is minimal in exchange for a much harder to use collision tree. It does make small applications a little shorter.

func NewTree

func NewTree(children ...int) (*Tree, error)

NewTree returns a new collision Tree

func (*Tree) Add

func (t *Tree) Add(sps ...*Space)

Add adds a set of spaces to the rtree

func (*Tree) Clear

func (t *Tree) Clear()

Clear resets a tree's contents to be empty

func (*Tree) ConeCast

func (t *Tree) ConeCast(x, y, angle, angleWidth, rays, length float64) (points []Point)

ConeCast repeatedly calls RayCast in a cone shape ConeCast advances COUNTER-CLOCKWISE

func (*Tree) ConeCastSingle

func (t *Tree) ConeCastSingle(x, y, angle, angleWidth, rays, length float64, invalidIDS []event.CID) (points []Point)

ConeCastSingle repeatedly calls RayCastSingle in a cone shape

func (*Tree) ConeCastSingleLabels

func (t *Tree) ConeCastSingleLabels(x, y, angle, angleWidth, rays, length float64, labels ...Label) (points []Point)

ConeCastSingleLabels repeatedly calls RayCastSingleLabels in a cone shape

func (*Tree) ConeCastSinglePiercing added in v1.5.0

func (t *Tree) ConeCastSinglePiercing(x, y, angle, angleWidth, rays, length float64, pierceCount int, labels ...Label) (points []Point)

ConeCastSingleLabels repeatedly calls RayCastLabelsPiercing in a cone shape

func (*Tree) Hit

func (t *Tree) Hit(sp *Space, fs ...Filter) []*Space

Hit is an experimental new syntax that probably has performance hits relative to Hits/HitLabel, see filters.go

func (*Tree) HitLabel

func (t *Tree) HitLabel(sp *Space, labels ...Label) *Space

HitLabel acts like Hits, but returns the first space within hits that matches one of the input labels. HitLabel can return the same space that is passed into it, if that space has a label in the set of accepted labels.

func (*Tree) Hits

func (t *Tree) Hits(sp *Space) []*Space

Hits returns the set of spaces which are colliding with the passed in space. All spaces collide with themselves, if they exist in the tree, but self-collision will not be reported by Hits.

func (*Tree) RayCast

func (t *Tree) RayCast(x, y, degrees, length float64) []Point

RayCast returns the set of points where a line from x,y going at a certain angle, for a certain length, intersects with existing rectangles in the rtree. It converts the ray into a series of points which are themselves used to check collision at a miniscule width and height.

func (*Tree) RayCastLabelsPiercing added in v1.5.0

func (t *Tree) RayCastLabelsPiercing(x, y, degrees, length float64, pierceCount int, labels ...Label) Point

RayCastLabelsPiercing disregards "pierceCount" first results of RayCastSingleLabels

func (*Tree) RayCastSingle

func (t *Tree) RayCastSingle(x, y, degrees, length float64, invalidIDS []event.CID) Point

RayCastSingle acts as RayCast, but it returns only the first collision that the generated ray intersects, ignoring entities in the given invalidIDs list. Example Use case: shooting a bullet, hitting the first thing that isn't yourself. Todo: this should use variadic args like the label functions, but we should also not be using these anyway, see top todo

func (*Tree) RayCastSingleIgnore

func (t *Tree) RayCastSingleIgnore(x, y, degrees, length float64, invalidIDS []event.CID, labels ...Label) Point

RayCastSingleIgnore is just like ignore labels but also ignores certain caller ids

func (*Tree) RayCastSingleIgnoreLabels

func (t *Tree) RayCastSingleIgnoreLabels(x, y, degrees, length float64, labels ...Label) Point

RayCastSingleIgnoreLabels is the opposite of Labels, in that it will return the first collision point that is not contained in the set of ignore labels

func (*Tree) RayCastSingleIgnorePiercing added in v1.5.0

func (t *Tree) RayCastSingleIgnorePiercing(x, y, degrees, length float64, pierceCount int, invalidIDS []event.CID, labels ...Label) Point

RayCastSingleIgnorePiercing disregards "pierceCount" first results of RayCastSingleIgnore

func (*Tree) RayCastSingleLabels

func (t *Tree) RayCastSingleLabels(x, y, degrees, length float64, labels ...Label) Point

RayCastSingleLabels acts like RayCastSingle, but only returns elements that match one of the input labels

func (*Tree) Remove

func (t *Tree) Remove(sps ...*Space) int

Remove removes spaces from the rtree and returns the number of spaces removed.

func (*Tree) ShiftSpace

func (t *Tree) ShiftSpace(x, y float64, s *Space) error

ShiftSpace adds x and y to a space and updates its position

func (*Tree) UpdateSpace

func (t *Tree) UpdateSpace(x, y, w, h float64, s *Space) error

UpdateSpace resets a space's location to a given rtreego.Rect. This is not an operation on a space because a space can exist in multiple rtrees.

Jump to

Keyboard shortcuts

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