Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update NavigationObstacle API #78081

Merged
merged 1 commit into from
Jun 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions doc/classes/NavigationObstacle2D.xml
Original file line number Diff line number Diff line change
@@ -13,12 +13,6 @@
<link title="Using NavigationObstacles">$DOCS_URL/tutorials/navigation/navigation_using_navigationobstacles.html</link>
</tutorials>
<methods>
<method name="get_agent_rid" qualifiers="const">
<return type="RID" />
<description>
Returns the [RID] of this agent on the [NavigationServer2D]. This [RID] is used for the moving avoidance "obstacle" component (using a fake avoidance agent) which size is defined by [member radius] and velocity set by using [member velocity].
</description>
</method>
<method name="get_avoidance_layer_value" qualifiers="const">
<return type="bool" />
<param index="0" name="layer_number" type="int" />
@@ -32,10 +26,10 @@
Returns the [RID] of the navigation map for this NavigationObstacle node. This function returns always the map set on the NavigationObstacle node and not the map of the abstract obstacle on the NavigationServer. If the obstacle map is changed directly with the NavigationServer API the NavigationObstacle node will not be aware of the map change. Use [method set_navigation_map] to change the navigation map for the NavigationObstacle and also update the obstacle on the NavigationServer.
</description>
</method>
<method name="get_obstacle_rid" qualifiers="const">
<method name="get_rid" qualifiers="const">
<return type="RID" />
<description>
Returns the [RID] of this obstacle on the [NavigationServer2D]. This [RID] is used for the static avoidance obstacle component which shape is defined by [member vertices].
Returns the [RID] of this obstacle on the [NavigationServer2D].
</description>
</method>
<method name="set_avoidance_layer_value">
@@ -55,6 +49,9 @@
</method>
</methods>
<members>
<member name="avoidance_enabled" type="bool" setter="set_avoidance_enabled" getter="get_avoidance_enabled" default="true">
If [code]true[/code] the obstacle affects avoidance using agents.
</member>
<member name="avoidance_layers" type="int" setter="set_avoidance_layers" getter="get_avoidance_layers" default="1">
A bitfield determining the avoidance layers for this obstacle. Agent's with a matching bit on the their avoidance mask will avoid this obstacle.
</member>
13 changes: 5 additions & 8 deletions doc/classes/NavigationObstacle3D.xml
Original file line number Diff line number Diff line change
@@ -13,12 +13,6 @@
<link title="Using NavigationObstacles">$DOCS_URL/tutorials/navigation/navigation_using_navigationobstacles.html</link>
</tutorials>
<methods>
<method name="get_agent_rid" qualifiers="const">
<return type="RID" />
<description>
Returns the [RID] of this agent on the [NavigationServer3D]. This [RID] is used for the moving avoidance "obstacle" component (using a fake avoidance agent) which size is defined by [member radius] and velocity set by using [member velocity].
</description>
</method>
<method name="get_avoidance_layer_value" qualifiers="const">
<return type="bool" />
<param index="0" name="layer_number" type="int" />
@@ -32,10 +26,10 @@
Returns the [RID] of the navigation map for this NavigationObstacle node. This function returns always the map set on the NavigationObstacle node and not the map of the abstract obstacle on the NavigationServer. If the obstacle map is changed directly with the NavigationServer API the NavigationObstacle node will not be aware of the map change. Use [method set_navigation_map] to change the navigation map for the NavigationObstacle and also update the obstacle on the NavigationServer.
</description>
</method>
<method name="get_obstacle_rid" qualifiers="const">
<method name="get_rid" qualifiers="const">
<return type="RID" />
<description>
Returns the [RID] of this obstacle on the [NavigationServer3D]. This [RID] is used for the static avoidance obstacle component which shape is defined by [member vertices].
Returns the [RID] of this obstacle on the [NavigationServer3D].
</description>
</method>
<method name="set_avoidance_layer_value">
@@ -55,6 +49,9 @@
</method>
</methods>
<members>
<member name="avoidance_enabled" type="bool" setter="set_avoidance_enabled" getter="get_avoidance_enabled" default="true">
If [code]true[/code] the obstacle affects avoidance using agents.
</member>
<member name="avoidance_layers" type="int" setter="set_avoidance_layers" getter="get_avoidance_layers" default="1">
A bitfield determining the avoidance layers for this obstacle. Agent's with a matching bit on the their avoidance mask will avoid this obstacle.
</member>
32 changes: 32 additions & 0 deletions doc/classes/NavigationServer2D.xml
Original file line number Diff line number Diff line change
@@ -464,18 +464,34 @@
Creates a new navigation obstacle.
</description>
</method>
<method name="obstacle_get_avoidance_enabled" qualifiers="const">
<return type="bool" />
<param index="0" name="obstacle" type="RID" />
<description>
Returns [code]true[/code] if the provided [param obstacle] has avoidance enabled.
</description>
</method>
<method name="obstacle_get_map" qualifiers="const">
<return type="RID" />
<param index="0" name="obstacle" type="RID" />
<description>
Returns the navigation map [RID] the requested [param obstacle] is currently assigned to.
</description>
</method>
<method name="obstacle_set_avoidance_enabled">
<return type="void" />
<param index="0" name="obstacle" type="RID" />
<param index="1" name="enabled" type="bool" />
<description>
If [param enabled] the provided [param obstacle] affects avoidance using agents.
</description>
</method>
<method name="obstacle_set_avoidance_layers">
<return type="void" />
<param index="0" name="obstacle" type="RID" />
<param index="1" name="layers" type="int" />
<description>
Set the obstacles's [code]avoidance_layers[/code] bitmask.
</description>
</method>
<method name="obstacle_set_map">
@@ -494,6 +510,22 @@
Sets the position of the obstacle in world space.
</description>
</method>
<method name="obstacle_set_radius">
<return type="void" />
<param index="0" name="obstacle" type="RID" />
<param index="1" name="radius" type="float" />
<description>
Sets the radius of the dynamic obstacle.
</description>
</method>
<method name="obstacle_set_velocity">
<return type="void" />
<param index="0" name="obstacle" type="RID" />
<param index="1" name="velocity" type="Vector2" />
<description>
Sets [param velocity] of the dynamic [param obstacle]. Allows other agents to better predict the movement of the dynamic obstacle. Only works in combination with the radius of the obstacle.
</description>
</method>
<method name="obstacle_set_vertices">
<return type="void" />
<param index="0" name="obstacle" type="RID" />
46 changes: 46 additions & 0 deletions doc/classes/NavigationServer3D.xml
Original file line number Diff line number Diff line change
@@ -529,13 +529,35 @@
Creates a new obstacle.
</description>
</method>
<method name="obstacle_get_avoidance_enabled" qualifiers="const">
<return type="bool" />
<param index="0" name="obstacle" type="RID" />
<description>
Returns [code]true[/code] if the provided [param obstacle] has avoidance enabled.
</description>
</method>
<method name="obstacle_get_map" qualifiers="const">
<return type="RID" />
<param index="0" name="obstacle" type="RID" />
<description>
Returns the navigation map [RID] the requested [param obstacle] is currently assigned to.
</description>
</method>
<method name="obstacle_get_use_3d_avoidance" qualifiers="const">
<return type="bool" />
<param index="0" name="obstacle" type="RID" />
<description>
Returns [code]true[/code] if the provided [param obstacle] uses avoidance in 3D space Vector3(x,y,z) instead of horizontal 2D Vector2(x,y) / Vector3(x,0.0,z).
</description>
</method>
<method name="obstacle_set_avoidance_enabled">
<return type="void" />
<param index="0" name="obstacle" type="RID" />
<param index="1" name="enabled" type="bool" />
<description>
If [param enabled] the provided [param obstacle] affects avoidance using agents.
</description>
</method>
<method name="obstacle_set_avoidance_layers">
<return type="void" />
<param index="0" name="obstacle" type="RID" />
@@ -568,6 +590,30 @@
Updates the [param position] in world space for the [param obstacle].
</description>
</method>
<method name="obstacle_set_radius">
<return type="void" />
<param index="0" name="obstacle" type="RID" />
<param index="1" name="radius" type="float" />
<description>
Sets the radius of the dynamic obstacle.
</description>
</method>
<method name="obstacle_set_use_3d_avoidance">
<return type="void" />
<param index="0" name="obstacle" type="RID" />
<param index="1" name="enabled" type="bool" />
<description>
Sets if the [param obstacle] uses the 2D avoidance or the 3D avoidance while avoidance is enabled.
</description>
</method>
<method name="obstacle_set_velocity">
<return type="void" />
<param index="0" name="obstacle" type="RID" />
<param index="1" name="velocity" type="Vector3" />
<description>
Sets [param velocity] of the dynamic [param obstacle]. Allows other agents to better predict the movement of the dynamic obstacle. Only works in combination with the radius of the obstacle.
</description>
</method>
<method name="obstacle_set_vertices">
<return type="void" />
<param index="0" name="obstacle" type="RID" />
2 changes: 0 additions & 2 deletions misc/extension_api_validation/4.0-stable.expected
Original file line number Diff line number Diff line change
@@ -85,11 +85,9 @@ Validate extension JSON: API was removed: classes/NavigationAgent3D/methods/set_
Validate extension JSON: API was removed: classes/NavigationAgent3D/properties/agent_height_offset
Validate extension JSON: API was removed: classes/NavigationAgent3D/properties/ignore_y
Validate extension JSON: API was removed: classes/NavigationAgent3D/properties/time_horizon
Validate extension JSON: API was removed: classes/NavigationObstacle2D/methods/get_rid
Validate extension JSON: API was removed: classes/NavigationObstacle2D/methods/is_radius_estimated
Validate extension JSON: API was removed: classes/NavigationObstacle2D/methods/set_estimate_radius
Validate extension JSON: API was removed: classes/NavigationObstacle2D/properties/estimate_radius
Validate extension JSON: API was removed: classes/NavigationObstacle3D/methods/get_rid
Validate extension JSON: API was removed: classes/NavigationObstacle3D/methods/is_radius_estimated
Validate extension JSON: API was removed: classes/NavigationObstacle3D/methods/set_estimate_radius
Validate extension JSON: API was removed: classes/NavigationObstacle3D/properties/estimate_radius
85 changes: 74 additions & 11 deletions modules/navigation/godot_navigation_server.cpp
Original file line number Diff line number Diff line change
@@ -802,9 +802,44 @@ RID GodotNavigationServer::obstacle_create() {
RID rid = obstacle_owner.make_rid();
NavObstacle *obstacle = obstacle_owner.get_or_null(rid);
obstacle->set_self(rid);

RID agent_rid = agent_owner.make_rid();
NavAgent *agent = agent_owner.get_or_null(agent_rid);
agent->set_self(agent_rid);

obstacle->set_agent(agent);

return rid;
}

COMMAND_2(obstacle_set_avoidance_enabled, RID, p_obstacle, bool, p_enabled) {
NavObstacle *obstacle = obstacle_owner.get_or_null(p_obstacle);
ERR_FAIL_COND(obstacle == nullptr);

obstacle->set_avoidance_enabled(p_enabled);
}

bool GodotNavigationServer::obstacle_get_avoidance_enabled(RID p_obstacle) const {
NavObstacle *obstacle = obstacle_owner.get_or_null(p_obstacle);
ERR_FAIL_COND_V(obstacle == nullptr, false);

return obstacle->is_avoidance_enabled();
}

COMMAND_2(obstacle_set_use_3d_avoidance, RID, p_obstacle, bool, p_enabled) {
NavObstacle *obstacle = obstacle_owner.get_or_null(p_obstacle);
ERR_FAIL_COND(obstacle == nullptr);

obstacle->set_use_3d_avoidance(p_enabled);
}

bool GodotNavigationServer::obstacle_get_use_3d_avoidance(RID p_obstacle) const {
NavObstacle *obstacle = obstacle_owner.get_or_null(p_obstacle);
ERR_FAIL_COND_V(obstacle == nullptr, false);

return obstacle->get_use_3d_avoidance();
}

COMMAND_2(obstacle_set_map, RID, p_obstacle, RID, p_map) {
NavObstacle *obstacle = obstacle_owner.get_or_null(p_obstacle);
ERR_FAIL_COND(obstacle == nullptr);
@@ -837,12 +872,27 @@ RID GodotNavigationServer::obstacle_get_map(RID p_obstacle) const {
return RID();
}

COMMAND_2(obstacle_set_radius, RID, p_obstacle, real_t, p_radius) {
ERR_FAIL_COND_MSG(p_radius < 0.0, "Radius must be positive.");
NavObstacle *obstacle = obstacle_owner.get_or_null(p_obstacle);
ERR_FAIL_COND(obstacle == nullptr);

obstacle->set_radius(p_radius);
}

COMMAND_2(obstacle_set_height, RID, p_obstacle, real_t, p_height) {
NavObstacle *obstacle = obstacle_owner.get_or_null(p_obstacle);
ERR_FAIL_COND(obstacle == nullptr);
obstacle->set_height(p_height);
}

COMMAND_2(obstacle_set_velocity, RID, p_obstacle, Vector3, p_velocity) {
NavObstacle *obstacle = obstacle_owner.get_or_null(p_obstacle);
ERR_FAIL_COND(obstacle == nullptr);

obstacle->set_velocity(p_velocity);
}

COMMAND_2(obstacle_set_position, RID, p_obstacle, Vector3, p_position) {
NavObstacle *obstacle = obstacle_owner.get_or_null(p_obstacle);
ERR_FAIL_COND(obstacle == nullptr);
@@ -917,29 +967,42 @@ COMMAND_1(free, RID, p_object) {
link_owner.free(p_object);

} else if (agent_owner.owns(p_object)) {
NavAgent *agent = agent_owner.get_or_null(p_object);
internal_free_agent(p_object);

} else if (obstacle_owner.owns(p_object)) {
internal_free_obstacle(p_object);

} else {
ERR_PRINT("Attempted to free a NavigationServer RID that did not exist (or was already freed).");
}
}

// Removes this agent from the map if assigned
void GodotNavigationServer::internal_free_agent(RID p_object) {
NavAgent *agent = agent_owner.get_or_null(p_object);
if (agent) {
if (agent->get_map() != nullptr) {
agent->get_map()->remove_agent(agent);
agent->set_map(nullptr);
}

agent_owner.free(p_object);
}
}

} else if (obstacle_owner.owns(p_object)) {
NavObstacle *obstacle = obstacle_owner.get_or_null(p_object);

// Removes this agent from the map if assigned
void GodotNavigationServer::internal_free_obstacle(RID p_object) {
NavObstacle *obstacle = obstacle_owner.get_or_null(p_object);
if (obstacle) {
if (obstacle->get_map() != nullptr) {
obstacle->get_map()->remove_obstacle(obstacle);
obstacle->set_map(nullptr);
}

if (obstacle->get_agent()) {
if (obstacle->get_agent()->get_self() != RID()) {
RID _agent_rid = obstacle->get_agent()->get_self();
obstacle->set_agent(nullptr);
internal_free_agent(_agent_rid);
}
}
obstacle_owner.free(p_object);

} else {
ERR_PRINT("Attempted to free a NavigationServer RID that did not exist (or was already freed).");
}
}

10 changes: 10 additions & 0 deletions modules/navigation/godot_navigation_server.h
Original file line number Diff line number Diff line change
@@ -198,8 +198,14 @@ class GodotNavigationServer : public NavigationServer3D {
COMMAND_2(agent_set_avoidance_priority, RID, p_agent, real_t, p_priority);

virtual RID obstacle_create() override;
COMMAND_2(obstacle_set_avoidance_enabled, RID, p_obstacle, bool, p_enabled);
virtual bool obstacle_get_avoidance_enabled(RID p_obstacle) const override;
COMMAND_2(obstacle_set_use_3d_avoidance, RID, p_obstacle, bool, p_enabled);
virtual bool obstacle_get_use_3d_avoidance(RID p_obstacle) const override;
COMMAND_2(obstacle_set_map, RID, p_obstacle, RID, p_map);
virtual RID obstacle_get_map(RID p_obstacle) const override;
COMMAND_2(obstacle_set_radius, RID, p_obstacle, real_t, p_radius);
COMMAND_2(obstacle_set_velocity, RID, p_obstacle, Vector3, p_velocity);
COMMAND_2(obstacle_set_position, RID, p_obstacle, Vector3, p_position);
COMMAND_2(obstacle_set_height, RID, p_obstacle, real_t, p_height);
virtual void obstacle_set_vertices(RID p_obstacle, const Vector<Vector3> &p_vertices) override;
@@ -215,6 +221,10 @@ class GodotNavigationServer : public NavigationServer3D {
virtual NavigationUtilities::PathQueryResult _query_path(const NavigationUtilities::PathQueryParameters &p_parameters) const override;

int get_process_info(ProcessInfo p_info) const override;

private:
void internal_free_agent(RID p_object);
void internal_free_obstacle(RID p_object);
};

#undef COMMAND_1
Loading