adding more comments

This commit is contained in:
unknown 2020-07-12 12:27:02 +02:00
parent ab6c5526ed
commit c24fe1e37c
1 changed files with 13 additions and 4 deletions

View File

@ -4,16 +4,26 @@ import (
"errors" "errors"
) )
// Quadtree entry interface
type Collidable interface { type Collidable interface {
GetRect() Rect GetRect() Rect
} }
// Common part of quadtree. Commot is always coppied to children
// with change of the level
type Common struct { type Common struct {
Depth int Depth int
Level int Level int
Cap int //max amount of objects per quadrant, if there is more quadrant splits Cap int //max amount of objects per quadrant, if there is more quadrant splits
} }
// Quadtree is is datastructure used for effective collision detection.
// In most cases you really only need to interact with root node.
// There are to ways to use quadtree. If objects used in quadtree hes very
// short live spam it is better to clear tree and reinset objects every frame.
// On the other hand, if objects are rather permanent use update approach.
// insert every shape just once and remove it if needed. Use Update method before
// detecting collisions or removing shapes.
type Quadtree struct { type Quadtree struct {
Rect Rect
tl, tr, bl, br, pr *Quadtree tl, tr, bl, br, pr *Quadtree
@ -82,6 +92,7 @@ func (q *Quadtree) split() {
} }
} }
// returns weather shape fits into quadtree completely
func (q *Quadtree) fits(rect Rect) bool { func (q *Quadtree) fits(rect Rect) bool {
return rect.Max.X > q.Min.X && rect.Max.X < q.Max.X && rect.Min.Y > q.Min.Y && rect.Max.Y < q.Max.Y return rect.Max.X > q.Min.X && rect.Max.X < q.Max.X && rect.Min.Y > q.Min.Y && rect.Max.Y < q.Max.Y
} }
@ -114,10 +125,8 @@ func (q *Quadtree) getSub(rect Rect) *Quadtree {
return nil return nil
} }
// Adds the shape to quad tree and asians it to correct quadrant. // Adds the shape to quad tree and assigns it to correct quadrant.
// Proper way is adding all shapes first and then detecting collisions. // Proper way is adding all shapes first and then detecting collisions.
// For struct to implement Collidable interface it has to implement
// GetRect() *pixel.Rect. GetRect function also slightly affects performance.
func (q *Quadtree) Insert(collidable Collidable) { func (q *Quadtree) Insert(collidable Collidable) {
rect := collidable.GetRect() rect := collidable.GetRect()
@ -241,7 +250,7 @@ func (q *Quadtree) Remove(c Collidable) error {
return nil return nil
} }
} }
return errors.New("Shape wos not found. Update before removing.") return errors.New("Shape not found. Update before removing.")
} }
// Resets the tree, use this every frame before inserting all shapes // Resets the tree, use this every frame before inserting all shapes