| 251 | } |
| 252 | |
| 253 | func (qt *quadtreeNode) Insert(item *quadtreeNodeData) { |
| 254 | if qt.hasNodes { |
| 255 | index := qt.getIndex(item.AABB) |
| 256 | |
| 257 | if index != -1 { |
| 258 | qt.Nodes[index].Insert(item) |
| 259 | return |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // If we don't subnodes within the Quadtree |
| 264 | qt.Objects = append(qt.Objects, item) |
| 265 | |
| 266 | // If total objects is greater than max objects and level is less than max levels |
| 267 | if (len(qt.Objects) > qt.Tree.MaxObjects) && (qt.Tree.MaxLevels <= 0 || qt.Level < qt.Tree.MaxLevels) { |
| 268 | // split if we don't already have subnodes |
| 269 | if !qt.hasNodes { |
| 270 | qt.split() |
| 271 | } |
| 272 | |
| 273 | // Add all objects to there corresponding subNodes |
| 274 | for i := 0; i < len(qt.Objects); { |
| 275 | object := qt.Objects[i] // Get the object out of the slice |
| 276 | bounds := object.AABB |
| 277 | index := qt.getIndex(bounds) |
| 278 | if index != -1 { |
| 279 | qt.Objects = append(qt.Objects[:i], qt.Objects[i+1:]...) // Remove the object from the slice |
| 280 | qt.Nodes[index].Insert(object) |
| 281 | } else { |
| 282 | i++ |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | func (qt *quadtreeNode) Remove(item AABBer, pRect AABB) { |
| 289 | if qt.hasNodes { |