Merge pull request #65453 from timothyqiu/agent-target-3.x

[3.x] Make NavigationAgent `target_location` a property
This commit is contained in:
Rémi Verschelde 2022-12-12 16:07:25 +01:00
commit 2c714f19e5
No known key found for this signature in database
GPG Key ID: C3336907360768E1
4 changed files with 20 additions and 36 deletions

View File

@ -5,7 +5,7 @@
</brief_description> </brief_description>
<description> <description>
3D agent that is used in navigation to reach a location while avoiding static and dynamic obstacles. The dynamic obstacles are avoided using RVO (Reciprocal Velocity Obstacles) collision avoidance. The agent needs navigation data to work correctly. By default this node will register to the default [World] navigation map. If this node is a child of a [Navigation] node it will register to the navigation map of the navigation node or the function [method set_navigation] can be used to set the navigation node directly. [NavigationAgent] is physics safe. 3D agent that is used in navigation to reach a location while avoiding static and dynamic obstacles. The dynamic obstacles are avoided using RVO (Reciprocal Velocity Obstacles) collision avoidance. The agent needs navigation data to work correctly. By default this node will register to the default [World] navigation map. If this node is a child of a [Navigation] node it will register to the navigation map of the navigation node or the function [method set_navigation] can be used to set the navigation node directly. [NavigationAgent] is physics safe.
[b]Note:[/b] After [method set_target_location] is used it is required to use the [method get_next_location] function once every physics frame to update the internal path logic of the NavigationAgent. The returned vector position from this function should be used as the next movement position for the agent's parent Node. [b]Note:[/b] After setting [member target_location] it is required to use the [method get_next_location] function once every physics frame to update the internal path logic of the NavigationAgent. The returned vector position from this function should be used as the next movement position for the agent's parent Node.
[b]Note:[/b] By default, the expensive calculations for avoidance are done in a thread. In HTML5 exports without thread support, they will be done on the main thread, which can lead to performance issues. [b]Note:[/b] By default, the expensive calculations for avoidance are done in a thread. In HTML5 exports without thread support, they will be done on the main thread, which can lead to performance issues.
</description> </description>
<tutorials> <tutorials>
@ -14,7 +14,7 @@
<method name="distance_to_target" qualifiers="const"> <method name="distance_to_target" qualifiers="const">
<return type="float" /> <return type="float" />
<description> <description>
Returns the distance to the target location, using the agent's global position. The user must set the target location with [method set_target_location] in order for this to be accurate. Returns the distance to the target location, using the agent's global position. The user must set [member target_location] in order for this to be accurate.
</description> </description>
</method> </method>
<method name="get_final_location"> <method name="get_final_location">
@ -59,12 +59,6 @@
Returns the [RID] of this agent on the [NavigationServer]. Returns the [RID] of this agent on the [NavigationServer].
</description> </description>
</method> </method>
<method name="get_target_location" qualifiers="const">
<return type="Vector3" />
<description>
Returns the user-defined target location (set with [method set_target_location]).
</description>
</method>
<method name="is_navigation_finished"> <method name="is_navigation_finished">
<return type="bool" /> <return type="bool" />
<description> <description>
@ -74,13 +68,13 @@
<method name="is_target_reachable"> <method name="is_target_reachable">
<return type="bool" /> <return type="bool" />
<description> <description>
Returns [code]true[/code] if the target location is reachable. The target location is set using [method set_target_location]. Returns [code]true[/code] if [member target_location] is reachable.
</description> </description>
</method> </method>
<method name="is_target_reached" qualifiers="const"> <method name="is_target_reached" qualifiers="const">
<return type="bool" /> <return type="bool" />
<description> <description>
Returns [code]true[/code] if the target location is reached. The target location is set using [method set_target_location]. It may not always be possible to reach the target location. It should always be possible to reach the final location though. See [method get_final_location]. Returns [code]true[/code] if [member target_location] is reached. It may not always be possible to reach the target location. It should always be possible to reach the final location though. See [method get_final_location].
</description> </description>
</method> </method>
<method name="set_navigation"> <method name="set_navigation">
@ -97,13 +91,6 @@
Sets the [RID] of the navigation map this NavigationAgent node should use and also updates the [code]agent[/code] on the NavigationServer. Sets the [RID] of the navigation map this NavigationAgent node should use and also updates the [code]agent[/code] on the NavigationServer.
</description> </description>
</method> </method>
<method name="set_target_location">
<return type="void" />
<argument index="0" name="location" type="Vector3" />
<description>
Sets the user desired final location. This will clear the current navigation path.
</description>
</method>
<method name="set_velocity"> <method name="set_velocity">
<return type="void" /> <return type="void" />
<argument index="0" name="velocity" type="Vector3" /> <argument index="0" name="velocity" type="Vector3" />
@ -147,6 +134,9 @@
<member name="target_desired_distance" type="float" setter="set_target_desired_distance" getter="get_target_desired_distance" default="1.0"> <member name="target_desired_distance" type="float" setter="set_target_desired_distance" getter="get_target_desired_distance" default="1.0">
The distance threshold before the final target point is considered to be reached. This will allow an agent to not have to hit the point of the final target exactly, but only the area. If this value is set to low the NavigationAgent will be stuck in a repath loop cause it will constantly overshoot or undershoot the distance to the final target point on each physics frame update. The distance threshold before the final target point is considered to be reached. This will allow an agent to not have to hit the point of the final target exactly, but only the area. If this value is set to low the NavigationAgent will be stuck in a repath loop cause it will constantly overshoot or undershoot the distance to the final target point on each physics frame update.
</member> </member>
<member name="target_location" type="Vector3" setter="set_target_location" getter="get_target_location" default="Vector3( 0, 0, 0 )">
The user-defined target location. Setting this property will clear the current navigation path.
</member>
<member name="time_horizon" type="float" setter="set_time_horizon" getter="get_time_horizon" default="5.0"> <member name="time_horizon" type="float" setter="set_time_horizon" getter="get_time_horizon" default="5.0">
The minimal amount of time for which this agent's velocities, that are computed with the collision avoidance algorithm, are safe with respect to other agents. The larger the number, the sooner the agent will respond to other agents, but the less freedom in choosing its velocities. Must be positive. The minimal amount of time for which this agent's velocities, that are computed with the collision avoidance algorithm, are safe with respect to other agents. The larger the number, the sooner the agent will respond to other agents, but the less freedom in choosing its velocities. Must be positive.
</member> </member>
@ -164,7 +154,7 @@
</signal> </signal>
<signal name="target_reached"> <signal name="target_reached">
<description> <description>
Notifies when the player-defined target, set with [method set_target_location], is reached. Notifies when the player-defined [member target_location] is reached.
</description> </description>
</signal> </signal>
<signal name="velocity_computed"> <signal name="velocity_computed">

View File

@ -5,7 +5,7 @@
</brief_description> </brief_description>
<description> <description>
2D agent that is used in navigation to reach a location while avoiding static and dynamic obstacles. The dynamic obstacles are avoided using RVO (Reciprocal Velocity Obstacles) collision avoidance. The agent needs navigation data to work correctly. By default this node will register to the default [World2D] navigation map. If this node is a child of a [Navigation2D] node it will register to the navigation map of the navigation node or the function [method set_navigation] can be used to set the navigation node directly. [NavigationAgent2D] is physics safe. 2D agent that is used in navigation to reach a location while avoiding static and dynamic obstacles. The dynamic obstacles are avoided using RVO (Reciprocal Velocity Obstacles) collision avoidance. The agent needs navigation data to work correctly. By default this node will register to the default [World2D] navigation map. If this node is a child of a [Navigation2D] node it will register to the navigation map of the navigation node or the function [method set_navigation] can be used to set the navigation node directly. [NavigationAgent2D] is physics safe.
[b]Note:[/b] After [method set_target_location] is used it is required to use the [method get_next_location] function once every physics frame to update the internal path logic of the NavigationAgent. The returned vector position from this function should be used as the next movement position for the agent's parent Node. [b]Note:[/b] After setting [member target_location] it is required to use the [method get_next_location] function once every physics frame to update the internal path logic of the NavigationAgent. The returned vector position from this function should be used as the next movement position for the agent's parent Node.
[b]Note:[/b] By default, the expensive calculations for avoidance are done in a thread. In HTML5 exports without thread support, they will be done on the main thread, which can lead to performance issues. [b]Note:[/b] By default, the expensive calculations for avoidance are done in a thread. In HTML5 exports without thread support, they will be done on the main thread, which can lead to performance issues.
</description> </description>
<tutorials> <tutorials>
@ -14,7 +14,7 @@
<method name="distance_to_target" qualifiers="const"> <method name="distance_to_target" qualifiers="const">
<return type="float" /> <return type="float" />
<description> <description>
Returns the distance to the target location, using the agent's global position. The user must set the target location with [method set_target_location] in order for this to be accurate. Returns the distance to the target location, using the agent's global position. The user must set [member target_location] in order for this to be accurate.
</description> </description>
</method> </method>
<method name="get_final_location"> <method name="get_final_location">
@ -59,12 +59,6 @@
Returns the [RID] of this agent on the [Navigation2DServer]. Returns the [RID] of this agent on the [Navigation2DServer].
</description> </description>
</method> </method>
<method name="get_target_location" qualifiers="const">
<return type="Vector2" />
<description>
Returns the user-defined target location (set with [method set_target_location]).
</description>
</method>
<method name="is_navigation_finished"> <method name="is_navigation_finished">
<return type="bool" /> <return type="bool" />
<description> <description>
@ -74,13 +68,13 @@
<method name="is_target_reachable"> <method name="is_target_reachable">
<return type="bool" /> <return type="bool" />
<description> <description>
Returns [code]true[/code] if the target location is reachable. The target location is set using [method set_target_location]. Returns [code]true[/code] if [member target_location] is reachable.
</description> </description>
</method> </method>
<method name="is_target_reached" qualifiers="const"> <method name="is_target_reached" qualifiers="const">
<return type="bool" /> <return type="bool" />
<description> <description>
Returns [code]true[/code] if the target location is reached. The target location is set using [method set_target_location]. It may not always be possible to reach the target location. It should always be possible to reach the final location though. See [method get_final_location]. Returns [code]true[/code] if [member target_location] is reached. It may not always be possible to reach the target location. It should always be possible to reach the final location though. See [method get_final_location].
</description> </description>
</method> </method>
<method name="set_navigation"> <method name="set_navigation">
@ -97,13 +91,6 @@
Sets the [RID] of the navigation map this NavigationAgent node should use and also updates the [code]agent[/code] on the NavigationServer. Sets the [RID] of the navigation map this NavigationAgent node should use and also updates the [code]agent[/code] on the NavigationServer.
</description> </description>
</method> </method>
<method name="set_target_location">
<return type="void" />
<argument index="0" name="location" type="Vector2" />
<description>
Sets the user desired final location. This will clear the current navigation path.
</description>
</method>
<method name="set_velocity"> <method name="set_velocity">
<return type="void" /> <return type="void" />
<argument index="0" name="velocity" type="Vector2" /> <argument index="0" name="velocity" type="Vector2" />
@ -141,6 +128,9 @@
<member name="target_desired_distance" type="float" setter="set_target_desired_distance" getter="get_target_desired_distance" default="1.0"> <member name="target_desired_distance" type="float" setter="set_target_desired_distance" getter="get_target_desired_distance" default="1.0">
The distance threshold before the final target point is considered to be reached. This will allow an agent to not have to hit the point of the final target exactly, but only the area. If this value is set to low the NavigationAgent will be stuck in a repath loop cause it will constantly overshoot or undershoot the distance to the final target point on each physics frame update. The distance threshold before the final target point is considered to be reached. This will allow an agent to not have to hit the point of the final target exactly, but only the area. If this value is set to low the NavigationAgent will be stuck in a repath loop cause it will constantly overshoot or undershoot the distance to the final target point on each physics frame update.
</member> </member>
<member name="target_location" type="Vector2" setter="set_target_location" getter="get_target_location" default="Vector2( 0, 0 )">
The user-defined target location. Setting this property will clear the current navigation path.
</member>
<member name="time_horizon" type="float" setter="set_time_horizon" getter="get_time_horizon" default="20.0"> <member name="time_horizon" type="float" setter="set_time_horizon" getter="get_time_horizon" default="20.0">
The minimal amount of time for which this agent's velocities, that are computed with the collision avoidance algorithm, are safe with respect to other agents. The larger the number, the sooner the agent will respond to other agents, but the less freedom in choosing its velocities. Must be positive. The minimal amount of time for which this agent's velocities, that are computed with the collision avoidance algorithm, are safe with respect to other agents. The larger the number, the sooner the agent will respond to other agents, but the less freedom in choosing its velocities. Must be positive.
</member> </member>
@ -158,7 +148,7 @@
</signal> </signal>
<signal name="target_reached"> <signal name="target_reached">
<description> <description>
Notifies when the player-defined target, set with [method set_target_location], is reached. Notifies when the player-defined [member target_location] is reached.
</description> </description>
</signal> </signal>
<signal name="velocity_computed"> <signal name="velocity_computed">

View File

@ -75,6 +75,7 @@ void NavigationAgent2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_target_location", "location"), &NavigationAgent2D::set_target_location); ClassDB::bind_method(D_METHOD("set_target_location", "location"), &NavigationAgent2D::set_target_location);
ClassDB::bind_method(D_METHOD("get_target_location"), &NavigationAgent2D::get_target_location); ClassDB::bind_method(D_METHOD("get_target_location"), &NavigationAgent2D::get_target_location);
ClassDB::bind_method(D_METHOD("get_next_location"), &NavigationAgent2D::get_next_location); ClassDB::bind_method(D_METHOD("get_next_location"), &NavigationAgent2D::get_next_location);
ClassDB::bind_method(D_METHOD("distance_to_target"), &NavigationAgent2D::distance_to_target); ClassDB::bind_method(D_METHOD("distance_to_target"), &NavigationAgent2D::distance_to_target);
ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &NavigationAgent2D::set_velocity); ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &NavigationAgent2D::set_velocity);
@ -88,6 +89,7 @@ void NavigationAgent2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("_avoidance_done", "new_velocity"), &NavigationAgent2D::_avoidance_done); ClassDB::bind_method(D_METHOD("_avoidance_done", "new_velocity"), &NavigationAgent2D::_avoidance_done);
ADD_GROUP("Pathfinding", ""); ADD_GROUP("Pathfinding", "");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "target_location", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_target_location", "get_target_location");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "path_desired_distance", PROPERTY_HINT_RANGE, "0.1,100,0.01"), "set_path_desired_distance", "get_path_desired_distance"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "path_desired_distance", PROPERTY_HINT_RANGE, "0.1,100,0.01"), "set_path_desired_distance", "get_path_desired_distance");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "target_desired_distance", PROPERTY_HINT_RANGE, "0.1,100,0.01"), "set_target_desired_distance", "get_target_desired_distance"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "target_desired_distance", PROPERTY_HINT_RANGE, "0.1,100,0.01"), "set_target_desired_distance", "get_target_desired_distance");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "path_max_distance", PROPERTY_HINT_RANGE, "10,100,1"), "set_path_max_distance", "get_path_max_distance"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "path_max_distance", PROPERTY_HINT_RANGE, "10,100,1"), "set_path_max_distance", "get_path_max_distance");

View File

@ -81,6 +81,7 @@ void NavigationAgent::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_target_location", "location"), &NavigationAgent::set_target_location); ClassDB::bind_method(D_METHOD("set_target_location", "location"), &NavigationAgent::set_target_location);
ClassDB::bind_method(D_METHOD("get_target_location"), &NavigationAgent::get_target_location); ClassDB::bind_method(D_METHOD("get_target_location"), &NavigationAgent::get_target_location);
ClassDB::bind_method(D_METHOD("get_next_location"), &NavigationAgent::get_next_location); ClassDB::bind_method(D_METHOD("get_next_location"), &NavigationAgent::get_next_location);
ClassDB::bind_method(D_METHOD("distance_to_target"), &NavigationAgent::distance_to_target); ClassDB::bind_method(D_METHOD("distance_to_target"), &NavigationAgent::distance_to_target);
ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &NavigationAgent::set_velocity); ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &NavigationAgent::set_velocity);
@ -94,6 +95,7 @@ void NavigationAgent::_bind_methods() {
ClassDB::bind_method(D_METHOD("_avoidance_done", "new_velocity"), &NavigationAgent::_avoidance_done); ClassDB::bind_method(D_METHOD("_avoidance_done", "new_velocity"), &NavigationAgent::_avoidance_done);
ADD_GROUP("Pathfinding", ""); ADD_GROUP("Pathfinding", "");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "target_location", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_target_location", "get_target_location");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "path_desired_distance", PROPERTY_HINT_RANGE, "0.1,100,0.01"), "set_path_desired_distance", "get_path_desired_distance"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "path_desired_distance", PROPERTY_HINT_RANGE, "0.1,100,0.01"), "set_path_desired_distance", "get_path_desired_distance");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "target_desired_distance", PROPERTY_HINT_RANGE, "0.1,100,0.01"), "set_target_desired_distance", "get_target_desired_distance"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "target_desired_distance", PROPERTY_HINT_RANGE, "0.1,100,0.01"), "set_target_desired_distance", "get_target_desired_distance");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "agent_height_offset", PROPERTY_HINT_RANGE, "-100.0,100,0.01"), "set_agent_height_offset", "get_agent_height_offset"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "agent_height_offset", PROPERTY_HINT_RANGE, "-100.0,100,0.01"), "set_agent_height_offset", "get_agent_height_offset");