top of page
  • Writer's pictureFelix Abbott

Flow Fields

I created a basic flow field for my Advanced Game AI class. It can detect impassible as well as rough terrain and have agents follow a path to the target location based on the field costs. The grid can be adjusted during runtime as seen above. Included in this devblog is an overview of my code for the algorithm.


Grid Controller

The grid controller creates the grid layout including the size of the grid and how big the cells are. Here you can also switch between the different fields you can view in the scene.


Grid Cell

Each cell on the field has a set of variables.

worldPosition: It's location in the game

gridIndex: It's location in the list of cells

cost: Cost of the cell (for the cost field)

bestCost: Cost of the cell (for the integration field)

bestDirection: Direction agent will follow to get to the target


Flow Fields -> Cost Field

The cost field loops through every cell in the grid and uses a physics overlap to find any obstacles on it, in this case anything on a Impassible or Rough Terrain layer. From there it goes through all the obstacles that were hit and updates the cost according (Max value for impassible obstacles and adds 3 for rough terrain). The cells never need to be reset to the default value here since the field is reinitialized each time a change is made, which automatically resets their values back to the default values before recalculating.


Flow Fields -> Integration Field

The integration field works on a similar way to the cost field. It sets a destination cell, resets the cost values (since it's the final destination) and gets added to the end of a queue. From there I use a while loop which stores the destination cell and deletes it from the queue, gets the neighboring cells based on cardinal directions (north, south, east and west, not including diagonal cells), then uses a foreach loop to go through all the neighboring cells and updates their cost values accordingly. Any cell besides impassible cells gets added to the queue, while any other type of cell gets added to the queue. This loops until eventually it goes through all the cells in the grid.


Flow Fields -> Flow Field

The actual flow field is overall simple. It goes through very cell in the grid and gets all the neighbors surrounding the current cell (including diagonal cells). From there I go through all the neighboring cells, check to see if the cost of the current cell is better than our current best cost and if it is, update the best cost as well as the best direction. So essentially, this is getting the best direction for the agents to follow to go to the target which is visualized with arrows as seen above.

Recent Posts

See All


bottom of page