little clean up, romving redundant pixel package import

This commit is contained in:
unknown 2020-07-02 11:21:02 +02:00
parent 9ffe982efb
commit ebb947b217
2 changed files with 35 additions and 38 deletions

View File

@ -170,7 +170,7 @@ func (u Vec) Normal() Vec {
} }
// Returns angle between two vectors // Returns angle between two vectors
func (u Vec) AngleTo(v Vec) { func (u Vec) AngleTo(v Vec) float64 {
u, v = u.Unit(), v.Unit() u, v = u.Unit(), v.Unit()
return math.Acos(u.Dot(v)) return math.Acos(u.Dot(v))
} }

View File

@ -1,11 +1,7 @@
package pixel package pixel
import (
"github.com/faiface/pixel"
)
type Collidable interface { type Collidable interface {
GetRect() *pixel.Rect GetRect() *Rect
} }
// i separated this data so i can simply copy it to subnodes // i separated this data so i can simply copy it to subnodes
@ -16,9 +12,9 @@ type Common struct {
} }
type Quadtree struct { type Quadtree struct {
pixel.Rect Rect
Nodes []*Quadtree nodes []*Quadtree
Shapes []Collidable shapes []Collidable
Common Common
} }
@ -30,7 +26,7 @@ type Quadtree struct {
// if shapes cannot fit into smallest quadrants. // if shapes cannot fit into smallest quadrants.
// cap - sets maximal capacity of quadrant before it splits to 4 smaller. Making can too big is // cap - sets maximal capacity of quadrant before it splits to 4 smaller. Making can too big is
// inefficient. optimal value can be 10 but its allways better to test what works the best. // inefficient. optimal value can be 10 but its allways better to test what works the best.
func NewQuadtree(bounds pixel.Rect, depth, cap int) *Quadtree { func NewQuadtree(bounds Rect, depth, cap int) *Quadtree {
return &Quadtree{ return &Quadtree{
Rect: bounds, Rect: bounds,
Common: Common{ Common: Common{
@ -42,41 +38,41 @@ func NewQuadtree(bounds pixel.Rect, depth, cap int) *Quadtree {
//generates subquadrants //generates subquadrants
func (q *Quadtree) split() { func (q *Quadtree) split() {
q.Nodes = make([]*Quadtree, 4) q.nodes = make([]*Quadtree, 4)
newCommon := q.Common newCommon := q.Common
newCommon.Level++ newCommon.level++
halfH := q.H() / 2 halfH := q.H() / 2
halfW := q.W() / 2 halfW := q.W() / 2
center := q.Center() center := q.Center()
//top-left //top-left
q.Nodes[0] = &Quadtree{ q.nodes[0] = &Quadtree{
Rect: pixel.Rect{ Rect: Rect{
Min: pixel.V(q.Min.X, q.Min.Y+halfH), Min: V(q.Min.X, q.Min.Y+halfH),
Max: pixel.V(q.Max.X-halfW, q.Max.Y), Max: V(q.Max.X-halfW, q.Max.Y),
}, },
Common: newCommon, Common: newCommon,
} }
//top-right //top-right
q.Nodes[1] = &Quadtree{ q.nodes[1] = &Quadtree{
Rect: pixel.Rect{ Rect: Rect{
Min: center, Min: center,
Max: q.Max, Max: q.Max,
}, },
Common: newCommon, Common: newCommon,
} }
//bottom-left //bottom-left
q.Nodes[2] = &Quadtree{ q.nodes[2] = &Quadtree{
Rect: pixel.Rect{ Rect: Rect{
Min: q.Min, Min: q.Min,
Max: center, Max: center,
}, },
Common: newCommon, Common: newCommon,
} }
//bottom-right //bottom-right
q.Nodes[3] = &Quadtree{ q.nodes[3] = &Quadtree{
Rect: pixel.Rect{ Rect: Rect{
Min: pixel.V(q.Min.X+halfW, q.Min.Y), Min: V(q.Min.X+halfW, q.Min.Y),
Max: pixel.V(q.Max.X, q.Min.Y+halfH), Max: V(q.Max.X, q.Min.Y+halfH),
}, },
Common: newCommon, Common: newCommon,
} }
@ -84,7 +80,7 @@ func (q *Quadtree) split() {
// finds out to witch subquadrant the shape belongs to. Shape has to overlap only with one quadrant, // finds out to witch subquadrant the shape belongs to. Shape has to overlap only with one quadrant,
// otherwise it returns -1 // otherwise it returns -1
func (q *Quadtree) getSub(rect *pixel.Rect) int8 { func (q *Quadtree) getSub(rect *Rect) int8 {
vertical := q.Min.X + q.W()/2 vertical := q.Min.X + q.W()/2
horizontal := q.Min.Y + q.H()/2 horizontal := q.Min.Y + q.H()/2
@ -118,40 +114,41 @@ func (q *Quadtree) getSub(rect *pixel.Rect) int8 {
// GetRect() *pixel.Rect defined. GetRect function also slightly affects performance. // GetRect() *pixel.Rect defined. GetRect function also slightly affects performance.
func (q *Quadtree) Insert(collidable Collidable) { func (q *Quadtree) Insert(collidable Collidable) {
rect := collidable.GetRect() rect := collidable.GetRect()
q.Shapes = append(q.Shapes, collidable) // this is little memory expensive but it makes acsesing shapes faster
if len(q.Nodes) != 0 { q.shapes = append(q.shapes, collidable)
if len(q.nodes) != 0 {
i := q.getSub(rect) i := q.getSub(rect)
if i != -1 { if i != -1 {
q.Nodes[i].Insert(collidable) q.nodes[i].Insert(collidable)
} }
return return
} else if q.Cap == len(q.Shapes) && q.Level != q.Depth { } else if q.Cap == len(q.shapes) && q.level != q.Depth {
q.split() q.split()
for _, s := range q.Shapes { for _, s := range q.shapes {
i := q.getSub(s.GetRect()) i := q.getSub(s.GetRect())
if i != -1 { if i != -1 {
q.Nodes[i].Insert(s) q.nodes[i].Insert(s)
} }
} }
} }
} }
// gets smallest generated quadrant that rect fits into // gets smallest generated quadrant that rect fits into
func (q *Quadtree) getQuad(rect *pixel.Rect) *Quadtree { func (q *Quadtree) getQuad(rect *Rect) *Quadtree {
if len(q.Nodes) == 0 { if len(q.nodes) == 0 {
return q return q
} }
subIdx := q.getSub(rect) subIdx := q.getSub(rect)
if subIdx == -1 { if subIdx == -1 {
return q return q
} }
return q.Nodes[subIdx].getQuad(rect) return q.nodes[subIdx].getQuad(rect)
} }
// returns all collidables that this rect can possibly collide with // returns all collidables that this rect can possibly collide with
// thought it also returns the shape it self if it wos inserted // thought it also returns the shape it self if it wos inserted
func (q *Quadtree) Retrieve(rect *pixel.Rect) []Collidable { func (q *Quadtree) Retrieve(rect *Rect) []Collidable {
return q.getQuad(rect).Shapes return q.getQuad(rect).shapes
} }
// returns all coliding shapes // returns all coliding shapes
@ -169,6 +166,6 @@ func (q *Quadtree) GetColliding(collidable Collidable) []Collidable {
// Resets the tree, use this every frame before inserting all shapes // Resets 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
func (q *Quadtree) Clear() { func (q *Quadtree) Clear() {
q.Shapes = []Collidable{} q.shapes = []Collidable{}
q.Nodes = []*Quadtree{} q.nodes = []*Quadtree{}
} }