Intro

A grid lattice will be created in Unity to allow for an agent to engage in path planning and obstacle avoidance.

Resources

Overview

Set up

  • Clone the repository
  • Open the folder D:\Unity\Projects\GameAIPathPlanning in Unity
  • Select Scenes/GridNavigation to open GridNavigation
  • Click play
    • You can click anywhere on the plane to see the Agent attempt to navigate there
    • You can also use the following keyboard controls

Keyboard controls

Spacebar - change search algorithm (see HUD top right)

CreateGrid Class Implementation

  • Game Grid calls CreateGrid.Create() and CreateGrid.IsTraversable()
  • The Create() method generates a 2D Boolean array indicating whether cells in the grid (canvas width × height) are traversable
  • The values in game world coordinates/dimensions such as canvasOrigin, canvasWidth, canvasHeight etc can be converted with Convert()
    • need to put width and height into a Vector2 to use it- ex) (canvasWidth, canvasHeight)

  • The IsTraversable() method checks 4-way and 8-way connectivity from grid[x,y] in the direction dir

Prefigs

  • In CustomPresetConfig.cs, can make new SceneConfigs by copying one like SC0, then add to awake
  • SCeneConfig()- pass in worldSize, worldOrigin, the ball (agentPos, agentScale)

Helper Methods

  • IsPointInsideAxisAlignedBoundingBox() — tests if a point lies within a bounding box

  • IsRangeOverlapping() — determines if two integer ranges overlap

  • IsAxisAlignedBoundingBoxOverlapping() — checks if two AABBs overlap or touch

GridTest (Unit / Integration Testing)

  • Unity tests use the NUnit framework and run via Unity’s Test Runner (Window General Test Runner)
  • Extend automated testing to cover more scenarios and edge cases

Debugging

  • Use print statments- print() or Debug.Log()/Error()
  • Or you can install the Unity plugin for the Visual Studio debugger

Tips

  • C-sharp API C# Lists, arrays, tuples
  • Unity Scripting API Vector2/Vector2Int class, Floor2Int
  • You can obtain obstacle polygon coordinates with poly.getPoints() (floating point in world coordinates or WCS) or poly.getIntegerPoints() (pre-converted to integer-discretized form). You should perform geometric tests using the integer versions.
  • The points of a polygon are stored in counterclockwise (CCW) order. Each adjacent pair (and the wraparound) returned by getIntegerPoints()/getPoints() is a line segment. Here is an efficient loop due to no modulus or multiplies while still handling wraparound.
var len = polyVerts.Length;
for (int i = 0, j = len - 1; i < len; j = i++)
{
    var p1 = polyVerts[j];
    var p2 = polyVerts[i];
    
    // Do something with line seg, p1->p2
}

Creating the Grid

description description

Algorithm

  • Determine number of grid rows and columns based on canvas width and height and cell width (square)
  • Create Grid using new bool[rows, columns]- by default all your values will be false
  • Loop through each cell (for each column x and an inner loop for each row y) and decide what would make that cell be navigable (i.e. flipped to true).
  • If either of the following is true, then grid[x, y] is true:
    • No polygon vertex intersects any edge
      • For each polygon, grab each vertix and determine if it crosses any of the edges on your square
      • Shrink method recommended
        • Rect- 0.001f less all around
        • RectInt (integer, better)- minimums are shrunk by adding 1, the maximums by substracting 1
    • No cell (RectInt) corners are inside a polygon
      • Determine if any shrunk corners is inside said polygon.

Shrink Cells

description description