Documentation ¶
Overview ¶
Package collision provides collision tree and space structures, raycasting utilities and hit detection functions on spaces.
Index ¶
- Constants
- Variables
- func Add(sps ...*Space)
- func Attach(v physics.Vector, s *Space, offsets ...float64) error
- func CallOnHits(s *Space, m map[Label]OnHit, doneCh chan bool)
- func Clear()
- func Detach(s *Space) error
- func NewRect(x, y, w, h float64) floatgeom.Rect3
- func OnIDs(fn func(int, int)) func(s, s2 *Space)
- func PhaseCollision(s *Space, trees ...*Tree) error
- func Remove(sps ...*Space)
- func ShiftSpace(x, y float64, s *Space) error
- func UpdateSpace(x, y, w, h float64, s *Space) error
- type AttachSpace
- type Filter
- type Label
- type OnHit
- type Phase
- type Point
- type ReactiveSpace
- type Rtree
- type Space
- func HitLabel(sp *Space, labels ...Label) *Space
- func Hits(sp *Space) []*Space
- func NewFullSpace(x, y, w, h float64, l Label, cID event.CID) *Space
- func NewLabeledSpace(x, y, w, h float64, l Label) *Space
- func NewRectSpace(rect floatgeom.Rect3, l Label, cID event.CID) *Space
- func NewSpace(x, y, w, h float64, cID event.CID) *Space
- func NewUnassignedSpace(x, y, w, h float64) *Space
- func (s *Space) Above(other *Space) float64
- func (s *Space) Below(other *Space) float64
- func (s *Space) Bounds() floatgeom.Rect3
- func (s *Space) Contains(other *Space) bool
- func (s *Space) GetCenter() (float64, float64)
- func (s *Space) GetH() float64
- func (s *Space) GetPos() (float64, float64)
- func (s *Space) GetW() float64
- func (s *Space) LeftOf(other *Space) float64
- func (s *Space) Overlap(other *Space) (xOver, yOver float64)
- func (s *Space) OverlapVector(other *Space) physics.Vector
- func (s *Space) RightOf(other *Space) float64
- func (s *Space) SetDim(w, h float64) error
- func (s *Space) String() string
- func (s *Space) SubtractRect(x2, y2, w2, h2 float64) []*Space
- func (s *Space) Update(x, y, w, h float64) error
- func (s *Space) UpdateLabel(classtype Label)
- func (s *Space) X() float64
- func (s *Space) Y() float64
- type Tree
- func (t *Tree) Add(sps ...*Space)
- func (t *Tree) Clear()
- func (t *Tree) Hit(sp *Space, fs ...Filter) []*Space
- func (t *Tree) HitLabel(sp *Space, labels ...Label) *Space
- func (t *Tree) Hits(sp *Space) []*Space
- func (t *Tree) Remove(sps ...*Space) int
- func (t *Tree) ShiftSpace(x, y float64, s *Space) error
- func (t *Tree) UpdateLabel(classtype Label, s *Space)
- func (t *Tree) UpdateSpace(x, y, w, h float64, s *Space) error
- func (t *Tree) UpdateSpaceRect(rect floatgeom.Rect3, s *Space) error
Constants ¶
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
const ( NONE = iota CID PID )
ID Types constant
Variables ¶
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 Attach ¶
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 ¶
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 NewRect ¶
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 PhaseCollision ¶
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 ShiftSpace ¶
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 ¶
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 ¶
A Filter will take a set of collision spaces and return the subset that match some requirement
func FirstLabel ¶
FirstLabel returns the first space that has a label in the input, or nothing
func With ¶
With will filter spaces so that only those returning true from the input keepFn will be in the output
func WithLabels ¶
WithLabels will only return spaces with a label in the input
func Without ¶
Without will filter spaces so that no spaces returning true from the input tossFn will be in the output
func WithoutCIDs ¶
WithoutCIDs will return no spaces with a CID in the input
func WithoutLabels ¶
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 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 ¶
A Point is a specific point where collision occurred and a zone to identify what was collided with.
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 ¶
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 ¶
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 ¶
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 ¶
NearestNeighbor returns the closest object to the specified point. Implemented per "Nearest Neighbor Queries" by Roussopoulos et al
func (*Rtree) NearestNeighbors ¶
NearestNeighbors returns the k nearest neighbors in the rtree to the input point.
func (*Rtree) SearchIntersect ¶
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 ¶
HitLabel acts like hits, but reutrns the first space within hits that matches one of the input labels
func NewFullSpace ¶
NewFullSpace returns a space with both a label and a caller id
func NewLabeledSpace ¶
NewLabeledSpace returns a space with an associated integer label
func NewRectSpace ¶
NewRectSpace creates a colliison space with the specified 3D rectangle
func NewUnassignedSpace ¶
NewUnassignedSpace returns a space that just has a rectangle
func (*Space) Above ¶
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 ¶
Below returns how much below this space another space is, Equivalent to -1 * Above
func (*Space) OverlapVector ¶
OverlapVector returns Overlap as a vector
func (*Space) RightOf ¶
RightOf returns how far to the right other is of this space. Equivalent to -1 * LeftOf
func (*Space) SubtractRect ¶
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) UpdateLabel ¶
UpdateLabel changes the label behind this space and resets it in the default rtree
type Tree ¶
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 ¶
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) Hit ¶
Hit is an experimental new syntax that probably has performance hits relative to Hits/HitLabel, see filters.go
func (*Tree) HitLabel ¶
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 ¶
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 ¶
Remove removes spaces from the rtree and returns the number of spaces removed.
func (*Tree) ShiftSpace ¶
ShiftSpace adds x and y to a space and updates its position
func (*Tree) UpdateLabel ¶
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 ¶
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.