3D Pathfinding for Flying and Swimming in Games

Navigating three-dimensional environments presents a unique challenge in game development, as flying and swimming entities are not bound to a flat, two-dimensional ground plane. To handle this freedom of movement, pathfinding systems must transition from traditional 2D navigation meshes to volumetric spatial representation and dynamic flight calculations. This article explores how developers adapt pathfinding for 3D environments using spatial partitioning, modified search algorithms, and steering behaviors to create realistic movement for aerial and aquatic creatures.

Volumetric Spatial Partitioning

Traditional pathfinding relies on a 2D Navigation Mesh (NavMesh) walked on by ground characters. For 3D entities, developers must represent the entire volume of open space. The most common way to achieve this is through spatial partitioning techniques that divide 3D space into searchable nodes.

3D Pathfinding Algorithms

Once the 3D space is partitioned, pathfinding algorithms must navigate the node network. The standard A* (A-Star) algorithm is easily adapted to 3D by adding a Z-axis to its heuristic distance calculations (typically using 3D Euclidean distance).

However, basic 3D A* paths can look jagged and unnatural for flying or swimming creatures. To solve this, developers use Theta* (Theta-Star) or Lazy Theta*. These “any-angle” pathfinding algorithms perform line-of-sight checks between non-adjacent nodes. If a straight line of sight exists through open 3D space, the algorithm skips intermediate nodes, resulting in smooth, direct, and natural-looking flight paths.

Dynamic Local Avoidance and Steering

Global pathfinding calculates the rough route from point A to point B, but flying and swimming entities must also react dynamically to moving obstacles, other agents, and sudden changes in the environment.

By combining volumetric Octrees for global path planning, any-angle 3D algorithms for smooth routing, and real-time steering forces for local avoidance, game developers can create highly convincing and performant navigation systems for any entity that flies through the air or swims through the deep.