<!--
{
  "availability" : [
    "iOS: 10.0.0 -",
    "iPadOS: 10.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.12.0 -",
    "tvOS: 10.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "GameplayKit",
  "identifier" : "/documentation/GameplayKit/GKOctree",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "GameplayKit"
    ],
    "preciseIdentifier" : "c:objc(cs)GKOctree"
  },
  "title" : "GKOctree"
}
-->

# GKOctree

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

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

## 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](/documentation/gameplaykit/gkoctree#1965708). When an octant contains more than one object, the tree subdivides that region into eight smaller octants, adding a level to the tree.

![](images/com.apple.gameplaykit/media-1965708@2x.png)

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`](/documentation/GameplayKit/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`](/documentation/GameplayKit/GKRTree) class for different ways to organize two-dimensional data.

## Topics

### Creating an Octree

[`-  initWithBoundingBox:minimumCellSize:`](/documentation/GameplayKit/GKOctree/init(boundingBox:minimumCellSize:))

Initializes an octree with the specified dimensions.

[`+  octreeWithBoundingBox:minimumCellSize:`](/documentation/GameplayKit/GKOctree/octreeWithBoundingBox:minimumCellSize:)

Creates an octree with the specified dimensions.

### Adding and Removing Elements

[`-  addElement:withPoint:`](/documentation/GameplayKit/GKOctree/add(_:at:))

Adds an object to the tree corresponding to the specified point in 3D space.

[`-  addElement:withBox:`](/documentation/GameplayKit/GKOctree/add(_:in:))

Adds an object to the tree corresponding to the specified volume of 3D space.

[`-  removeElement:withNode:`](/documentation/GameplayKit/GKOctree/remove(_:using:))

Removes the specified object from the tree, using a reference to its containing node.

[`-  removeElement:`](/documentation/GameplayKit/GKOctree/remove(_:))

Searches for the specified object and removes it from the tree.

### Searching for Elements

[`-  elementsAtPoint:`](/documentation/GameplayKit/GKOctree/elements(at:))

Returns all objects whose corresponding locations overlap the specified point.

[`-  elementsInBox:`](/documentation/GameplayKit/GKOctree/elements(in:))

Returns all objects whose corresponding locations overlap the specified volume.

### Constants

[`GKBox`](/documentation/GameplayKit/GKBox)

The definition of an axis-aligned rectangular bounding volume addressed by the tree.

## Relationships

### Conforms To

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`Equatable`](/documentation/Swift/Equatable)

[`Hashable`](/documentation/Swift/Hashable)

[`CVarArg`](/documentation/Swift/CVarArg)

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)