Skip to content

Commit

Permalink
[Navigation] Restore 2D only navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
AThousandShips committed Dec 12, 2024
1 parent 3877573 commit 4852cf3
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 55 deletions.
12 changes: 2 additions & 10 deletions modules/navigation/3d/godot_navigation_server_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1147,45 +1147,37 @@ uint32_t GodotNavigationServer3D::obstacle_get_avoidance_layers(RID p_obstacle)
return obstacle->get_avoidance_layers();
}

void GodotNavigationServer3D::parse_source_geometry_data(const Ref<NavigationMesh> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, Node *p_root_node, const Callable &p_callback) {
#ifndef _3D_DISABLED
void GodotNavigationServer3D::parse_source_geometry_data(const Ref<NavigationMesh> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, Node *p_root_node, const Callable &p_callback) {
ERR_FAIL_COND_MSG(!Thread::is_main_thread(), "The SceneTree can only be parsed on the main thread. Call this function from the main thread or use call_deferred().");
ERR_FAIL_COND_MSG(!p_navigation_mesh.is_valid(), "Invalid navigation mesh.");
ERR_FAIL_NULL_MSG(p_root_node, "No parsing root node specified.");
ERR_FAIL_COND_MSG(!p_root_node->is_inside_tree(), "The root node needs to be inside the SceneTree.");

ERR_FAIL_NULL(NavMeshGenerator3D::get_singleton());
NavMeshGenerator3D::get_singleton()->parse_source_geometry_data(p_navigation_mesh, p_source_geometry_data, p_root_node, p_callback);
#endif // _3D_DISABLED
}

void GodotNavigationServer3D::bake_from_source_geometry_data(const Ref<NavigationMesh> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, const Callable &p_callback) {
#ifndef _3D_DISABLED
ERR_FAIL_COND_MSG(!p_navigation_mesh.is_valid(), "Invalid navigation mesh.");
ERR_FAIL_COND_MSG(!p_source_geometry_data.is_valid(), "Invalid NavigationMeshSourceGeometryData3D.");

ERR_FAIL_NULL(NavMeshGenerator3D::get_singleton());
NavMeshGenerator3D::get_singleton()->bake_from_source_geometry_data(p_navigation_mesh, p_source_geometry_data, p_callback);
#endif // _3D_DISABLED
}

void GodotNavigationServer3D::bake_from_source_geometry_data_async(const Ref<NavigationMesh> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, const Callable &p_callback) {
#ifndef _3D_DISABLED
ERR_FAIL_COND_MSG(!p_navigation_mesh.is_valid(), "Invalid navigation mesh.");
ERR_FAIL_COND_MSG(!p_source_geometry_data.is_valid(), "Invalid NavigationMeshSourceGeometryData3D.");

ERR_FAIL_NULL(NavMeshGenerator3D::get_singleton());
NavMeshGenerator3D::get_singleton()->bake_from_source_geometry_data_async(p_navigation_mesh, p_source_geometry_data, p_callback);
#endif // _3D_DISABLED
}

bool GodotNavigationServer3D::is_baking_navigation_mesh(Ref<NavigationMesh> p_navigation_mesh) const {
#ifdef _3D_DISABLED
return false;
#else
return NavMeshGenerator3D::get_singleton()->is_baking(p_navigation_mesh);
#endif // _3D_DISABLED
}
#endif // _3D_DISABLED

COMMAND_1(free, RID, p_object) {
if (map_owner.owns(p_object)) {
Expand Down
2 changes: 2 additions & 0 deletions modules/navigation/3d/godot_navigation_server_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,12 @@ class GodotNavigationServer3D : public NavigationServer3D {
COMMAND_2(obstacle_set_avoidance_layers, RID, p_obstacle, uint32_t, p_layers);
virtual uint32_t obstacle_get_avoidance_layers(RID p_obstacle) const override;

#ifndef _3D_DISABLED
virtual void parse_source_geometry_data(const Ref<NavigationMesh> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, Node *p_root_node, const Callable &p_callback = Callable()) override;
virtual void bake_from_source_geometry_data(const Ref<NavigationMesh> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, const Callable &p_callback = Callable()) override;
virtual void bake_from_source_geometry_data_async(const Ref<NavigationMesh> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, const Callable &p_callback = Callable()) override;
virtual bool is_baking_navigation_mesh(Ref<NavigationMesh> p_navigation_mesh) const override;
#endif // _3D_DISABLED

virtual RID source_geometry_parser_create() override;
virtual void source_geometry_parser_set_callback(RID p_parser, const Callable &p_callback) override;
Expand Down
4 changes: 0 additions & 4 deletions modules/navigation/3d/nav_mesh_queries_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef _3D_DISABLED

#include "nav_mesh_queries_3d.h"

#include "../nav_base.h"
Expand Down Expand Up @@ -976,5 +974,3 @@ void NavMeshQueries3D::simplify_path_segment(int p_start_inx, int p_end_inx, con
simplify_path_segment(point_max_index, p_end_inx, p_points, p_epsilon, r_valid_points);
}
}

#endif // _3D_DISABLED
4 changes: 0 additions & 4 deletions modules/navigation/3d/nav_mesh_queries_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
#ifndef NAV_MESH_QUERIES_3D_H
#define NAV_MESH_QUERIES_3D_H

#ifndef _3D_DISABLED

#include "../nav_utils.h"

#include "servers/navigation/navigation_path_query_parameters_3d.h"
Expand Down Expand Up @@ -120,6 +118,4 @@ class NavMeshQueries3D {
static LocalVector<uint32_t> get_simplified_path_indices(const LocalVector<Vector3> &p_path, real_t p_epsilon);
};

#endif // _3D_DISABLED

#endif // NAV_MESH_QUERIES_3D_H
7 changes: 3 additions & 4 deletions modules/navigation/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ env_navigation = env_modules.Clone()
thirdparty_obj = []

# Recast Thirdparty source files
if env["builtin_recastnavigation"]:
if not env["disable_3d"] and env["builtin_recastnavigation"]:
thirdparty_dir = "#thirdparty/recastnavigation/Recast/"
thirdparty_sources = [
"Source/Recast.cpp",
Expand Down Expand Up @@ -52,7 +52,7 @@ if env["builtin_rvo2_2d"]:
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_sources)

# RVO 3D Thirdparty source files
if env["builtin_rvo2_3d"]:
if not env["disable_3d"] and env["builtin_rvo2_3d"]:
thirdparty_dir = "#thirdparty/rvo2/rvo2_3d/"
thirdparty_sources = [
"Agent3d.cpp",
Expand All @@ -76,8 +76,7 @@ module_obj = []

env_navigation.add_source_files(module_obj, "*.cpp")
env_navigation.add_source_files(module_obj, "2d/*.cpp")
if not env["disable_3d"]:
env_navigation.add_source_files(module_obj, "3d/*.cpp")
env_navigation.add_source_files(module_obj, "3d/*.cpp")
if env.editor_build:
env_navigation.add_source_files(module_obj, "editor/*.cpp")
env.modules_sources += module_obj
Expand Down
2 changes: 1 addition & 1 deletion modules/navigation/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
def can_build(env, platform):
return not env["disable_3d"]
return True


def configure(env):
Expand Down
87 changes: 70 additions & 17 deletions modules/navigation/nav_agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ void NavAgent::set_avoidance_enabled(bool p_enabled) {
_update_rvo_agent_properties();
}

#ifndef _3D_DISABLED
void NavAgent::set_use_3d_avoidance(bool p_enabled) {
use_3d_avoidance = p_enabled;
_update_rvo_agent_properties();
}
#endif

void NavAgent::_update_rvo_agent_properties() {
#ifndef _3D_DISABLED
if (use_3d_avoidance) {
rvo_agent_3d.neighborDist_ = neighbor_distance;
rvo_agent_3d.maxNeighbors_ = max_neighbors;
Expand All @@ -58,7 +61,9 @@ void NavAgent::_update_rvo_agent_properties() {
rvo_agent_3d.avoidance_layers_ = avoidance_layers;
rvo_agent_3d.avoidance_mask_ = avoidance_mask;
rvo_agent_3d.avoidance_priority_ = avoidance_priority;
} else {
} else
#endif
{
rvo_agent_2d.neighborDist_ = neighbor_distance;
rvo_agent_2d.maxNeighbors_ = max_neighbors;
rvo_agent_2d.timeHorizon_ = time_horizon_agents;
Expand Down Expand Up @@ -137,9 +142,12 @@ void NavAgent::dispatch_avoidance_callback() {

Vector3 new_velocity;

#ifndef _3D_DISABLED
if (use_3d_avoidance) {
new_velocity = Vector3(rvo_agent_3d.velocity_.x(), rvo_agent_3d.velocity_.y(), rvo_agent_3d.velocity_.z());
} else {
} else
#endif
{
new_velocity = Vector3(rvo_agent_2d.velocity_.x(), 0.0, rvo_agent_2d.velocity_.y());
}

Expand All @@ -153,9 +161,12 @@ void NavAgent::dispatch_avoidance_callback() {

void NavAgent::set_neighbor_distance(real_t p_neighbor_distance) {
neighbor_distance = p_neighbor_distance;
#ifndef _3D_DISABLED
if (use_3d_avoidance) {
rvo_agent_3d.neighborDist_ = neighbor_distance;
} else {
} else
#endif
{
rvo_agent_2d.neighborDist_ = neighbor_distance;
}
agent_dirty = true;
Expand All @@ -165,9 +176,12 @@ void NavAgent::set_neighbor_distance(real_t p_neighbor_distance) {

void NavAgent::set_max_neighbors(int p_max_neighbors) {
max_neighbors = p_max_neighbors;
#ifndef _3D_DISABLED
if (use_3d_avoidance) {
rvo_agent_3d.maxNeighbors_ = max_neighbors;
} else {
} else
#endif
{
rvo_agent_2d.maxNeighbors_ = max_neighbors;
}
agent_dirty = true;
Expand All @@ -177,9 +191,12 @@ void NavAgent::set_max_neighbors(int p_max_neighbors) {

void NavAgent::set_time_horizon_agents(real_t p_time_horizon) {
time_horizon_agents = p_time_horizon;
#ifndef _3D_DISABLED
if (use_3d_avoidance) {
rvo_agent_3d.timeHorizon_ = time_horizon_agents;
} else {
} else
#endif
{
rvo_agent_2d.timeHorizon_ = time_horizon_agents;
}
agent_dirty = true;
Expand All @@ -189,9 +206,12 @@ void NavAgent::set_time_horizon_agents(real_t p_time_horizon) {

void NavAgent::set_time_horizon_obstacles(real_t p_time_horizon) {
time_horizon_obstacles = p_time_horizon;
#ifndef _3D_DISABLED
if (use_3d_avoidance) {
rvo_agent_3d.timeHorizonObst_ = time_horizon_obstacles;
} else {
} else
#endif
{
rvo_agent_2d.timeHorizonObst_ = time_horizon_obstacles;
}
agent_dirty = true;
Expand All @@ -201,9 +221,12 @@ void NavAgent::set_time_horizon_obstacles(real_t p_time_horizon) {

void NavAgent::set_radius(real_t p_radius) {
radius = p_radius;
#ifndef _3D_DISABLED
if (use_3d_avoidance) {
rvo_agent_3d.radius_ = radius;
} else {
} else
#endif
{
rvo_agent_2d.radius_ = radius;
}
agent_dirty = true;
Expand All @@ -213,9 +236,12 @@ void NavAgent::set_radius(real_t p_radius) {

void NavAgent::set_height(real_t p_height) {
height = p_height;
#ifndef _3D_DISABLED
if (use_3d_avoidance) {
rvo_agent_3d.height_ = height;
} else {
} else
#endif
{
rvo_agent_2d.height_ = height;
}
agent_dirty = true;
Expand All @@ -226,9 +252,12 @@ void NavAgent::set_height(real_t p_height) {
void NavAgent::set_max_speed(real_t p_max_speed) {
max_speed = p_max_speed;
if (avoidance_enabled) {
#ifndef _3D_DISABLED
if (use_3d_avoidance) {
rvo_agent_3d.maxSpeed_ = max_speed;
} else {
} else
#endif
{
rvo_agent_2d.maxSpeed_ = max_speed;
}
}
Expand All @@ -240,9 +269,12 @@ void NavAgent::set_max_speed(real_t p_max_speed) {
void NavAgent::set_position(const Vector3 p_position) {
position = p_position;
if (avoidance_enabled) {
#ifndef _3D_DISABLED
if (use_3d_avoidance) {
rvo_agent_3d.position_ = RVO3D::Vector3(p_position.x, p_position.y, p_position.z);
} else {
} else
#endif
{
rvo_agent_2d.elevation_ = p_position.y;
rvo_agent_2d.position_ = RVO2D::Vector2(p_position.x, p_position.z);
}
Expand All @@ -261,9 +293,12 @@ void NavAgent::set_velocity(const Vector3 p_velocity) {
// This velocity is not guaranteed, RVO simulation will only try to fulfill it
velocity = p_velocity;
if (avoidance_enabled) {
#ifndef _3D_DISABLED
if (use_3d_avoidance) {
rvo_agent_3d.prefVelocity_ = RVO3D::Vector3(velocity.x, velocity.y, velocity.z);
} else {
} else
#endif
{
rvo_agent_2d.prefVelocity_ = RVO2D::Vector2(velocity.x, velocity.z);
}
}
Expand All @@ -279,9 +314,12 @@ void NavAgent::set_velocity_forced(const Vector3 p_velocity) {
// use velocity instead to update with a safer "suggestion"
velocity_forced = p_velocity;
if (avoidance_enabled) {
#ifndef _3D_DISABLED
if (use_3d_avoidance) {
rvo_agent_3d.velocity_ = RVO3D::Vector3(p_velocity.x, p_velocity.y, p_velocity.z);
} else {
} else
#endif
{
rvo_agent_2d.velocity_ = RVO2D::Vector2(p_velocity.x, p_velocity.z);
}
}
Expand All @@ -293,19 +331,25 @@ void NavAgent::set_velocity_forced(const Vector3 p_velocity) {
void NavAgent::update() {
// Updates this agent with the calculated results from the rvo simulation
if (avoidance_enabled) {
#ifndef _3D_DISABLED
if (use_3d_avoidance) {
velocity = Vector3(rvo_agent_3d.velocity_.x(), rvo_agent_3d.velocity_.y(), rvo_agent_3d.velocity_.z());
} else {
} else
#endif
{
velocity = Vector3(rvo_agent_2d.velocity_.x(), 0.0, rvo_agent_2d.velocity_.y());
}
}
}

void NavAgent::set_avoidance_mask(uint32_t p_mask) {
avoidance_mask = p_mask;
#ifndef _3D_DISABLED
if (use_3d_avoidance) {
rvo_agent_3d.avoidance_mask_ = avoidance_mask;
} else {
} else
#endif
{
rvo_agent_2d.avoidance_mask_ = avoidance_mask;
}
agent_dirty = true;
Expand All @@ -315,9 +359,12 @@ void NavAgent::set_avoidance_mask(uint32_t p_mask) {

void NavAgent::set_avoidance_layers(uint32_t p_layers) {
avoidance_layers = p_layers;
#ifndef _3D_DISABLED
if (use_3d_avoidance) {
rvo_agent_3d.avoidance_layers_ = avoidance_layers;
} else {
} else
#endif
{
rvo_agent_2d.avoidance_layers_ = avoidance_layers;
}
agent_dirty = true;
Expand All @@ -329,9 +376,12 @@ void NavAgent::set_avoidance_priority(real_t p_priority) {
ERR_FAIL_COND_MSG(p_priority < 0.0, "Avoidance priority must be between 0.0 and 1.0 inclusive.");
ERR_FAIL_COND_MSG(p_priority > 1.0, "Avoidance priority must be between 0.0 and 1.0 inclusive.");
avoidance_priority = p_priority;
#ifndef _3D_DISABLED
if (use_3d_avoidance) {
rvo_agent_3d.avoidance_priority_ = avoidance_priority;
} else {
} else
#endif
{
rvo_agent_2d.avoidance_priority_ = avoidance_priority;
}
agent_dirty = true;
Expand All @@ -350,6 +400,7 @@ void NavAgent::sync() {
const Dictionary NavAgent::get_avoidance_data() const {
// Returns debug data from RVO simulation internals of this agent.
Dictionary _avoidance_data;
#ifndef _3D_DISABLED
if (use_3d_avoidance) {
_avoidance_data["max_neighbors"] = int(rvo_agent_3d.maxNeighbors_);
_avoidance_data["max_speed"] = float(rvo_agent_3d.maxSpeed_);
Expand All @@ -365,7 +416,9 @@ const Dictionary NavAgent::get_avoidance_data() const {
_avoidance_data["avoidance_layers"] = int(rvo_agent_3d.avoidance_layers_);
_avoidance_data["avoidance_mask"] = int(rvo_agent_3d.avoidance_mask_);
_avoidance_data["avoidance_priority"] = float(rvo_agent_3d.avoidance_priority_);
} else {
} else
#endif
{
_avoidance_data["max_neighbors"] = int(rvo_agent_2d.maxNeighbors_);
_avoidance_data["max_speed"] = float(rvo_agent_2d.maxSpeed_);
_avoidance_data["neighbor_distance"] = float(rvo_agent_2d.neighborDist_);
Expand Down
Loading

0 comments on commit 4852cf3

Please sign in to comment.