Up to date

This page is up to date for Godot 4.1. If you still find outdated information, please open an issue.

NavigationPolygon

Inherits: Resource < RefCounted < Object

A navigation polygon that defines traversable areas and obstacles.

Description

There are two ways to create polygons. Either by using the add_outline method, or using the add_polygon method.

Using add_outline:

GDScriptC#

  1. var polygon = NavigationPolygon.new()
  2. var outline = PackedVector2Array([Vector2(0, 0), Vector2(0, 50), Vector2(50, 50), Vector2(50, 0)])
  3. polygon.add_outline(outline)
  4. polygon.make_polygons_from_outlines()
  5. $NavigationRegion2D.navigation_polygon = polygon
  1. var polygon = new NavigationPolygon();
  2. var outline = new Vector2[] { new Vector2(0, 0), new Vector2(0, 50), new Vector2(50, 50), new Vector2(50, 0) };
  3. polygon.AddOutline(outline);
  4. polygon.MakePolygonsFromOutlines();
  5. GetNode<NavigationRegion2D>("NavigationRegion2D").NavigationPolygon = polygon;

Using add_polygon and indices of the vertices array.

GDScriptC#

  1. var polygon = NavigationPolygon.new()
  2. var vertices = PackedVector2Array([Vector2(0, 0), Vector2(0, 50), Vector2(50, 50), Vector2(50, 0)])
  3. polygon.vertices = vertices
  4. var indices = PackedInt32Array([0, 1, 2, 3])
  5. polygon.add_polygon(indices)
  6. $NavigationRegion2D.navigation_polygon = polygon
  1. var polygon = new NavigationPolygon();
  2. var vertices = new Vector2[] { new Vector2(0, 0), new Vector2(0, 50), new Vector2(50, 50), new Vector2(50, 0) };
  3. polygon.Vertices = vertices;
  4. var indices = new int[] { 0, 1, 2, 3 };
  5. polygon.AddPolygon(indices);
  6. GetNode<NavigationRegion2D>("NavigationRegion2D").NavigationPolygon = polygon;

Tutorials

Properties

float

cell_size

1.0

Methods

void

add_outline ( PackedVector2Array outline )

void

add_outline_at_index ( PackedVector2Array outline, int index )

void

add_polygon ( PackedInt32Array polygon )

void

clear_outlines ( )

void

clear_polygons ( )

NavigationMesh

get_navigation_mesh ( )

PackedVector2Array

get_outline ( int idx ) const

int

get_outline_count ( ) const

PackedInt32Array

get_polygon ( int idx )

int

get_polygon_count ( ) const

PackedVector2Array

get_vertices ( ) const

void

make_polygons_from_outlines ( )

void

remove_outline ( int idx )

void

set_outline ( int idx, PackedVector2Array outline )

void

set_vertices ( PackedVector2Array vertices )


Property Descriptions

float cell_size = 1.0

  • void set_cell_size ( float value )

  • float get_cell_size ( )

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


Method Descriptions

void add_outline ( PackedVector2Array outline )

Appends a PackedVector2Array that contains the vertices of an outline to the internal array that contains all the outlines. You have to call make_polygons_from_outlines in order for this array to be converted to polygons that the engine will use.


void add_outline_at_index ( PackedVector2Array outline, int index )

Adds a PackedVector2Array that contains the vertices of an outline to the internal array that contains all the outlines at a fixed position. You have to call make_polygons_from_outlines in order for this array to be converted to polygons that the engine will use.


void add_polygon ( PackedInt32Array polygon )

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


void clear_outlines ( )

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


void clear_polygons ( )

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


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 NavigationServer3D.region_set_navigation_mesh API directly (as 2D uses the 3D server behind the scene).


PackedVector2Array get_outline ( int idx ) const

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


int get_outline_count ( ) const

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


PackedInt32Array get_polygon ( int idx )

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


int get_polygon_count ( ) const

Returns the count of all polygons.


PackedVector2Array get_vertices ( ) const

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


void make_polygons_from_outlines ( )

Creates polygons from the outlines added in the editor or by script.


void remove_outline ( int idx )

Removes an outline created in the editor or by script. You have to call make_polygons_from_outlines for the polygons to update.


void set_outline ( int idx, PackedVector2Array outline )

Changes an outline created in the editor or by script. You have to call make_polygons_from_outlines for the polygons to update.


void set_vertices ( PackedVector2Array vertices )

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