switching to simpler and more perforamnt aproach of removing shapes from quadtree
This commit is contained in:
parent
18b0395578
commit
b35f160475
|
@ -1,14 +1,13 @@
|
||||||
package quadtree
|
package quadtree
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
|
|
||||||
"github.com/faiface/pixel"
|
"github.com/faiface/pixel"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Collidable is interface that stores inserted objects
|
// Collidable is interface that stores inserted objects
|
||||||
type Collidable interface {
|
type Collidable interface {
|
||||||
GetRect() pixel.Rect
|
GetRect() pixel.Rect
|
||||||
|
IsDead() bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Common part of quadtree. Commot is always coppied to children
|
// Common part of quadtree. Commot is always coppied to children
|
||||||
|
@ -164,6 +163,9 @@ func (q *Quadtree) Update() {
|
||||||
q.bl.Update()
|
q.bl.Update()
|
||||||
q.br.Update()
|
q.br.Update()
|
||||||
for _, c := range q.Shapes {
|
for _, c := range q.Shapes {
|
||||||
|
if c.IsDead() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
rect := c.GetRect()
|
rect := c.GetRect()
|
||||||
sub := q.getSub(rect)
|
sub := q.getSub(rect)
|
||||||
if sub != nil {
|
if sub != nil {
|
||||||
|
@ -176,6 +178,9 @@ func (q *Quadtree) Update() {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for _, c := range q.Shapes {
|
for _, c := range q.Shapes {
|
||||||
|
if c.IsDead() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if q.fits(c.GetRect()) || q.pr == nil {
|
if q.fits(c.GetRect()) || q.pr == nil {
|
||||||
new = append(new, c)
|
new = append(new, c)
|
||||||
} else {
|
} else {
|
||||||
|
@ -211,35 +216,6 @@ func (q *Quadtree) GetColliding(rect pixel.Rect, con *[]Collidable) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// gets a smallest possible quadrant rect fits into.
|
|
||||||
func (q *Quadtree) getSmallestQuad(rect pixel.Rect) *Quadtree {
|
|
||||||
current := q
|
|
||||||
for {
|
|
||||||
sub := current.getSub(rect)
|
|
||||||
if sub == nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
current = sub
|
|
||||||
}
|
|
||||||
return current
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove removes shape from quadtree the fast wey. Always update before removing objects
|
|
||||||
// unless you are not ,moving with it.
|
|
||||||
func (q *Quadtree) Remove(c Collidable) error {
|
|
||||||
sq := q.getSmallestQuad(c.GetRect())
|
|
||||||
for i, o := range sq.Shapes {
|
|
||||||
if o == c {
|
|
||||||
last := len(sq.Shapes) - 1
|
|
||||||
sq.Shapes[i] = nil
|
|
||||||
sq.Shapes[i] = sq.Shapes[last]
|
|
||||||
sq.Shapes = sq.Shapes[:last]
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return errors.New("shape not found, update before removing")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clear clears the tree, use this every frame before inserting all shapes
|
// Clear clears the tree, use this every frame before inserting all shapes
|
||||||
// other wise you will run out of memory eventually and tree will not even work properly.
|
// other wise you will run out of memory eventually and tree will not even work properly.
|
||||||
// You should not use this if you are using Upsate() nethod
|
// You should not use this if you are using Upsate() nethod
|
||||||
|
|
|
@ -31,6 +31,10 @@ func (c *collider) GetRect() pixel.Rect {
|
||||||
return pixel.R(c.X-c.size, c.Y-c.size, c.X+c.size, c.Y+c.size)
|
return pixel.R(c.X-c.size, c.Y-c.size, c.X+c.size, c.Y+c.size)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *collider) IsDead() bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
type collizionTest struct {
|
type collizionTest struct {
|
||||||
description string
|
description string
|
||||||
target collider
|
target collider
|
||||||
|
|
Loading…
Reference in New Issue