From 81c392489ae4aaf48e0243aef79ed16f12943964 Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 9 Jun 2023 20:00:28 +0200 Subject: [PATCH] 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 https://github.com/godotengine/godot/commit/d87f1247689ae82996aeac77b6e9870bbc88142d --- doc/classes/NavigationAgent.xml | 2 ++ doc/classes/NavigationAgent2D.xml | 2 ++ scene/2d/navigation_agent_2d.cpp | 16 ++++++++++++++++ scene/3d/navigation_agent.cpp | 16 ++++++++++++++++ 4 files changed, 36 insertions(+) diff --git a/doc/classes/NavigationAgent.xml b/doc/classes/NavigationAgent.xml index 9a378a58b..651fd92b3 100644 --- a/doc/classes/NavigationAgent.xml +++ b/doc/classes/NavigationAgent.xml @@ -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. diff --git a/doc/classes/NavigationAgent2D.xml b/doc/classes/NavigationAgent2D.xml index 10b33de72..e93e8dbe3 100644 --- a/doc/classes/NavigationAgent2D.xml +++ b/doc/classes/NavigationAgent2D.xml @@ -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. diff --git a/scene/2d/navigation_agent_2d.cpp b/scene/2d/navigation_agent_2d.cpp index 722f1d038..4b5543cef 100644 --- a/scene/2d/navigation_agent_2d.cpp +++ b/scene/2d/navigation_agent_2d.cpp @@ -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(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 diff --git a/scene/3d/navigation_agent.cpp b/scene/3d/navigation_agent.cpp index 0d7dc4968..998a47f06 100644 --- a/scene/3d/navigation_agent.cpp +++ b/scene/3d/navigation_agent.cpp @@ -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(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