Skip to content

Commit

Permalink
Make navigation mesh edge connections optional
Browse files Browse the repository at this point in the history
Makes navigation mesh edge connections optional.
  • Loading branch information
smix8 committed May 11, 2023
1 parent fd4a06c commit f986b52
Show file tree
Hide file tree
Showing 23 changed files with 230 additions and 2 deletions.
3 changes: 3 additions & 0 deletions doc/classes/NavigationRegion2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,8 @@
<member name="travel_cost" type="float" setter="set_travel_cost" getter="get_travel_cost" default="1.0">
When pathfinding moves inside this region's navigation mesh the traveled distances are multiplied with [code]travel_cost[/code] for determining the shortest path.
</member>
<member name="use_edge_connections" type="bool" setter="set_use_edge_connections" getter="get_use_edge_connections" default="true">
If enabled the navigation region will use edge connections to connect with other navigation regions within proximity of the navigation map edge connection margin.
</member>
</members>
</class>
3 changes: 3 additions & 0 deletions doc/classes/NavigationRegion3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
<member name="travel_cost" type="float" setter="set_travel_cost" getter="get_travel_cost" default="1.0">
When pathfinding moves inside this region's navigation mesh the traveled distances are multiplied with [code]travel_cost[/code] for determining the shortest path.
</member>
<member name="use_edge_connections" type="bool" setter="set_use_edge_connections" getter="get_use_edge_connections" default="true">
If enabled the navigation region will use edge connections to connect with other navigation regions within proximity of the navigation map edge connection margin.
</member>
</members>
<signals>
<signal name="bake_finished">
Expand Down
30 changes: 30 additions & 0 deletions doc/classes/NavigationServer2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,13 @@
Returns all navigation regions [RID]s that are currently assigned to the requested navigation [param map].
</description>
</method>
<method name="map_get_use_edge_connections" qualifiers="const">
<return type="bool" />
<param index="0" name="map" type="RID" />
<description>
Returns whether the navigation [param map] allows navigation regions to use edge connections to connect with other navigation regions within proximity of the navigation map edge connection margin.
</description>
</method>
<method name="map_is_active" qualifiers="const">
<return type="bool" />
<param index="0" name="map" type="RID" />
Expand Down Expand Up @@ -443,6 +450,14 @@
Set the map's link connection radius used to connect links to navigation polygons.
</description>
</method>
<method name="map_set_use_edge_connections">
<return type="void" />
<param index="0" name="map" type="RID" />
<param index="1" name="enabled" type="bool" />
<description>
Set the navigation [param map] edge connection use. If [param enabled] the navigation map allows navigation regions to use edge connections to connect with other navigation regions within proximity of the navigation map edge connection margin.
</description>
</method>
<method name="obstacle_create">
<return type="RID" />
<description>
Expand Down Expand Up @@ -559,6 +574,13 @@
Returns the travel cost of this [param region].
</description>
</method>
<method name="region_get_use_edge_connections" qualifiers="const">
<return type="bool" />
<param index="0" name="region" type="RID" />
<description>
Returns whether the navigation [param region] is set to use edge connections to connect with other navigation regions within proximity of the navigation map edge connection margin.
</description>
</method>
<method name="region_owns_point" qualifiers="const">
<return type="bool" />
<param index="0" name="region" type="RID" />
Expand Down Expand Up @@ -625,6 +647,14 @@
Sets the [param travel_cost] for this [param region].
</description>
</method>
<method name="region_set_use_edge_connections">
<return type="void" />
<param index="0" name="region" type="RID" />
<param index="1" name="enabled" type="bool" />
<description>
If [param enabled] the navigation [param region] will use edge connections to connect with other navigation regions within proximity of the navigation map edge connection margin.
</description>
</method>
<method name="set_debug_enabled">
<return type="void" />
<param index="0" name="enabled" type="bool" />
Expand Down
30 changes: 30 additions & 0 deletions doc/classes/NavigationServer3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,13 @@
Returns the map's up direction.
</description>
</method>
<method name="map_get_use_edge_connections" qualifiers="const">
<return type="bool" />
<param index="0" name="map" type="RID" />
<description>
Returns true if the navigation [param map] allows navigation regions to use edge connections to connect with other navigation regions within proximity of the navigation map edge connection margin.
</description>
</method>
<method name="map_is_active" qualifiers="const">
<return type="bool" />
<param index="0" name="map" type="RID" />
Expand Down Expand Up @@ -508,6 +515,14 @@
Sets the map up direction.
</description>
</method>
<method name="map_set_use_edge_connections">
<return type="void" />
<param index="0" name="map" type="RID" />
<param index="1" name="enabled" type="bool" />
<description>
Set the navigation [param map] edge connection use. If [param enabled] the navigation map allows navigation regions to use edge connections to connect with other navigation regions within proximity of the navigation map edge connection margin.
</description>
</method>
<method name="obstacle_create">
<return type="RID" />
<description>
Expand Down Expand Up @@ -641,6 +656,13 @@
Returns the travel cost of this [param region].
</description>
</method>
<method name="region_get_use_edge_connections" qualifiers="const">
<return type="bool" />
<param index="0" name="region" type="RID" />
<description>
Returns true if the navigation [param region] is set to use edge connections to connect with other navigation regions within proximity of the navigation map edge connection margin.
</description>
</method>
<method name="region_owns_point" qualifiers="const">
<return type="bool" />
<param index="0" name="region" type="RID" />
Expand Down Expand Up @@ -707,6 +729,14 @@
Sets the [param travel_cost] for this [param region].
</description>
</method>
<method name="region_set_use_edge_connections">
<return type="void" />
<param index="0" name="region" type="RID" />
<param index="1" name="enabled" type="bool" />
<description>
If [param enabled] the navigation [param region] will use edge connections to connect with other navigation regions within proximity of the navigation map edge connection margin.
</description>
</method>
<method name="set_active">
<return type="void" />
<param index="0" name="active" type="bool" />
Expand Down
6 changes: 6 additions & 0 deletions doc/classes/ProjectSettings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1915,6 +1915,9 @@
<member name="navigation/2d/default_link_connection_radius" type="int" setter="" getter="" default="4">
Default link connection radius for 2D navigation maps. See [method NavigationServer2D.map_set_link_connection_radius].
</member>
<member name="navigation/2d/use_edge_connections" type="bool" setter="" getter="" default="true">
If enabled 2D navigation regions will use edge connections to connect with other navigation regions within proximity of the navigation map edge connection margin. This setting only affects World2D default navigation maps.
</member>
<member name="navigation/3d/default_cell_size" type="float" setter="" getter="" default="0.25">
Default cell size for 3D navigation maps. See [method NavigationServer3D.map_set_cell_size].
</member>
Expand All @@ -1924,6 +1927,9 @@
<member name="navigation/3d/default_link_connection_radius" type="float" setter="" getter="" default="1.0">
Default link connection radius for 3D navigation maps. See [method NavigationServer3D.map_set_link_connection_radius].
</member>
<member name="navigation/3d/use_edge_connections" type="bool" setter="" getter="" default="true">
If enabled 3D navigation regions will use edge connections to connect with other navigation regions within proximity of the navigation map edge connection margin. This setting only affects World3D default navigation maps.
</member>
<member name="navigation/avoidance/thread_model/avoidance_use_high_priority_threads" type="bool" setter="" getter="" default="true">
If enabled and avoidance calculations use multiple threads the threads run with high priority.
</member>
Expand Down
28 changes: 28 additions & 0 deletions modules/navigation/godot_navigation_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,20 @@ real_t GodotNavigationServer::map_get_cell_size(RID p_map) const {
return map->get_cell_size();
}

COMMAND_2(map_set_use_edge_connections, RID, p_map, bool, p_enabled) {
NavMap *map = map_owner.get_or_null(p_map);
ERR_FAIL_COND(map == nullptr);

map->set_use_edge_connections(p_enabled);
}

bool GodotNavigationServer::map_get_use_edge_connections(RID p_map) const {
NavMap *map = map_owner.get_or_null(p_map);
ERR_FAIL_COND_V(map == nullptr, false);

return map->get_use_edge_connections();
}

COMMAND_2(map_set_edge_connection_margin, RID, p_map, real_t, p_connection_margin) {
NavMap *map = map_owner.get_or_null(p_map);
ERR_FAIL_COND(map == nullptr);
Expand Down Expand Up @@ -312,6 +326,20 @@ RID GodotNavigationServer::region_create() {
return rid;
}

COMMAND_2(region_set_use_edge_connections, RID, p_region, bool, p_enabled) {
NavRegion *region = region_owner.get_or_null(p_region);
ERR_FAIL_COND(region == nullptr);

region->set_use_edge_connections(p_enabled);
}

bool GodotNavigationServer::region_get_use_edge_connections(RID p_region) const {
NavRegion *region = region_owner.get_or_null(p_region);
ERR_FAIL_COND_V(region == nullptr, false);

return region->get_use_edge_connections();
}

COMMAND_2(region_set_map, RID, p_region, RID, p_map) {
NavRegion *region = region_owner.get_or_null(p_region);
ERR_FAIL_COND(region == nullptr);
Expand Down
6 changes: 6 additions & 0 deletions modules/navigation/godot_navigation_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ class GodotNavigationServer : public NavigationServer3D {
COMMAND_2(map_set_cell_size, RID, p_map, real_t, p_cell_size);
virtual real_t map_get_cell_size(RID p_map) const override;

COMMAND_2(map_set_use_edge_connections, RID, p_map, bool, p_enabled);
virtual bool map_get_use_edge_connections(RID p_map) const override;

COMMAND_2(map_set_edge_connection_margin, RID, p_map, real_t, p_connection_margin);
virtual real_t map_get_edge_connection_margin(RID p_map) const override;

Expand All @@ -129,6 +132,9 @@ class GodotNavigationServer : public NavigationServer3D {

virtual RID region_create() override;

COMMAND_2(region_set_use_edge_connections, RID, p_region, bool, p_enabled);
virtual bool region_get_use_edge_connections(RID p_region) const override;

COMMAND_2(region_set_enter_cost, RID, p_region, real_t, p_enter_cost);
virtual real_t region_get_enter_cost(RID p_region) const override;
COMMAND_2(region_set_travel_cost, RID, p_region, real_t, p_travel_cost);
Expand Down
5 changes: 5 additions & 0 deletions modules/navigation/nav_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class NavBase : public NavRid {
public:
NavigationUtilities::PathSegmentType get_type() const { return type; }

virtual void set_use_edge_connections(bool p_enabled) {}
virtual bool get_use_edge_connections() const { return false; }

void set_navigation_layers(uint32_t p_navigation_layers) { navigation_layers = p_navigation_layers; }
uint32_t get_navigation_layers() const { return navigation_layers; }

Expand All @@ -59,6 +62,8 @@ class NavBase : public NavRid {

void set_owner_id(ObjectID p_owner_id) { owner_id = p_owner_id; }
ObjectID get_owner_id() const { return owner_id; }

virtual ~NavBase(){};
};

#endif // NAV_BASE_H
12 changes: 11 additions & 1 deletion modules/navigation/nav_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ void NavMap::set_cell_size(real_t p_cell_size) {
regenerate_polygons = true;
}

void NavMap::set_use_edge_connections(bool p_enabled) {
if (use_edge_connections == p_enabled) {
return;
}
use_edge_connections = p_enabled;
regenerate_links = true;
}

void NavMap::set_edge_connection_margin(real_t p_edge_connection_margin) {
if (edge_connection_margin == p_edge_connection_margin) {
return;
Expand Down Expand Up @@ -751,7 +759,9 @@ void NavMap::sync() {
_new_pm_edge_merge_count += 1;
} else {
CRASH_COND_MSG(E.value.size() != 1, vformat("Number of connection != 1. Found: %d", E.value.size()));
free_edges.push_back(E.value[0]);
if (use_edge_connections && E.value[0].polygon->owner->get_use_edge_connections()) {
free_edges.push_back(E.value[0]);
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions modules/navigation/nav_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class NavMap : public NavRid {
/// each cell has the following cell_size.
real_t cell_size = 0.25;

bool use_edge_connections = true;
/// This value is used to detect the near edges to connect.
real_t edge_connection_margin = 0.25;

Expand Down Expand Up @@ -130,6 +131,11 @@ class NavMap : public NavRid {
return cell_size;
}

void set_use_edge_connections(bool p_enabled);
bool get_use_edge_connections() const {
return use_edge_connections;
}

void set_edge_connection_margin(real_t p_edge_connection_margin);
real_t get_edge_connection_margin() const {
return edge_connection_margin;
Expand Down
7 changes: 7 additions & 0 deletions modules/navigation/nav_region.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ void NavRegion::set_map(NavMap *p_map) {
}
}

void NavRegion::set_use_edge_connections(bool p_enabled) {
if (use_edge_connections != p_enabled) {
use_edge_connections = p_enabled;
polygons_dirty = true;
}
}

void NavRegion::set_transform(Transform3D p_transform) {
if (transform == p_transform) {
return;
Expand Down
7 changes: 7 additions & 0 deletions modules/navigation/nav_region.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class NavRegion : public NavBase {
Ref<NavigationMesh> mesh;
Vector<gd::Edge::Connection> connections;

bool use_edge_connections = true;

bool polygons_dirty = true;

/// Cache
Expand All @@ -61,6 +63,11 @@ class NavRegion : public NavBase {
return map;
}

void set_use_edge_connections(bool p_enabled);
bool get_use_edge_connections() const {
return use_edge_connections;
}

void set_transform(Transform3D transform);
const Transform3D &get_transform() const {
return transform;
Expand Down
20 changes: 19 additions & 1 deletion scene/2d/navigation_region_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ bool NavigationRegion2D::is_enabled() const {
return enabled;
}

void NavigationRegion2D::set_use_edge_connections(bool p_enabled) {
if (use_edge_connections == p_enabled) {
return;
}

use_edge_connections = p_enabled;

NavigationServer2D::get_singleton()->region_set_use_edge_connections(region, use_edge_connections);
}

bool NavigationRegion2D::get_use_edge_connections() const {
return use_edge_connections;
}

void NavigationRegion2D::set_navigation_layers(uint32_t p_navigation_layers) {
if (navigation_layers == p_navigation_layers) {
return;
Expand Down Expand Up @@ -210,7 +224,7 @@ void NavigationRegion2D::_notification(int p_what) {

bool enabled_geometry_face_random_color = ns2d->get_debug_navigation_enable_geometry_face_random_color();
bool enabled_edge_lines = ns2d->get_debug_navigation_enable_edge_lines();
bool enable_edge_connections = ns2d->get_debug_navigation_enable_edge_connections();
bool enable_edge_connections = use_edge_connections && ns2d->get_debug_navigation_enable_edge_connections() && ns2d->map_get_use_edge_connections(get_world_2d()->get_navigation_map());

Color debug_face_color = ns2d->get_debug_navigation_geometry_face_color();
Color debug_edge_color = ns2d->get_debug_navigation_geometry_edge_color();
Expand Down Expand Up @@ -340,6 +354,9 @@ void NavigationRegion2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &NavigationRegion2D::set_enabled);
ClassDB::bind_method(D_METHOD("is_enabled"), &NavigationRegion2D::is_enabled);

ClassDB::bind_method(D_METHOD("set_use_edge_connections", "enabled"), &NavigationRegion2D::set_use_edge_connections);
ClassDB::bind_method(D_METHOD("get_use_edge_connections"), &NavigationRegion2D::get_use_edge_connections);

ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationRegion2D::set_navigation_layers);
ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationRegion2D::get_navigation_layers);

Expand All @@ -365,6 +382,7 @@ void NavigationRegion2D::_bind_methods() {

ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "navigation_polygon", PROPERTY_HINT_RESOURCE_TYPE, "NavigationPolygon"), "set_navigation_polygon", "get_navigation_polygon");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_edge_connections"), "set_use_edge_connections", "get_use_edge_connections");
ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_2D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "enter_cost"), "set_enter_cost", "get_enter_cost");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "travel_cost"), "set_travel_cost", "get_travel_cost");
Expand Down
5 changes: 5 additions & 0 deletions scene/2d/navigation_region_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class NavigationRegion2D : public Node2D {
GDCLASS(NavigationRegion2D, Node2D);

bool enabled = true;
bool use_edge_connections = true;

RID region;
uint32_t navigation_layers = 1;
real_t enter_cost = 0.0;
Expand Down Expand Up @@ -71,6 +73,9 @@ class NavigationRegion2D : public Node2D {
void set_enabled(bool p_enabled);
bool is_enabled() const;

void set_use_edge_connections(bool p_enabled);
bool get_use_edge_connections() const;

void set_navigation_layers(uint32_t p_navigation_layers);
uint32_t get_navigation_layers() const;

Expand Down
Loading

0 comments on commit f986b52

Please sign in to comment.