๐ค Artificial Intelligence
Game AI refers to the techniques used to make NPCs (non-player characters) and other game entities behave in believable and engaging ways. Unlike research AI (machine learning, neural networks), game AI is typically focused on creating the illusion of intelligence efficiently and predictably.
Behavior Patterns
Finite State Machines (FSM)
A Finite State Machine is one of the simplest and most widely-used patterns in game AI. An entity has a set of discrete states (e.g., idle, patrol, chase, attack), and transitions between them triggered by conditions (e.g., โplayer spottedโ transitions from patrol to chase). FSMs are easy to implement, debug, and reason about โ but can become unwieldy with many states and complex transitions.
Behavior Trees
Behavior trees are a more scalable alternative to FSMs for complex AI. They organize actions into a tree of nodes: selectors (try each child until one succeeds), sequences (run children in order), decorators (modify child behavior), and leaf nodes (actual actions).
Steering Behaviors
Steering behaviors describe how autonomous agents move through a scene โ seeking a target, fleeing from a threat, wandering randomly, or flocking with a group. Individual behaviors can be combined to produce complex emergent movement. Craig Reynoldsโ original paper is the seminal resource on the topic.
Pathfinding
Pathfinding determines how an agent navigates from point A to point B while avoiding obstacles.
Algorithms
- A* (A-star) is the most commonly used pathfinding algorithm in games. It finds the shortest path between two nodes on a graph by combining an actual cost function with a heuristic estimate of the remaining distance to the goal.
- Dijkstra is similar to A* but without the heuristic. It explores more nodes and is slower, but is guaranteed to find the optimal path in any graph.
Navigation Meshes
For 3D games, grid-based pathfinding is rarely practical. Instead, a navigation mesh (navmesh) defines the walkable surfaces of your scene as a polygon mesh. Agents pathfind across the navmesh, which is far more memory-efficient than a voxel grid and handles complex geometry naturally. Recast & Detour is the industry-standard C++ navmesh toolkit (used internally by Unity, Unreal Engine, and Godot). Several JavaScript ports and other options exist (see Resources below).
Resources
- Navcat
- Yuka
- react-three-npc by Supun Sethsara
- A* introduction by Red Blob Games
- three-pathfinding
- easystarjs
- ngraph.path
- PathFinding.js
- recastjs (recast-detour on NPM)
- recast-detour
- Yuka route planning and collision avoidance
- AI and Games YouTube channel
- The Total Beginnerโs Guide to Game AI
- Game AI Pro