Ported from godot4: Add NavigationLink helper functions for global positions

Adds helper functions to set the links start and end position with global positions or get them as global positions.
Adds global start and end position for the navigation link to the 'link_reached' signal of NavigationAgent. That signal gets emitted when a navigation link waypoint is reached. Requires that 'owner' meta data is enabled on the NavigationAgent.
- smix8
d87f124768
This commit is contained in:
Relintai 2023-06-09 20:00:28 +02:00
parent 29784d5d5b
commit 81c392489a
4 changed files with 36 additions and 0 deletions

View File

@ -187,6 +187,8 @@
- [code]type[/code]: Always [constant NavigationPathQueryResult3D.PATH_SEGMENT_TYPE_LINK].
- [code]rid[/code]: The [RID] of the link.
- [code]owner[/code]: The object which manages the link (usually [NavigationLink3D]).
- [code]link_entry_position[/code]: If [code]owner[/code] is available and the owner is a [NavigationLink2D], it will contain the global position of the link's point the agent is entering.
- [code]link_exit_position[/code]: If [code]owner[/code] is available and the owner is a [NavigationLink2D], it will contain the global position of the link's point which the agent is exiting.
</description>
</signal>
<signal name="navigation_finished">

View File

@ -184,6 +184,8 @@
- [code]type[/code]: Always [constant NavigationPathQueryResult2D.PATH_SEGMENT_TYPE_LINK].
- [code]rid[/code]: The [RID] of the link.
- [code]owner[/code]: The object which manages the link (usually [NavigationLink2D]).
- [code]link_entry_position[/code]: If [code]owner[/code] is available and the owner is a [NavigationLink2D], it will contain the global position of the link's point the agent is entering.
- [code]link_exit_position[/code]: If [code]owner[/code] is available and the owner is a [NavigationLink2D], it will contain the global position of the link's point which the agent is exiting.
</description>
</signal>
<signal name="navigation_finished">

View File

@ -33,6 +33,7 @@
#include "core/config/engine.h"
#include "core/containers/vector.h"
#include "scene/2d/navigation_2d.h"
#include "scene/2d/navigation_link_2d.h"
#include "scene/resources/world_2d.h"
#include "servers/navigation/navigation_path_query_parameters_2d.h"
#include "servers/navigation/navigation_path_query_result_2d.h"
@ -630,6 +631,21 @@ void NavigationAgent2D::update_navigation() {
}
details["owner"] = owner;
if (waypoint_type == NavigationPathQueryResult2D::PATH_SEGMENT_TYPE_LINK) {
const NavigationLink2D *navlink = Object::cast_to<NavigationLink2D>(owner);
if (navlink) {
Vector2 link_global_start_position = navlink->get_global_start_position();
Vector2 link_global_end_position = navlink->get_global_end_position();
if (waypoint.distance_to(link_global_start_position) < waypoint.distance_to(link_global_end_position)) {
details["link_entry_position"] = link_global_start_position;
details["link_exit_position"] = link_global_end_position;
} else {
details["link_entry_position"] = link_global_end_position;
details["link_exit_position"] = link_global_start_position;
}
}
}
}
// Emit a signal for the waypoint

View File

@ -33,6 +33,7 @@
#include "core/config/engine.h"
#include "core/math/geometry.h"
#include "scene/3d/navigation.h"
#include "scene/3d/navigation_link_3d.h"
#include "scene/resources/material.h"
#include "scene/resources/mesh.h"
#include "scene/resources/world_3d.h"
@ -653,6 +654,21 @@ void NavigationAgent::update_navigation() {
}
details["owner"] = owner;
if (waypoint_type == NavigationPathQueryResult3D::PATH_SEGMENT_TYPE_LINK) {
const NavigationLink3D *navlink = Object::cast_to<NavigationLink3D>(owner);
if (navlink) {
Vector3 link_global_start_position = navlink->get_global_start_position();
Vector3 link_global_end_position = navlink->get_global_end_position();
if (waypoint.distance_to(link_global_start_position) < waypoint.distance_to(link_global_end_position)) {
details["link_entry_position"] = link_global_start_position;
details["link_exit_position"] = link_global_end_position;
} else {
details["link_entry_position"] = link_global_end_position;
details["link_exit_position"] = link_global_start_position;
}
}
}
}
// Emit a signal for the waypoint