From 097f8a5fdb9b8fc5fc455356fab851439c06f35f Mon Sep 17 00:00:00 2001 From: Josh Jones Date: Wed, 8 Feb 2023 23:18:53 -0800 Subject: [PATCH] Fix missing avoidance updates when using same velocity When using avoidance, if you set the same velocity for the agent, you won't get an update from the avoidance system. This changes both the `set_target_position` and `set_velocity` setters to always accept user provided values, even if they are the same. This ensures that repathing and avoidance logic is always run when the user expects. --- scene/2d/navigation_agent_2d.cpp | 11 +++++------ scene/3d/navigation_agent_3d.cpp | 11 +++++------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/scene/2d/navigation_agent_2d.cpp b/scene/2d/navigation_agent_2d.cpp index 85f6840fde9..f8dd7d12d4d 100644 --- a/scene/2d/navigation_agent_2d.cpp +++ b/scene/2d/navigation_agent_2d.cpp @@ -436,9 +436,8 @@ real_t NavigationAgent2D::get_path_max_distance() { } void NavigationAgent2D::set_target_position(Vector2 p_position) { - if (target_position.is_equal_approx(p_position)) { - return; - } + // Intentionally not checking for equality of the parameter, as we want to update the path even if the target position is the same in case the world changed. + // Revisit later when the navigation server can update the path without requesting a new path. target_position = p_position; target_position_submitted = true; @@ -491,9 +490,9 @@ Vector2 NavigationAgent2D::get_final_position() { } void NavigationAgent2D::set_velocity(Vector2 p_velocity) { - if (target_velocity.is_equal_approx(p_velocity)) { - return; - } + // Intentionally not checking for equality of the parameter. + // We need to always submit the velocity to the navigation server, even when it is the same, in order to run avoidance every frame. + // Revisit later when the navigation server can update avoidance without users resubmitting the velocity. target_velocity = p_velocity; velocity_submitted = true; diff --git a/scene/3d/navigation_agent_3d.cpp b/scene/3d/navigation_agent_3d.cpp index 524304425c8..bd1d05f049e 100644 --- a/scene/3d/navigation_agent_3d.cpp +++ b/scene/3d/navigation_agent_3d.cpp @@ -461,9 +461,8 @@ real_t NavigationAgent3D::get_path_max_distance() { } void NavigationAgent3D::set_target_position(Vector3 p_position) { - if (target_position.is_equal_approx(p_position)) { - return; - } + // Intentionally not checking for equality of the parameter, as we want to update the path even if the target position is the same in case the world changed. + // Revisit later when the navigation server can update the path without requesting a new path. target_position = p_position; target_position_submitted = true; @@ -516,9 +515,9 @@ Vector3 NavigationAgent3D::get_final_position() { } void NavigationAgent3D::set_velocity(Vector3 p_velocity) { - if (target_velocity.is_equal_approx(p_velocity)) { - return; - } + // Intentionally not checking for equality of the parameter. + // We need to always submit the velocity to the navigation server, even when it is the same, in order to run avoidance every frame. + // Revisit later when the navigation server can update avoidance without users resubmitting the velocity. target_velocity = p_velocity; velocity_submitted = true;