NavigationPolygon

Inherits: Resource < RefCounted < Object

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

Description

A navigation mesh can be created either by baking it with the help of the NavigationServer2D, 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.

GDScriptC#

  1. var new_navigation_mesh = NavigationPolygon.new()
  2. var bounding_outline = PackedVector2Array([Vector2(0, 0), Vector2(0, 50), Vector2(50, 50), Vector2(50, 0)])
  3. new_navigation_mesh.add_outline(bounding_outline)
  4. NavigationServer2D.bake_from_source_geometry_data(new_navigation_mesh, NavigationMeshSourceGeometryData2D.new());
  5. $NavigationRegion2D.navigation_polygon = new_navigation_mesh
  1. var newNavigationMesh = new NavigationPolygon();
  2. var boundingOutline = new Vector2[] { new Vector2(0, 0), new Vector2(0, 50), new Vector2(50, 50), new Vector2(50, 0) };
  3. newNavigationMesh.AddOutline(boundingOutline);
  4. NavigationServer2D.BakeFromSourceGeometryData(newNavigationMesh, new NavigationMeshSourceGeometryData2D());
  5. GetNode<NavigationRegion2D>("NavigationRegion2D").NavigationPolygon = newNavigationMesh;

Adding vertices and polygon indices manually.

GDScriptC#

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

Tutorials

Properties

float

agent_radius

10.0

float

cell_size

1.0

int

parsed_collision_mask

4294967295

ParsedGeometryType

parsed_geometry_type

2

StringName

source_geometry_group_name

&”navigation_polygon_source_geometry_group”

SourceGeometryMode

source_geometry_mode

0

Methods

void

add_outline ( PackedVector2Array outline )

void

add_outline_at_index ( PackedVector2Array outline, int index )

void

add_polygon ( PackedInt32Array polygon )

void

clear ( )

void

clear_outlines ( )

void

clear_polygons ( )

NavigationMesh

get_navigation_mesh ( )

PackedVector2Array

get_outline ( int idx ) const

int

get_outline_count ( ) const

bool

get_parsed_collision_mask_value ( int layer_number ) 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_parsed_collision_mask_value ( int layer_number, bool value )

void

set_vertices ( PackedVector2Array vertices )


Enumerations

enum ParsedGeometryType:

ParsedGeometryType PARSED_GEOMETRY_MESH_INSTANCES = 0

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

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

ParsedGeometryType PARSED_GEOMETRY_STATIC_COLLIDERS = 1

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

ParsedGeometryType PARSED_GEOMETRY_BOTH = 2

Both PARSED_GEOMETRY_MESH_INSTANCES and PARSED_GEOMETRY_STATIC_COLLIDERS.

ParsedGeometryType PARSED_GEOMETRY_MAX = 3

Represents the size of the ParsedGeometryType enum.


enum SourceGeometryMode:

SourceGeometryMode SOURCE_GEOMETRY_ROOT_NODE_CHILDREN = 0

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

SourceGeometryMode 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.

SourceGeometryMode SOURCE_GEOMETRY_GROUPS_EXPLICIT = 2

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

SourceGeometryMode SOURCE_GEOMETRY_MAX = 3

Represents the size of the SourceGeometryMode enum.


Property Descriptions

float agent_radius = 10.0

  • void set_agent_radius ( float value )

  • float get_agent_radius ( )

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


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.


int parsed_collision_mask = 4294967295

  • void set_parsed_collision_mask ( int value )

  • int get_parsed_collision_mask ( )

The physics layers to scan for static colliders.

Only used when parsed_geometry_type is PARSED_GEOMETRY_STATIC_COLLIDERS or PARSED_GEOMETRY_BOTH.


ParsedGeometryType parsed_geometry_type = 2

Determines which type of nodes will be parsed as geometry. See ParsedGeometryType for possible values.


StringName source_geometry_group_name = &"navigation_polygon_source_geometry_group"

  • void set_source_geometry_group_name ( StringName value )

  • StringName get_source_geometry_group_name ( )

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.


SourceGeometryMode source_geometry_mode = 0

The source of the geometry used when baking. See SourceGeometryMode for possible values.


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.


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.


void add_polygon ( PackedInt32Array polygon )

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


void clear ( )

Clears the internal arrays for vertices and polygon indices.


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.


bool get_parsed_collision_mask_value ( int layer_number ) const

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


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.

Deprecated. This function is deprecated, and might be removed in a future release. Use NavigationServer2D.parse_source_geometry_data and NavigationServer2D.bake_from_source_geometry_data instead.


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_parsed_collision_mask_value ( int layer_number, bool value )

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


void set_vertices ( PackedVector2Array vertices )

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

Previous Next


© Copyright 2014-present Juan Linietsky, Ariel Manzur and the Godot community (CC BY 3.0). Revision 53e837c6.

Built with Sphinx using a theme provided by Read the Docs.