Skip to content

NavigationPolygon

Inherits: Resource < RefCounted < Object

A 2D navigation mesh that describes a traversable surface for pathfinding.

A navigation mesh can be created either by baking it with the help of the NavigationSystem2D, or by adding vertices and convex polygon indices arrays manually.

To bake a navigation mesh at least one outline needs to be added that defines the outer bounds of the baked area.

var newNavigationMesh = new NavigationPolygon();
Vector2[] boundingOutline = [new Vector2(0, 0), new Vector2(0, 50), new Vector2(50, 50), new Vector2(50, 0)];
newNavigationMesh.AddOutline(boundingOutline);
NavigationSystem2D.BakeFromSourceGeometryData(newNavigationMesh, new NavigationMeshSourceGeometryData2D());
GetEntity<NavigationRegion2D>("NavigationRegion2D").NavigationPolygon = newNavigationMesh;

Adding vertices and polygon indices manually.

var newNavigationMesh = new NavigationPolygon();
Vector2[] newVertices = [new Vector2(0, 0), new Vector2(0, 50), new Vector2(50, 50), new Vector2(50, 0)];
newNavigationMesh.Vertices = newVertices;
int[] newPolygonIndices = [0, 1, 2, 3];
newNavigationMesh.AddPolygon(newPolygonIndices);
GetEntity<NavigationRegion2D>("NavigationRegion2D").NavigationPolygon = newNavigationMesh;
Type Name Default
float agent_radius 10.0
Rect2 baking_rect Rect2(0, 0, 0, 0)
Vector2 baking_rect_offset Vector2(0, 0)
float border_size 0.0
float cell_size 1.0
int parsed_collision_mask 4294967295
ParsedGeometryType parsed_geometry_type 2
SamplePartitionType sample_partition_type 0
StringName source_geometry_group_name &"navigation_polygon_source_geometry_group"
SourceGeometryMode source_geometry_mode 0
Returns Method
void add_outline(outline: PackedVector2Array)
void add_outline_at_index(outline: PackedVector2Array, index: int)
void add_polygon(polygon: PackedInt32Array)
void clear()
void clear_outlines()
void clear_polygons()
NavigationMesh get_navigation_mesh()
PackedVector2Array get_outline(idx: int) const
int get_outline_count() const
bool get_parsed_collision_mask_value(layer_number: int) const
PackedInt32Array get_polygon(idx: int)
int get_polygon_count() const
PackedVector2Array get_vertices() const
void remove_outline(idx: int)
void set_outline(idx: int, outline: PackedVector2Array)
void set_parsed_collision_mask_value(layer_number: int, value: bool)
void set_vertices(vertices: PackedVector2Array)

SamplePartitionType

enum SamplePartitionType

SAMPLE_PARTITION_CONVEX_PARTITION = 0

Convex partitioning that results in a navigation mesh with convex polygons.

SAMPLE_PARTITION_TRIANGULATE = 1

Triangulation partitioning that results in a navigation mesh with triangle polygons.

SAMPLE_PARTITION_MAX = 2

Represents the size of the SamplePartitionType enum.

ParsedGeometryType

enum ParsedGeometryType

PARSED_GEOMETRY_MESH_INSTANCES = 0

Parses mesh instances as obstruction geometry. This includes Polygon2D, MeshInstance2D, MultiMeshInstance2D, and TileMapLayer nodes.

Meshes are only parsed when they use a 2D vertices surface format.

PARSED_GEOMETRY_STATIC_COLLIDERS = 1

Parses StaticBody2D and TileMapLayer colliders as obstruction geometry. The collider should be in any of the layers specified by parsed_collision_mask.

PARSED_GEOMETRY_BOTH = 2

Both PARSED_GEOMETRY_MESH_INSTANCES and PARSED_GEOMETRY_STATIC_COLLIDERS.

PARSED_GEOMETRY_MAX = 3

Represents the size of the ParsedGeometryType enum.

SourceGeometryMode

enum SourceGeometryMode

SOURCE_GEOMETRY_ROOT_NODE_CHILDREN = 0

Scans the child nodes of the root node recursively for geometry.

SOURCE_GEOMETRY_GROUPS_WITH_CHILDREN = 1

Scans nodes in a group and their child nodes recursively for geometry. The group is specified by source_geometry_group_name.

SOURCE_GEOMETRY_GROUPS_EXPLICIT = 2

Uses nodes in a group for geometry. The group is specified by source_geometry_group_name.

SOURCE_GEOMETRY_MAX = 3

Represents the size of the SourceGeometryMode enum.

agent_radius

float agent_radius = 10.0

The distance to erode/shrink the walkable surface when baking the navigation mesh.

Note: The radius must be equal or higher than 0.0. If the radius is 0.0, it won’t be possible to fix invalid outline overlaps and other precision errors during the baking process. As a result, some obstacles may be excluded incorrectly from the final navigation mesh, or may delete the navigation mesh’s polygons.

baking_rect

Rect2 baking_rect = Rect2(0, 0, 0, 0)

If the baking Rect2 has an area the navigation mesh baking will be restricted to its enclosing area.

baking_rect_offset

Vector2 baking_rect_offset = Vector2(0, 0)

The position offset applied to the baking_rect Rect2.

border_size

float border_size = 0.0

The size of the non-navigable border around the bake bounding area defined by the baking_rect Rect2.

In conjunction with the baking_rect the border size can be used to bake tile aligned navigation meshes without the tile edges being shrunk by agent_radius.

cell_size

float cell_size = 1.0

The cell size used to rasterize the navigation mesh vertices. Must match with the cell size on the navigation map.

parsed_collision_mask

int parsed_collision_mask = 4294967295

The physics layers to scan for static colliders.

Only used when parsed_geometry_type is PARSED_GEOMETRY_STATIC_COLLIDERS or PARSED_GEOMETRY_BOTH.

parsed_geometry_type

ParsedGeometryType parsed_geometry_type = 2

Determines which type of nodes will be parsed as geometry.

sample_partition_type

SamplePartitionType sample_partition_type = 0

Partitioning algorithm for creating the navigation mesh polys.

source_geometry_group_name

StringName source_geometry_group_name = &"navigation_polygon_source_geometry_group"

The group name of nodes that should be parsed for baking source geometry.

Only used when source_geometry_mode is SOURCE_GEOMETRY_GROUPS_WITH_CHILDREN or SOURCE_GEOMETRY_GROUPS_EXPLICIT.

source_geometry_mode

SourceGeometryMode source_geometry_mode = 0

The source of the geometry used when baking.

add_outline

void add_outline(outline: PackedVector2Array)

Appends a PackedVector2Array that contains the vertices of an outline to the internal array that contains all the outlines.

add_outline_at_index

void add_outline_at_index(outline: PackedVector2Array, index: int)

Adds a PackedVector2Array that contains the vertices of an outline to the internal array that contains all the outlines at a fixed position.

add_polygon

void add_polygon(polygon: PackedInt32Array)

Adds a polygon using the indices of the vertices you get when calling get_vertices().

clear

void clear()

Clears the internal arrays for vertices and polygon indices.

clear_outlines

void clear_outlines()

Clears the array of the outlines, but it doesn’t clear the vertices and the polygons that were created by them.

clear_polygons

void clear_polygons()

Clears the array of polygons, but it doesn’t clear the array of outlines and vertices.

get_navigation_mesh

NavigationMesh get_navigation_mesh()

Returns the NavigationMesh resulting from this navigation polygon. This navigation mesh can be used to update the navigation mesh of a region with the NavigationSystem3D.region_set_navigation_mesh() API directly.

get_outline

PackedVector2Array get_outline(idx: int) const

Returns a PackedVector2Array containing the vertices of an outline that was created in the editor or by script.

get_outline_count

int get_outline_count() const

Returns the number of outlines that were created in the editor or by script.

get_parsed_collision_mask_value

bool get_parsed_collision_mask_value(layer_number: int) const

Returns whether or not the specified layer of the parsed_collision_mask is enabled, given a layer_number between 1 and 32.

get_polygon

PackedInt32Array get_polygon(idx: int)

Returns a PackedInt32Array containing the indices of the vertices of a created polygon.

get_polygon_count

int get_polygon_count() const

Returns the count of all polygons.

get_vertices

PackedVector2Array get_vertices() const

Returns a PackedVector2Array containing all the vertices being used to create the polygons.

remove_outline

void remove_outline(idx: int)

Removes an outline created in the editor or by script. You have to bake the navigation polygon again with NavigationSystem2D.bake_from_source_geometry_data() for the polygons to update.

set_outline

void set_outline(idx: int, outline: PackedVector2Array)

Changes an outline created in the editor or by script. You have to bake the navigation polygon again with NavigationSystem2D.bake_from_source_geometry_data() for the polygons to update.

set_parsed_collision_mask_value

void set_parsed_collision_mask_value(layer_number: int, value: bool)

Based on value, enables or disables the specified layer in the parsed_collision_mask, given a layer_number between 1 and 32.

set_vertices

void set_vertices(vertices: PackedVector2Array)

Sets the vertices that can be then indexed to create polygons with the add_polygon() method.