COMP 3200 - Intro to Artificial Intelligence - Lecture 06 - Grid Representations and Vector Fields
Overview
Dave Churchill explains how grid representations trade spatial precision for simpler storage, fast lookups, and efficient pathfinding, using examples from StarCraft, Dragon Age, Unity navigation meshes, and game-world movement. He develops vector fields from breadth-first-search distance maps to route many entities toward one goal, then covers grid limitations, influence maps, and queue and visited-state optimizations.
Key takeaways
- A grid representation makes obstacle checks a constant-time array lookup, but smaller cells increase the number of pathfinding states quadratically in two dimensions.
- StarCraft II illustrates why one environment may need multiple spatial resolutions: 32-by-32-pixel build tiles, 8-by-8-pixel walk tiles, and pixel-level collision checks.
- For many entities moving to one destination, BFS can build a distance map once and each cell can point to a lower-distance neighbor, creating a reusable vector field.
- A distance-map value of -1 can serve both as an unreachable marker and as an unvisited marker during BFS, eliminating the need for a separate closed-list structure.
- Grid paths can support continuous-looking movement: convert cell paths to unobstructed waypoints, remove unnecessary turns, and apply steering or smoothing.
- Naive grids can be costly for large or sparse worlds; a 1,000-by-1,000 map has one million cells, so hierarchical or sparse representations may be preferable.
Chapters
- A space representation determines how locations and movement actions are modeled, and which pathfinding algorithms apply.
- A one-meter grid would be impractical for giving human-readable directions across Newfoundland; a graph of towns and roads is more suitable.
- The same grid techniques can apply to games, robotics, and simulations.
- Representation decisions involve localization, construction cost, memory, pathfinding time, and how easily obstacles or road closures can be updated.
- Arbitrary-precision positions allow flexible movement but can imply infinitely many possible actions, making search impractical without discretization.
- Grid movement usually limits actions to adjacent cells, while graph representations can connect meaningful locations such as towns.
- Convex polygons support hierarchical pathfinding: search between polygons first, then travel directly within a polygon because any two points inside are mutually visible.
- StarCraft II uses a triangular environment representation for pathfinding; some algorithms specifically require triangular meshes.
- Unity navigation meshes describe regions where characters may walk and can be represented as grids or more general graphs.
- A grid abstracts the world into equally sized cells that can be stored in a 2D array, simplifying visualization and computation.
- StarCraft uses different spatial resolutions for different tasks: 32-by-32-pixel build tiles, 8-by-8-pixel walk tiles, and pixel-level unit collision boxes.
- Grids can represent height-map terrain as well as flat surfaces, provided the environment has no overhangs.
- For equal-sized square cells, convert a world coordinate to a cell by dividing each coordinate by the grid-cell size and taking integer indices.
- Chess and checkers already define positions on a grid, while Civilization-style hex maps can still use 2D arrays internally.
- Grid shape determines available neighbors and actions; triangular and hexagonal grids do not use the same adjacency pattern as square grids.
- A task grid can encode whether actions such as walking or building are allowed at each location.
- Initialize cells as traversible, then mark cells overlapped by obstacles as blocked to create a constant-time lookup table.
- Smaller cells improve geometric accuracy but increase memory use and pathfinding work; halving cell width and height creates four times as many cells.
- Four-directional movement uses up, down, left, and right; octile movement also permits diagonals.
- The COMP 3200 assignments progress from four-directional pathfinding to eight-directional movement.
- A height-map grid can encode elevation per cell and restrict movement according to allowable height differences.
- A grid path can look robotic when an entity follows every cell transition as a sequence of 90-degree turns.
- The workflow is to rasterize obstacles, map start and goal positions to cells, select grid actions, and run a search such as BFS, DFS, or UCS.
- Convert the resulting path into unobstructed waypoints, skip reachable intermediate waypoints, and apply steering or smoothing for natural movement.
- Grids are easy to modify when obstacles change, work with graph-search algorithms, and store data contiguously in cache-friendly arrays.
- Grid abstraction loses localization precision and may misrepresent small gaps or complex geometry.
- A naive grid wastes memory on sparse spaces; a 1,000-by-1,000 map already contains one million cells, motivating hierarchical or sparse-grid methods.
- A vector field assigns a direction vector to each grid cell, telling an entity which way to move toward a goal.
- Flow fields are especially useful when many NPCs share one destination, such as zombies pursuing a player in Diablo-like games.
- Compute the field once per goal instead of running a separate A* search for every entity.
- Initialize distance-map cells to -1, set the goal cell to 0, and run BFS outward through traversible cells.
- Each newly reached cell receives its predecessor's distance plus one, giving the four-directional shortest-path distance to the goal.
- Cells left at -1 are unreachable, so an entity on one can be identified immediately as having no path to the destination.
- For each reachable cell, point toward an adjacent cell with a lower distance value; combine directions into a diagonal when allowed.
- An entity follows the field by looking up its current cell's vector and applying that direction to its position.
- Recompute the field when the goal changes, then share it among all entities traveling to that goal.
- Removing the first item from an array-backed queue can take O(n) time because every remaining element shifts left.
- Keep queued items in the array and advance a front index instead of deleting elements; the queue is empty when the front reaches the end.
- This avoids repeated shifting while using storage proportional to the number of items ever enqueued during the search.
- In a distance map, the initial -1 value doubles as an unvisited marker, so any nonnegative value means BFS has already reached the cell.
- This lets the distance grid serve as the closed list without allocating a second structure.
- For other grid searches, a Boolean 2D array can provide constant-time visited checks, although retrieving every visited state then requires scanning the grid.
- Influence maps modify movement costs to express preferences, such as avoiding a shoreline or steering clear of an enemy turret.
- Positive or negative influences can guide agents away from threats or toward defensive objectives, alongside ordinary pathfinding costs.
- Churchill's demo routes 500,000 particles using a shared vector field at about 76 frames per second; a 235,000-cell grid's computation takes about 0.4 milliseconds.
Transcript excerpt
0:00 lecture number six. So today we're going to be talking about uh grid space representations and something called vector fields which are a class of algorithms which are really good and possible um when you're working in grid space representations. And um the reason for this lecture is because I've gotten a number of questions over the years saying like okay we're we're doing like these grid examples in the class but most interesting problems aren't able to be simplified into grids probably. So why are we learning this if most problems can't actually be solved in this way? And it turns out that um a surprising amount of problems can actually be solved using grid algorithms. And this lecture is an attempt to clarify some of that and present you with some um some really cool algorithms that uh can do some neat
0:49 things when you do have a grid structure. So just a quick note is that I'm going to be talking a lot about games um in this lecture for uh examples. Um but that's because that's what I work with. I I like games. I think other people like games. It's more fun to talk about games than real world problems sometimes. Um but just keep in mind that these problem these representations can also be applied to things like robotics and simulations and stuff like this. It's not only um about games. I just think games make the best examples because they're maybe easiest to understand. And um just remember at some level everything is a game, right? No matter what problem it is we're tackling, there's some thing that we're trying to maximize just like a video game. So um we can think of these things as games.
1:36 So space representation um we talked
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Dave Churchill.