---
title: GKOctree
framework: gameplaykit
role: symbol
role_heading: Class
path: gameplaykit/gkoctree
---

# GKOctree

A data structure for organizing objects based on their locations in a three-dimensional space.

## Declaration

```swift
class GKOctree<ElementType> where ElementType : NSObject
```

## Overview

Overview An octree manages its structure to optimize for spatial searches—unlike a basic data structure such as an array or dictionary, an octree can find all elements occupying a specific position or volume very quickly. The octree partitioning strategy divides space into eight octants at each level, as illustrated in Figure 1. When an octant contains more than one object, the tree subdivides that region into eight smaller octants, adding a level to the tree.

Octrees can be useful for many tasks in game design. For example: Deciding which game characters are close enough to each other for interaction Deciding which portions of a large game world need to be processed at a given time The GKOctree class is one of three spatial partitioning data structures that GameplayKit provides, and the only one suited to three-dimensional data. See the GKQuadTree class for the two-dimensional analogue of an octree, and the GKRTree class for different ways to organize two-dimensional data.

## Topics

### Creating an Octree

- [init(boundingBox:minimumCellSize:)](gameplaykit/gkoctree/init(boundingbox:minimumcellsize:).md)

### Adding and Removing Elements

- [add(_:at:)](gameplaykit/gkoctree/add(_:at:).md)
- [add(_:in:)](gameplaykit/gkoctree/add(_:in:).md)
- [remove(_:using:)](gameplaykit/gkoctree/remove(_:using:).md)
- [remove(_:)](gameplaykit/gkoctree/remove(_:).md)

### Searching for Elements

- [elements(at:)](gameplaykit/gkoctree/elements(at:).md)
- [elements(in:)](gameplaykit/gkoctree/elements(in:).md)

### Constants

- [GKBox](gameplaykit/gkbox.md)

## Relationships

### Inherits From

- [NSObject](objectivec/nsobject-swift.class.md)

### Conforms To

- [CVarArg](swift/cvararg.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [Equatable](swift/equatable.md)
- [Hashable](swift/hashable.md)
- [NSObjectProtocol](objectivec/nsobjectprotocol.md)

## See Also

### Spatial Partitioning

- [GKQuadtree](gameplaykit/gkquadtree.md)
- [GKQuadtreeNode](gameplaykit/gkquadtreenode.md)
- [GKOctreeNode](gameplaykit/gkoctreenode.md)
- [GKRTree](gameplaykit/gkrtree.md)
