Documentation
¶
Overview ¶
Copyright 2019 Tskken. All rights reserved. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.
Package quadgo provides a basic quad-tree implementation for game collision detection. It provides most standard read and write operations for a quad-tree. The main uses cases would be using quadgo with its provided quadgo.IsIntersect() functions to check for intersects with in your game space. You would insert objects (quadgo.Entity) types with ether quadgo.Insert(), quadgo.InsertWithAction(), or quadgo.InsertEntities().
Note that all read operations with in this library are run concurrently but not safe with write operations. No mutex locks are provided with in this library so if you want to make safe writes and reads concurrently, you would have to set up your own mutex lock functions.
Index ¶
- type Action
- type Bound
- type Entities
- type Entity
- type Option
- type Point
- type QuadGo
- func (q *QuadGo) Insert(minX, minY, maxX, maxY float64)
- func (q *QuadGo) InsertEntities(entities ...*Entity) error
- func (q *QuadGo) InsertWithAction(minX, minY, maxX, maxY float64, action Action)
- func (q *QuadGo) Intersects(bound Bound) <-chan Entities
- func (q *QuadGo) IsEntity(entity *Entity) <-chan bool
- func (q *QuadGo) IsIntersect(bound Bound) <-chan bool
- func (q *QuadGo) Remove(entity *Entity) error
- func (q *QuadGo) Retrieve(bound Bound) <-chan Entities
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Action ¶
type Action func()
Action is a function type that can be given to an entity to be executed later.
type Bound ¶
type Bound struct {
Min, Max, Center Point
}
Bound is the basic rectangular bounds for nodes and entities in QuadGo.
func NewBound ¶
NewBound creates a new Bound struct from the given min and max points.
Note: QuadGo format follows the standard 0,0 as the top left of the screen.
func (Bound) IsEqual ¶
IsEqual checks if the given bound is equal to this bound.
Only checks min and max points as center is based off those points and checking it would be redundant.
func (Bound) IsIntersect ¶
IsIntersect returns whether or not the given Bound intersects with this bound.
type Entities ¶
type Entities []*Entity
Entities is a list of Entity's.
type Entity ¶
Entity is the Entity structure type for QuadGo.
Entity holds the Bound information for an entity in the tree and an Action function as a closer style function type which can store a function to use later. Entity also holds an ID which is by default a random uint64 value that is used to be able to accurately compare entities with IsEntity()
func NewEntity ¶
NewEntity creates a new entity from the given min and max points.
The ID for any given entity created will be default set to a random uint64 value seeded at creation time with time.Now().UnixNano(). If you want to set an ID you self just change the ID after creation.
func NewEntityWithAction ¶
NewEntityWithAction creates a new entity with the given min and max x and y positions of its bounds along with an Action function.
Example:
quadgo.NewEntityWithAction(0, 0, 50, 50, func(){
fmt.Println("hello from an action")
})
type Option ¶
type Option func(*options)
Option function type for setting the options of a new tree.
func SetMaxDepth ¶
SetMaxDepth sets the max depth that the tree can split to.
func SetMaxEntities ¶
SetMaxEntities sets the max number of entities per each node in the new tree.
type Point ¶
type Point struct {
X, Y float64
}
Point is the basic 2D coordinate structure for QuadGo
type QuadGo ¶
type QuadGo struct {
// contains filtered or unexported fields
}
QuadGo - Base quad-tree data structure.
func New ¶
New creates the basic QuadGo instance.
New requires a width and a height but can also be given any number of other supported Option functions.
Example:
basic - quadgo.New(800, 600) with option - quadgo.New(800, 600, SetMaxDepth(5))
QuadGo sets the New defaults for max depth to 5 and max entities to 10.
func (*QuadGo) Insert ¶
Insert takes the desired min and max xy points for the inserted entity.
Insert will insert the entity for the given bounds in to all leaf nodes that the given bounds intersects with. This can mean duplicate references if the given bound is large and can intersect many leaf nodes. These are Entity references which help save on memory use but be aware if you insert large objects it can hinder performance.
func (*QuadGo) InsertEntities ¶
InsertEntities inserts any number of entities in the quad-tree.
This will return an error if you do not give it any entities.
func (*QuadGo) InsertWithAction ¶
InsertWithAction takes the desired min and max xy points for the inserted entity and an Action function.
func (*QuadGo) Intersects ¶
Intersects takes a bound and returns all entities that the given bound intersects with. If no entities were found it will return an empty list of Entities.
The return of this function is a <-channel of Entities. This is due to the fact that all reads are run concurrently. If You want to just wait for this function to return and block the hole time you can just call it with `entities := <-quadgo.Intersects(bound)`. This will block until entities are returned on the chan.
If you want to run intersects and then do actions before retrieving the data from the chan you can just save the chan with `out := quadgo.Intersects(bound)`. you can then later use Go's `entities := <-out` to block until the value is returned from intersects.
func (*QuadGo) IsEntity ¶
IsEntity checks if a given entity exists within the tree. The given entity has to be an exact value of the entity you want to find. This means it has to have the same ID and Bounds. Action functions are not compared.
The return of this function is a <-channel of bool. This is due to the fact that all reads are run concurrently. If You want to just wait for this function to return and block the hole time you can just call it with `is := <-quadgo.IsEntity(entity)`. This will block until a value is returned on the chan.
If you want to run IsEntity() and then do actions before retrieving the data from the chan you can just save the chan with `out := quadgo.IsEntity(entity)`. you can then later use Go's `is := <-out` to block until the value is returned from isEntity.
func (*QuadGo) IsIntersect ¶
IsIntersect take a bound and returns if that bound intersects any entity within the tree.
The return of this function is a <-channel of bool. This is due to the fact that all reads are run concurrently. If You want to just wait for this function to return and block the hole time you can just call it with `is := <-quadgo.IsIntersect(bound)`. This will block until a value is returned on the chan.
If you want to run isIntersect and then do actions before retrieving the data from the chan you can just save the chan with `out := quadgo.IsIntersect(bound)`. you can then later use Go's `is := <-out` to block until the value is returned from isIntersect.
func (*QuadGo) Remove ¶
Remove removes the given Entity from the quad-tree.
The given entity has to be the exact same as the one you want to delete. This function uses the Entities ID and a comparison with its Bounds to confirm that the found entity is in fact the entity to remove.
This will return an error if the entity given was not found in the quad-tree.
func (*QuadGo) Retrieve ¶
Retrieve returns all entities from all nodes the given bounds intersects with. Retrieve excludes duplected entities.
The return of this function is a <-channel of entities. This is due to the fact that all reads are run concurrently. If You want to just wait for this function to return and block the hole time you can just call it with `entities := <-quadgo.Retrieve(bound)`. This will block until values are returned on the Entities chan.
If you want to run retrieve and then do actions before retrieving the data from the Entities chan you can just save the chan with `out := quadgo.Retrieve(bound)`. You can then later use Go's `entities := <- out` to block till the entities are returned from retrieve.