Skip to content

Commit 287fdb1

Browse files
committed
Add navigation region point and segment queries
Adds point and segment queries for regions, e.g. closet point, point normal, or segment intersection.
1 parent 2124995 commit 287fdb1

14 files changed

+105
-6
lines changed

doc/classes/NavigationServer2D.xml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,15 +486,15 @@
486486
<param index="0" name="map" type="RID" />
487487
<param index="1" name="to_point" type="Vector2" />
488488
<description>
489-
Returns the point closest to the provided [param to_point] on the navigation mesh surface.
489+
Returns the navigation mesh surface point closest to the provided [param to_point] on the navigation [param map].
490490
</description>
491491
</method>
492492
<method name="map_get_closest_point_owner" qualifiers="const">
493493
<return type="RID" />
494494
<param index="0" name="map" type="RID" />
495495
<param index="1" name="to_point" type="Vector2" />
496496
<description>
497-
Returns the owner region RID for the point returned by [method map_get_closest_point].
497+
Returns the owner region RID for the navigation mesh surface point closest to the provided [param to_point] on the navigation [param map].
498498
</description>
499499
</method>
500500
<method name="map_get_edge_connection_margin" qualifiers="const">
@@ -768,6 +768,14 @@
768768
Creates a new region.
769769
</description>
770770
</method>
771+
<method name="region_get_closest_point" qualifiers="const">
772+
<return type="Vector2" />
773+
<param index="0" name="region" type="RID" />
774+
<param index="1" name="to_point" type="Vector2" />
775+
<description>
776+
Returns the navigation mesh surface point closest to the provided [param to_point] on the navigation [param region].
777+
</description>
778+
</method>
771779
<method name="region_get_connection_pathway_end" qualifiers="const">
772780
<return type="Vector2" />
773781
<param index="0" name="region" type="RID" />

doc/classes/NavigationServer3D.xml

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -532,23 +532,23 @@
532532
<param index="0" name="map" type="RID" />
533533
<param index="1" name="to_point" type="Vector3" />
534534
<description>
535-
Returns the point closest to the provided [param to_point] on the navigation mesh surface.
535+
Returns the navigation mesh surface point closest to the provided [param to_point] on the navigation [param map].
536536
</description>
537537
</method>
538538
<method name="map_get_closest_point_normal" qualifiers="const">
539539
<return type="Vector3" />
540540
<param index="0" name="map" type="RID" />
541541
<param index="1" name="to_point" type="Vector3" />
542542
<description>
543-
Returns the normal for the point returned by [method map_get_closest_point].
543+
Returns the navigation mesh surface normal closest to the provided [param to_point] on the navigation [param map].
544544
</description>
545545
</method>
546546
<method name="map_get_closest_point_owner" qualifiers="const">
547547
<return type="RID" />
548548
<param index="0" name="map" type="RID" />
549549
<param index="1" name="to_point" type="Vector3" />
550550
<description>
551-
Returns the owner region RID for the point returned by [method map_get_closest_point].
551+
Returns the owner region RID for the navigation mesh surface point closest to the provided [param to_point] on the navigation [param map].
552552
</description>
553553
</method>
554554
<method name="map_get_closest_point_to_segment" qualifiers="const">
@@ -558,7 +558,8 @@
558558
<param index="2" name="end" type="Vector3" />
559559
<param index="3" name="use_collision" type="bool" default="false" />
560560
<description>
561-
Returns the closest point between the navigation surface and the segment.
561+
Returns the navigation mesh surface point closest to the provided [param start] and [param end] segment on the navigation [param map].
562+
If [param use_collision] is [code]true[/code], a closest point test is only done when the segment intersects with the navigation mesh surface.
562563
</description>
563564
</method>
564565
<method name="map_get_edge_connection_margin" qualifiers="const">
@@ -908,6 +909,33 @@
908909
Creates a new region.
909910
</description>
910911
</method>
912+
<method name="region_get_closest_point" qualifiers="const">
913+
<return type="Vector3" />
914+
<param index="0" name="region" type="RID" />
915+
<param index="1" name="to_point" type="Vector3" />
916+
<description>
917+
Returns the navigation mesh surface point closest to the provided [param to_point] on the navigation [param region].
918+
</description>
919+
</method>
920+
<method name="region_get_closest_point_normal" qualifiers="const">
921+
<return type="Vector3" />
922+
<param index="0" name="region" type="RID" />
923+
<param index="1" name="to_point" type="Vector3" />
924+
<description>
925+
Returns the navigation mesh surface normal closest to the provided [param to_point] on the navigation [param region].
926+
</description>
927+
</method>
928+
<method name="region_get_closest_point_to_segment" qualifiers="const">
929+
<return type="Vector3" />
930+
<param index="0" name="region" type="RID" />
931+
<param index="1" name="start" type="Vector3" />
932+
<param index="2" name="end" type="Vector3" />
933+
<param index="3" name="use_collision" type="bool" default="false" />
934+
<description>
935+
Returns the navigation mesh surface point closest to the provided [param start] and [param end] segment on the navigation [param region].
936+
If [param use_collision] is [code]true[/code], a closest point test is only done when the segment intersects with the navigation mesh surface.
937+
</description>
938+
</method>
911939
<method name="region_get_connection_pathway_end" qualifiers="const">
912940
<return type="Vector3" />
913941
<param index="0" name="region" type="RID" />

modules/navigation/2d/godot_navigation_server_2d.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,11 @@ int FORWARD_1_C(region_get_connections_count, RID, p_region, rid_to_rid);
318318
Vector2 FORWARD_2_R_C(v3_to_v2, region_get_connection_pathway_start, RID, p_region, int, p_connection_id, rid_to_rid, int_to_int);
319319
Vector2 FORWARD_2_R_C(v3_to_v2, region_get_connection_pathway_end, RID, p_region, int, p_connection_id, rid_to_rid, int_to_int);
320320

321+
Vector2 GodotNavigationServer2D::region_get_closest_point(RID p_region, const Vector2 &p_point) const {
322+
Vector3 result = NavigationServer3D::get_singleton()->region_get_closest_point(p_region, v2_to_v3(p_point));
323+
return v3_to_v2(result);
324+
}
325+
321326
Vector2 GodotNavigationServer2D::region_get_random_point(RID p_region, uint32_t p_navigation_layers, bool p_uniformly) const {
322327
Vector3 result = NavigationServer3D::get_singleton()->region_get_random_point(p_region, p_navigation_layers, p_uniformly);
323328
return v3_to_v2(result);

modules/navigation/2d/godot_navigation_server_2d.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class GodotNavigationServer2D : public NavigationServer2D {
101101
virtual int region_get_connections_count(RID p_region) const override;
102102
virtual Vector2 region_get_connection_pathway_start(RID p_region, int p_connection_id) const override;
103103
virtual Vector2 region_get_connection_pathway_end(RID p_region, int p_connection_id) const override;
104+
virtual Vector2 region_get_closest_point(RID p_region, const Vector2 &p_point) const override;
104105
virtual Vector2 region_get_random_point(RID p_region, uint32_t p_navigation_layers, bool p_uniformly) const override;
105106

106107
virtual RID link_create() override;

modules/navigation/3d/godot_navigation_server_3d.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,27 @@ Vector3 GodotNavigationServer3D::region_get_connection_pathway_end(RID p_region,
536536
return Vector3();
537537
}
538538

539+
Vector3 GodotNavigationServer3D::region_get_closest_point_to_segment(RID p_region, const Vector3 &p_from, const Vector3 &p_to, bool p_use_collision) const {
540+
const NavRegion *region = region_owner.get_or_null(p_region);
541+
ERR_FAIL_NULL_V(region, Vector3());
542+
543+
return region->get_closest_point_to_segment(p_from, p_to, p_use_collision);
544+
}
545+
546+
Vector3 GodotNavigationServer3D::region_get_closest_point(RID p_region, const Vector3 &p_point) const {
547+
const NavRegion *region = region_owner.get_or_null(p_region);
548+
ERR_FAIL_NULL_V(region, Vector3());
549+
550+
return region->get_closest_point_info(p_point).point;
551+
}
552+
553+
Vector3 GodotNavigationServer3D::region_get_closest_point_normal(RID p_region, const Vector3 &p_point) const {
554+
const NavRegion *region = region_owner.get_or_null(p_region);
555+
ERR_FAIL_NULL_V(region, Vector3());
556+
557+
return region->get_closest_point_info(p_point).normal;
558+
}
559+
539560
Vector3 GodotNavigationServer3D::region_get_random_point(RID p_region, uint32_t p_navigation_layers, bool p_uniformly) const {
540561
const NavRegion *region = region_owner.get_or_null(p_region);
541562
ERR_FAIL_NULL_V(region, Vector3());

modules/navigation/3d/godot_navigation_server_3d.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ class GodotNavigationServer3D : public NavigationServer3D {
178178
virtual int region_get_connections_count(RID p_region) const override;
179179
virtual Vector3 region_get_connection_pathway_start(RID p_region, int p_connection_id) const override;
180180
virtual Vector3 region_get_connection_pathway_end(RID p_region, int p_connection_id) const override;
181+
virtual Vector3 region_get_closest_point_to_segment(RID p_region, const Vector3 &p_from, const Vector3 &p_to, bool p_use_collision = false) const override;
182+
virtual Vector3 region_get_closest_point(RID p_region, const Vector3 &p_point) const override;
183+
virtual Vector3 region_get_closest_point_normal(RID p_region, const Vector3 &p_point) const override;
181184
virtual Vector3 region_get_random_point(RID p_region, uint32_t p_navigation_layers, bool p_uniformly) const override;
182185

183186
virtual RID link_create() override;

modules/navigation/nav_region.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,22 @@ void NavRegion::set_navigation_mesh(Ref<NavigationMesh> p_navigation_mesh) {
105105
polygons_dirty = true;
106106
}
107107

108+
Vector3 NavRegion::get_closest_point_to_segment(const Vector3 &p_from, const Vector3 &p_to, bool p_use_collision) const {
109+
RWLockRead read_lock(region_rwlock);
110+
111+
return NavMeshQueries3D::polygons_get_closest_point_to_segment(
112+
get_polygons(), p_from, p_to, p_use_collision);
113+
}
114+
115+
gd::ClosestPointQueryResult NavRegion::get_closest_point_info(const Vector3 &p_point) const {
116+
RWLockRead read_lock(region_rwlock);
117+
118+
return NavMeshQueries3D::polygons_get_closest_point_info(get_polygons(), p_point);
119+
}
120+
108121
Vector3 NavRegion::get_random_point(uint32_t p_navigation_layers, bool p_uniformly) const {
122+
RWLockRead read_lock(region_rwlock);
123+
109124
if (!get_enabled()) {
110125
return Vector3();
111126
}
@@ -114,6 +129,8 @@ Vector3 NavRegion::get_random_point(uint32_t p_navigation_layers, bool p_uniform
114129
}
115130

116131
bool NavRegion::sync() {
132+
RWLockWrite write_lock(region_rwlock);
133+
117134
bool something_changed = polygons_dirty /* || something_dirty? */;
118135

119136
update_polygons();

modules/navigation/nav_region.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
#include "scene/resources/navigation_mesh.h"
3939

4040
class NavRegion : public NavBase {
41+
RWLock region_rwlock;
42+
4143
NavMap *map = nullptr;
4244
Transform3D transform;
4345
bool enabled = true;
@@ -88,6 +90,8 @@ class NavRegion : public NavBase {
8890
return polygons;
8991
}
9092

93+
Vector3 get_closest_point_to_segment(const Vector3 &p_from, const Vector3 &p_to, bool p_use_collision) const;
94+
gd::ClosestPointQueryResult get_closest_point_info(const Vector3 &p_point) const;
9195
Vector3 get_random_point(uint32_t p_navigation_layers, bool p_uniformly) const;
9296

9397
real_t get_surface_area() const { return surface_area; };

servers/navigation_server_2d.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ void NavigationServer2D::_bind_methods() {
8686
ClassDB::bind_method(D_METHOD("region_get_connections_count", "region"), &NavigationServer2D::region_get_connections_count);
8787
ClassDB::bind_method(D_METHOD("region_get_connection_pathway_start", "region", "connection"), &NavigationServer2D::region_get_connection_pathway_start);
8888
ClassDB::bind_method(D_METHOD("region_get_connection_pathway_end", "region", "connection"), &NavigationServer2D::region_get_connection_pathway_end);
89+
ClassDB::bind_method(D_METHOD("region_get_closest_point", "region", "to_point"), &NavigationServer2D::region_get_closest_point);
8990
ClassDB::bind_method(D_METHOD("region_get_random_point", "region", "navigation_layers", "uniformly"), &NavigationServer2D::region_get_random_point);
9091

9192
ClassDB::bind_method(D_METHOD("link_create"), &NavigationServer2D::link_create);

servers/navigation_server_2d.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ class NavigationServer2D : public Object {
149149
virtual Vector2 region_get_connection_pathway_start(RID p_region, int p_connection_id) const = 0;
150150
virtual Vector2 region_get_connection_pathway_end(RID p_region, int p_connection_id) const = 0;
151151

152+
virtual Vector2 region_get_closest_point(RID p_region, const Vector2 &p_point) const = 0;
152153
virtual Vector2 region_get_random_point(RID p_region, uint32_t p_navigation_layers, bool p_uniformly) const = 0;
153154

154155
/// Creates a new link between positions in the nav map.

0 commit comments

Comments
 (0)