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) *rtreego.Rect
- 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
- func ConeCast(x, y, angle, angleWidth, rays, length float64) (points []Point)
- func ConeCastSingle(x, y, angle, angleWidth, rays, length float64, invalidIDS []event.CID) (points []Point)
- func ConeCastSingleLabels(x, y, angle, angleWidth, rays, length float64, labels ...Label) (points []Point)
- func ConeCastSinglePiercing(x, y, angle, angleWidth, rays, length float64, pierceCount int, ...) (points []Point)
- func NewPoint(s *Space, x, y float64) Point
- func NilPoint() Point
- func RayCast(x, y, degrees, length float64) []Point
- func RayCastLabelsPiercing(x, y, degrees, length float64, pierceCount int, labels ...Label) Point
- func RayCastSingle(x, y, degrees, length float64, invalidIDS []event.CID) Point
- func RayCastSingleIgnore(x, y, degrees, length float64, invalidIDS []event.CID, labels ...Label) Point
- func RayCastSingleIgnoreLabels(x, y, degrees, length float64, labels ...Label) Point
- func RayCastSingleIgnorePiercing(x, y, degrees, length float64, pierceCount int, invalidIDS []event.CID, ...) Point
- func RayCastSingleLabels(x, y, degrees, length float64, labels ...Label) Point
- type ReactiveSpace
- 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 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() *rtreego.Rect
- 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) GetX() float64
- func (s *Space) GetY() 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)
- 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)
- type Tree
- func (t *Tree) Add(sps ...*Space)
- func (t *Tree) Clear()
- func (t *Tree) ConeCast(x, y, angle, angleWidth, rays, length float64) (points []Point)
- func (t *Tree) ConeCastSingle(x, y, angle, angleWidth, rays, length float64, invalidIDS []event.CID) (points []Point)
- func (t *Tree) ConeCastSingleLabels(x, y, angle, angleWidth, rays, length float64, labels ...Label) (points []Point)
- func (t *Tree) ConeCastSinglePiercing(x, y, angle, angleWidth, rays, length float64, pierceCount int, ...) (points []Point)
- 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) RayCast(x, y, degrees, length float64) []Point
- func (t *Tree) RayCastLabelsPiercing(x, y, degrees, length float64, pierceCount int, labels ...Label) Point
- func (t *Tree) RayCastSingle(x, y, degrees, length float64, invalidIDS []event.CID) Point
- func (t *Tree) RayCastSingleIgnore(x, y, degrees, length float64, invalidIDS []event.CID, labels ...Label) Point
- func (t *Tree) RayCastSingleIgnoreLabels(x, y, degrees, length float64, labels ...Label) Point
- func (t *Tree) RayCastSingleIgnorePiercing(x, y, degrees, length float64, pierceCount int, invalidIDS []event.CID, ...) Point
- func (t *Tree) RayCastSingleLabels(x, y, degrees, length float64, labels ...Label) Point
- func (t *Tree) Remove(sps ...*Space) int
- func (t *Tree) ShiftSpace(x, y float64, s *Space) error
- func (t *Tree) UpdateSpace(x, y, w, h float64, 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.
func ConeCast ¶
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 RayCast ¶
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
RayCastLabelsPiercing disregards "pierceCount" first results of RayCastSingleLabels
func RayCastSingle ¶
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 ¶
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 ¶
RayCastSingleLabels acts like RayCastSingle, but only returns elements that match one of the input labels
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 ¶
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 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 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) UpdateLabel ¶
UpdateLabel changes the label behind this space and resets it in the legacy 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. 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 (*Tree) ConeCast ¶
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 ¶
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) RayCast ¶
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 ¶
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 ¶
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 ¶
RayCastSingleLabels acts like RayCastSingle, but only returns elements that match one of the input labels
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