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

# GKObstacleGraph

A navigation graph for 2D game worlds that creates a minimal network for precise pathfinding around obstacles.

```
class GKObstacleGraph<NodeType> where NodeType : GKGraphNode2D
```

## Overview

You create an obstacle graph with a collection of [`GKObstacle`](/documentation/GameplayKit/GKObstacle) objects. To use the graph for pathfinding, you add [`GKGraphNode2D`](/documentation/GameplayKit/GKGraphNode2D) objects representing points of interest (such as the current position of a game character and the location it needs to find a route to). Then use methods of the superclass [`GKGraph`](/documentation/GameplayKit/GKGraph) to find routes through the graph.

Unlike the related [`GKMeshGraph`](/documentation/GameplayKit/GKMeshGraph) class, an obstacle graph creates a minimal network of graph nodes, resulting in paths that are efficient but not smooth.

To learn more about graphs and pathfinding, see [Pathfinding](https://developer.apple.com/library/archive/documentation/General/Conceptual/GameplayKit_Guide/Pathfinding.html#//apple_ref/doc/uid/TP40015172-CH3) in [GameplayKit Programming Guide](https://developer.apple.com/library/archive/documentation/General/Conceptual/GameplayKit_Guide/index.html#//apple_ref/doc/uid/TP40015172).

## Topics

### Creating a Graph

[`-  initWithObstacles:bufferRadius:nodeClass:`](/documentation/GameplayKit/GKObstacleGraph/init(obstacles:bufferRadius:nodeClass:))

Initializes a graph with the specified list of obstacles, using the specified node class.

[`+  graphWithObstacles:bufferRadius:nodeClass:`](/documentation/GameplayKit/GKObstacleGraph/graphWithObstacles:bufferRadius:nodeClass:)

Creates a graph with the specified list of obstacles, using the specified node class.

[`-  initWithObstacles:bufferRadius:`](/documentation/GameplayKit/GKObstacleGraph/init(obstacles:bufferRadius:))

Initializes a graph with the specified list of obstacles.

[`+  graphWithObstacles:bufferRadius:`](/documentation/GameplayKit/GKObstacleGraph/graphWithObstacles:bufferRadius:)

Creates a graph with the specified list of obstacles.

### Working with Obstacles

[`obstacles`](/documentation/GameplayKit/GKObstacleGraph/obstacles)

The list of obstacle objects in the graph, each of which describes a polygon-shaped impassable area.

[`-  addObstacles:`](/documentation/GameplayKit/GKObstacleGraph/addObstacles(_:))

Adds new obstacles to the graph.

[`-  removeObstacles:`](/documentation/GameplayKit/GKObstacleGraph/removeObstacles(_:))

Removes the specified obstacle from the graph.

[`-  removeAllObstacles`](/documentation/GameplayKit/GKObstacleGraph/removeAllObstacles())

Removes all obstacles from the graph.

[`-  nodesForObstacle:`](/documentation/GameplayKit/GKObstacleGraph/nodes(for:))

Returns the group of nodes corresponding to an obstacle in the graph.

### Working with Nodes

[`-  connectNodeUsingObstacles:`](/documentation/GameplayKit/GKObstacleGraph/connectUsingObstacles(node:))

Adds the specified node to the graph, connecting it to its nearest neighbors without creating connections that pass through obstacles or their buffer regions.

[`-  connectNodeUsingObstacles:ignoringObstacles:`](/documentation/GameplayKit/GKObstacleGraph/connectUsingObstacles(node:ignoring:))

Adds the specified node to the graph, connecting it to its nearest neighbors while ignoring the area occupied by the specified obstacles.

[`-  connectNodeUsingObstacles:ignoringBufferRadiusOfObstacles:`](/documentation/GameplayKit/GKObstacleGraph/connectUsingObstacles(node:ignoringBufferRadiusOf:))

Adds the specified node to the graph, connecting it to its nearest neighbors while ignoring the buffer regions around the specified obstacles.

[`bufferRadius`](/documentation/GameplayKit/GKObstacleGraph/bufferRadius)

The distance from obstacle edges that should also be considered impassable.

### Locking Node Connections

[`-  lockConnectionFromNode:toNode:`](/documentation/GameplayKit/GKObstacleGraph/lockConnection(from:to:))

Prevents the specified nodes from being disconnected due to the addition of obstacles.

[`-  unlockConnectionFromNode:toNode:`](/documentation/GameplayKit/GKObstacleGraph/unlockConnection(from:to:))

Allows the specified nodes to be disconnected due to the addition of obstacles.

[`-  isConnectionLockedFromNode:toNode:`](/documentation/GameplayKit/GKObstacleGraph/isConnectionLocked(from:to:))

Returns a Boolean value indicating whether the specified nodes are protected from disconnection due to the addition of obstacles.

### Instance Methods

[`-  classForGenericArgumentAtIndex:`](/documentation/GameplayKit/GKObstacleGraph/classForGenericArgument(at:))

## Relationships

### Inherits From

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

### Conforms To

[`NSCoding`](/documentation/Foundation/NSCoding)

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

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

[`NSCopying`](/documentation/Foundation/NSCopying)

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

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

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

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

[`NSSecureCoding`](/documentation/Foundation/NSSecureCoding)

---

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)