Experimenting with different collision resolutions.
This commit is contained in:
@@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
QuadtreeMaxColliders = 10
|
||||
QuadtreeMaxColliders = 40
|
||||
)
|
||||
|
||||
type Quadrant struct {
|
||||
@@ -31,25 +31,27 @@ func (q *Quadtree) Insert(obj colliders.Collider) bool {
|
||||
|
||||
var result bool = false
|
||||
//check that object meets containment criteria
|
||||
if q.ContainsPoint(obj.GetPosition()) {
|
||||
|
||||
//if we have children, we go deeper
|
||||
if len(q.children) > 0 {
|
||||
for _, node := range q.children {
|
||||
if node.Insert(obj) {
|
||||
result = true
|
||||
break
|
||||
}
|
||||
}
|
||||
} else if len(q.colliders) < QuadtreeMaxColliders {
|
||||
//otherwise, if we have space, add
|
||||
q.colliders = append(q.colliders, obj)
|
||||
result = true
|
||||
} else {
|
||||
//otherwise we have to subdivide, reorganize, and add
|
||||
result = q.SubdivideAndInsert(obj)
|
||||
}
|
||||
if !q.ContainsPoint(obj.GetPosition()) {
|
||||
return result
|
||||
}
|
||||
|
||||
//if we have children, we go deeper
|
||||
if len(q.children) > 0 {
|
||||
for _, node := range q.children {
|
||||
if node.Insert(obj) {
|
||||
result = true
|
||||
break
|
||||
}
|
||||
}
|
||||
} else if len(q.colliders) < QuadtreeMaxColliders {
|
||||
//otherwise, if we have space, add
|
||||
q.colliders = append(q.colliders, obj)
|
||||
result = true
|
||||
} else {
|
||||
//otherwise we have to subdivide, reorganize, and add
|
||||
result = q.SubdivideAndInsert(obj)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -67,26 +69,27 @@ func (q *Quadtree) ContainsPoint(p gamedata.Vector) bool {
|
||||
func (q *Quadtree) Remove(obj colliders.Collider) bool {
|
||||
var result bool = false
|
||||
|
||||
if q.ContainsPoint(obj.GetPosition()) {
|
||||
if !q.ContainsPoint(obj.GetPosition()) {
|
||||
return result
|
||||
}
|
||||
|
||||
if len(q.colliders) > 0 {
|
||||
//examine existing colliders
|
||||
var collection []colliders.Collider
|
||||
for _, node := range q.colliders {
|
||||
if node == obj {
|
||||
result = true
|
||||
} else {
|
||||
collection = append(collection, node)
|
||||
}
|
||||
if len(q.colliders) > 0 {
|
||||
//examine existing colliders
|
||||
var collection []colliders.Collider
|
||||
for _, node := range q.colliders {
|
||||
if node == obj {
|
||||
result = true
|
||||
} else {
|
||||
collection = append(collection, node)
|
||||
}
|
||||
q.colliders = collection
|
||||
} else {
|
||||
//need to check children
|
||||
for _, child := range q.children {
|
||||
if child.Remove(obj) {
|
||||
result = true
|
||||
break
|
||||
}
|
||||
}
|
||||
q.colliders = collection
|
||||
} else {
|
||||
//need to check children
|
||||
for _, child := range q.children {
|
||||
if child.Remove(obj) {
|
||||
result = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user