NavigationPolygon
Experimental: This class may be changed or removed in future versions.
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 theNavigationServer2D, 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.
varnew_navigation_mesh=NavigationPolygon.new()varbounding_outline=PackedVector2Array([Vector2(0,0),Vector2(0,50),Vector2(50,50),Vector2(50,0)])new_navigation_mesh.add_outline(bounding_outline)NavigationServer2D.bake_from_source_geometry_data(new_navigation_mesh,NavigationMeshSourceGeometryData2D.new());$NavigationRegion2D.navigation_polygon=new_navigation_mesh
varnewNavigationMesh=newNavigationPolygon();Vector2[]boundingOutline=[newVector2(0,0),newVector2(0,50),newVector2(50,50),newVector2(50,0)];newNavigationMesh.AddOutline(boundingOutline);NavigationServer2D.BakeFromSourceGeometryData(newNavigationMesh,newNavigationMeshSourceGeometryData2D());GetNode<NavigationRegion2D>("NavigationRegion2D").NavigationPolygon=newNavigationMesh;
Adding vertices and polygon indices manually.
varnew_navigation_mesh=NavigationPolygon.new()varnew_vertices=PackedVector2Array([Vector2(0,0),Vector2(0,50),Vector2(50,50),Vector2(50,0)])new_navigation_mesh.vertices=new_verticesvarnew_polygon_indices=PackedInt32Array([0,1,2,3])new_navigation_mesh.add_polygon(new_polygon_indices)$NavigationRegion2D.navigation_polygon=new_navigation_mesh
varnewNavigationMesh=newNavigationPolygon();Vector2[]newVertices=[newVector2(0,0),newVector2(0,50),newVector2(50,50),newVector2(50,0)];newNavigationMesh.Vertices=newVertices;int[]newPolygonIndices=[0,1,2,3];newNavigationMesh.AddPolygon(newPolygonIndices);GetNode<NavigationRegion2D>("NavigationRegion2D").NavigationPolygon=newNavigationMesh;
Tutorials
Properties
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
|
Methods
add_outline(outline:PackedVector2Array) | |
add_outline_at_index(outline:PackedVector2Array, index:int) | |
add_polygon(polygon:PackedInt32Array) | |
clear() | |
get_outline(idx:int)const | |
get_parsed_collision_mask_value(layer_number:int)const | |
get_polygon(idx:int) | |
remove_outline(idx:int) | |
set_outline(idx:int, outline:PackedVector2Array) | |
set_parsed_collision_mask_value(layer_number:int, value:bool) | |
set_vertices(vertices:PackedVector2Array) |
Enumerations
enumSamplePartitionType:🔗
SamplePartitionTypeSAMPLE_PARTITION_CONVEX_PARTITION =0
Convex partitioning that yields navigation mesh with convex polygons.
SamplePartitionTypeSAMPLE_PARTITION_TRIANGULATE =1
Triangulation partitioning that yields navigation mesh with triangle polygons.
SamplePartitionTypeSAMPLE_PARTITION_MAX =2
Represents the size of theSamplePartitionType enum.
enumParsedGeometryType:🔗
ParsedGeometryTypePARSED_GEOMETRY_MESH_INSTANCES =0
Parses mesh instances as obstruction geometry. This includesPolygon2D,MeshInstance2D,MultiMeshInstance2D, andTileMap nodes.
Meshes are only parsed when they use a 2D vertices surface format.
ParsedGeometryTypePARSED_GEOMETRY_STATIC_COLLIDERS =1
ParsesStaticBody2D andTileMap colliders as obstruction geometry. The collider should be in any of the layers specified byparsed_collision_mask.
ParsedGeometryTypePARSED_GEOMETRY_BOTH =2
BothPARSED_GEOMETRY_MESH_INSTANCES andPARSED_GEOMETRY_STATIC_COLLIDERS.
ParsedGeometryTypePARSED_GEOMETRY_MAX =3
Represents the size of theParsedGeometryType enum.
enumSourceGeometryMode:🔗
SourceGeometryModeSOURCE_GEOMETRY_ROOT_NODE_CHILDREN =0
Scans the child nodes of the root node recursively for geometry.
SourceGeometryModeSOURCE_GEOMETRY_GROUPS_WITH_CHILDREN =1
Scans nodes in a group and their child nodes recursively for geometry. The group is specified bysource_geometry_group_name.
SourceGeometryModeSOURCE_GEOMETRY_GROUPS_EXPLICIT =2
Uses nodes in a group for geometry. The group is specified bysource_geometry_group_name.
SourceGeometryModeSOURCE_GEOMETRY_MAX =3
Represents the size of theSourceGeometryMode enum.
Property Descriptions
floatget_agent_radius()
The distance to erode/shrink the walkable surface when baking the navigation mesh.
Rect2baking_rect =Rect2(0,0,0,0)
🔗
Rect2get_baking_rect()
If the bakingRect2 has an area the navigation mesh baking will be restricted to its enclosing area.
Vector2baking_rect_offset =Vector2(0,0)
🔗
Vector2get_baking_rect_offset()
The position offset applied to thebaking_rectRect2.
floatget_border_size()
The size of the non-navigable border around the bake bounding area defined by thebaking_rectRect2.
In conjunction with thebaking_rect the border size can be used to bake tile aligned navigation meshes without the tile edges being shrunk byagent_radius.
floatget_cell_size()
The cell size used to rasterize the navigation mesh vertices. Must match with the cell size on the navigation map.
intparsed_collision_mask =4294967295
🔗
intget_parsed_collision_mask()
The physics layers to scan for static colliders.
Only used whenparsed_geometry_type isPARSED_GEOMETRY_STATIC_COLLIDERS orPARSED_GEOMETRY_BOTH.
ParsedGeometryTypeparsed_geometry_type =2
🔗
ParsedGeometryTypeget_parsed_geometry_type()
Determines which type of nodes will be parsed as geometry. SeeParsedGeometryType for possible values.
SamplePartitionTypesample_partition_type =0
🔗
SamplePartitionTypeget_sample_partition_type()
Partitioning algorithm for creating the navigation mesh polys. SeeSamplePartitionType for possible values.
StringNamesource_geometry_group_name =&"navigation_polygon_source_geometry_group"
🔗
StringNameget_source_geometry_group_name()
The group name of nodes that should be parsed for baking source geometry.
Only used whensource_geometry_mode isSOURCE_GEOMETRY_GROUPS_WITH_CHILDREN orSOURCE_GEOMETRY_GROUPS_EXPLICIT.
SourceGeometryModesource_geometry_mode =0
🔗
SourceGeometryModeget_source_geometry_mode()
The source of the geometry used when baking. SeeSourceGeometryMode for possible values.
Method Descriptions
voidadd_outline(outline:PackedVector2Array)🔗
Appends aPackedVector2Array that contains the vertices of an outline to the internal array that contains all the outlines.
voidadd_outline_at_index(outline:PackedVector2Array, index:int)🔗
Adds aPackedVector2Array that contains the vertices of an outline to the internal array that contains all the outlines at a fixed position.
voidadd_polygon(polygon:PackedInt32Array)🔗
Adds a polygon using the indices of the vertices you get when callingget_vertices().
Clears the internal arrays for vertices and polygon indices.
Clears the array of the outlines, but it doesn't clear the vertices and the polygons that were created by them.
Clears the array of polygons, but it doesn't clear the array of outlines and vertices.
NavigationMeshget_navigation_mesh()🔗
Returns theNavigationMesh resulting from this navigation polygon. This navigation mesh can be used to update the navigation mesh of a region with theNavigationServer3D.region_set_navigation_mesh() API directly (as 2D uses the 3D server behind the scene).
PackedVector2Arrayget_outline(idx:int)const🔗
Returns aPackedVector2Array containing the vertices of an outline that was created in the editor or by script.
Returns the number of outlines that were created in the editor or by script.
boolget_parsed_collision_mask_value(layer_number:int)const🔗
Returns whether or not the specified layer of theparsed_collision_mask is enabled, given alayer_number
between 1 and 32.
PackedInt32Arrayget_polygon(idx:int)🔗
Returns aPackedInt32Array containing the indices of the vertices of a created polygon.
Returns the count of all polygons.
PackedVector2Arrayget_vertices()const🔗
Returns aPackedVector2Array containing all the vertices being used to create the polygons.
voidmake_polygons_from_outlines()🔗
Deprecated: UseNavigationServer2D.parse_source_geometry_data() andNavigationServer2D.bake_from_source_geometry_data() instead.
Creates polygons from the outlines added in the editor or by script.
Removes an outline created in the editor or by script. You have to callmake_polygons_from_outlines() for the polygons to update.
voidset_outline(idx:int, outline:PackedVector2Array)🔗
Changes an outline created in the editor or by script. You have to callmake_polygons_from_outlines() for the polygons to update.
voidset_parsed_collision_mask_value(layer_number:int, value:bool)🔗
Based onvalue
, enables or disables the specified layer in theparsed_collision_mask, given alayer_number
between 1 and 32.
voidset_vertices(vertices:PackedVector2Array)🔗
Sets the vertices that can be then indexed to create polygons with theadd_polygon() method.