collision

package
v2.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2018 License: Apache-2.0 Imports: 10 Imported by: 84

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) floatgeom.Rect3

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 {
	floatgeom.Point3
	Zone *Space
}

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

func NewPoint

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

NewPoint creates a new point

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

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 Rtree

type Rtree struct {
	MinChildren int
	MaxChildren int
	// contains filtered or unexported fields
}

Rtree represents an R-tree, a balanced search tree for storing and querying Space objects. MinChildren/MaxChildren specify the minimum/maximum branching factors.

func (*Rtree) Delete

func (tree *Rtree) Delete(obj *Space) bool

Delete removes an object from the tree. If the object is not found, ok is false; otherwise ok is true.

Implemented per Section 3.3 of "R-trees: A Dynamic Index Structure for Space Searching" by A. Guttman, Proceedings of ACM SIGMOD, p. 47-57, 1984.

func (*Rtree) Insert

func (tree *Rtree) Insert(obj *Space)

Insert inserts a Space object into the tree. If insertion causes a leaf node to overflow, the tree is rebalanced automatically.

Implemented per Section 3.2 of "R-trees: A Dynamic Index Structure for Space Searching" by A. Guttman, Proceedings of ACM SIGMOD, p. 47-57, 1984.

func (*Rtree) NearestNeighbor

func (tree *Rtree) NearestNeighbor(p floatgeom.Point3) *Space

NearestNeighbor returns the closest object to the specified point. Implemented per "Nearest Neighbor Queries" by Roussopoulos et al

func (*Rtree) NearestNeighbors

func (tree *Rtree) NearestNeighbors(k int, p floatgeom.Point3) []*Space

NearestNeighbors returns the k nearest neighbors in the rtree to the input point.

func (*Rtree) SearchIntersect

func (tree *Rtree) SearchIntersect(bb floatgeom.Rect3) []*Space

SearchIntersect returns all objects that intersect the specified rectangle.

Implemented per Section 3.1 of "R-trees: A Dynamic Index Structure for Space Searching" by A. Guttman, Proceedings of ACM SIGMOD, p. 47-57, 1984.

type Space

type Space struct {
	Location floatgeom.Rect3
	// 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 NewRectSpace

func NewRectSpace(rect floatgeom.Rect3, l Label, cID event.CID) *Space

NewRectSpace creates a colliison space with the specified 3D rectangle

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() floatgeom.Rect3

Bounds satisfies the rtreego.Spatial interface.

func (*Space) Contains

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

Contains returns whether this space contains another

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) 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) error

SetDim sets the dimensions of the space in the default 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 subrectangle 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 default rtree

func (*Space) UpdateLabel

func (s *Space) UpdateLabel(classtype Label)

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

func (*Space) X

func (s *Space) X() float64

X returns a space's x position (leftmost)

func (*Space) Y

func (s *Space) Y() float64

Y returns a space's y position (upmost)

type Tree

type Tree struct {
	*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.

func NewTree

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

NewTree returns a new collision Tree. The first argument will be used as the minimum children per tree node. The second will be the maximum children per tree node. Further arguments are ignored. If less than two arguments are given, DefaultMinChildren and DefaultMaxChildren will be used.

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) 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) 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) UpdateLabel

func (t *Tree) UpdateLabel(classtype Label, s *Space)

UpdateLabel will set the input space's label in this tree. Modifying a space's label without going through a tree's method such as this will have no effect.

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.

func (*Tree) UpdateSpaceRect

func (t *Tree) UpdateSpaceRect(rect floatgeom.Rect3, s *Space) error

UpdateSpaceRect acts as UpdateSpace, but takes in a rectangle instead of four distinct arguments.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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