diff --git a/modules/navigation/SCsub b/modules/navigation/SCsub index ecc55532cf01..e57b4bd9f363 100644 --- a/modules/navigation/SCsub +++ b/modules/navigation/SCsub @@ -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", @@ -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", diff --git a/modules/navigation/nav_agent.cpp b/modules/navigation/nav_agent.cpp index 2dbe57eb4aa6..ce4dacd4a120 100644 --- a/modules/navigation/nav_agent.cpp +++ b/modules/navigation/nav_agent.cpp @@ -40,12 +40,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; @@ -61,7 +64,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; @@ -134,9 +139,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()); } @@ -150,9 +158,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; @@ -160,9 +171,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; @@ -170,9 +184,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; @@ -180,9 +197,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; @@ -190,9 +210,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; @@ -200,9 +223,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; @@ -211,9 +237,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; } } @@ -223,9 +252,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); } @@ -242,9 +274,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); } } @@ -258,9 +293,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); } } @@ -270,9 +308,12 @@ 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()); } } @@ -280,9 +321,12 @@ void NavAgent::update() { 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; @@ -290,9 +334,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; @@ -302,9 +349,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; @@ -319,6 +369,7 @@ bool NavAgent::check_dirty() { 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_); @@ -334,7 +385,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_); diff --git a/modules/navigation/nav_agent.h b/modules/navigation/nav_agent.h index 18997803f25e..3e4dc0249020 100644 --- a/modules/navigation/nav_agent.h +++ b/modules/navigation/nav_agent.h @@ -37,7 +37,9 @@ #include "core/templates/local_vector.h" #include +#ifndef _3D_DISABLED #include +#endif class NavMap; @@ -59,8 +61,10 @@ class NavAgent : public NavRid { NavMap *map = nullptr; RVO2D::Agent2D rvo_agent_2d; +#ifndef _3D_DISABLED RVO3D::Agent3D rvo_agent_3d; bool use_3d_avoidance = false; +#endif bool avoidance_enabled = false; uint32_t avoidance_layers = 1; @@ -80,8 +84,13 @@ class NavAgent : public NavRid { void set_avoidance_enabled(bool p_enabled); bool is_avoidance_enabled() { return avoidance_enabled; } +#ifdef _3D_DISABLED + void set_use_3d_avoidance(bool p_enabled) {} + bool get_use_3d_avoidance() { return false; } +#else void set_use_3d_avoidance(bool p_enabled); bool get_use_3d_avoidance() { return use_3d_avoidance; } +#endif void set_map(NavMap *p_map); NavMap *get_map() { return map; } @@ -89,7 +98,9 @@ class NavAgent : public NavRid { bool is_map_changed(); RVO2D::Agent2D *get_rvo_agent_2d() { return &rvo_agent_2d; } +#ifndef _3D_DISABLED RVO3D::Agent3D *get_rvo_agent_3d() { return &rvo_agent_3d; } +#endif void set_avoidance_callback(Callable p_callback); bool has_avoidance_callback() const; diff --git a/modules/navigation/nav_map.cpp b/modules/navigation/nav_map.cpp index 8df1db533d94..4007c7b35f0e 100644 --- a/modules/navigation/nav_map.cpp +++ b/modules/navigation/nav_map.cpp @@ -259,13 +259,16 @@ void NavMap::set_agent_as_controlled(NavAgent *agent) { return; } +#ifndef _3D_DISABLED if (agent->get_use_3d_avoidance()) { int64_t agent_3d_index = active_3d_avoidance_agents.find(agent); if (agent_3d_index < 0) { active_3d_avoidance_agents.push_back(agent); agents_dirty = true; } - } else { + } else +#endif + { int64_t agent_2d_index = active_2d_avoidance_agents.find(agent); if (agent_2d_index < 0) { active_2d_avoidance_agents.push_back(agent); @@ -275,11 +278,13 @@ void NavMap::set_agent_as_controlled(NavAgent *agent) { } void NavMap::remove_agent_as_controlled(NavAgent *agent) { +#ifndef _3D_DISABLED int64_t agent_3d_index = active_3d_avoidance_agents.find(agent); if (agent_3d_index >= 0) { active_3d_avoidance_agents.remove_at_unordered(agent_3d_index); agents_dirty = true; } +#endif int64_t agent_2d_index = active_2d_avoidance_agents.find(agent); if (agent_2d_index >= 0) { active_2d_avoidance_agents.remove_at_unordered(agent_2d_index); @@ -774,6 +779,7 @@ void NavMap::_update_rvo_agents_tree_2d() { rvo_simulation_2d.kdTree_->buildAgentTree(raw_agents); } +#ifndef _3D_DISABLED void NavMap::_update_rvo_agents_tree_3d() { // Cannot use LocalVector here as RVO library expects std::vector to build KdTree. std::vector raw_agents; @@ -783,6 +789,7 @@ void NavMap::_update_rvo_agents_tree_3d() { } rvo_simulation_3d.kdTree_->buildAgentTree(raw_agents); } +#endif void NavMap::_update_rvo_simulation() { if (obstacles_dirty) { @@ -801,18 +808,22 @@ void NavMap::compute_single_avoidance_step_2d(uint32_t index, NavAgent **agent) (*(agent + index))->update(); } +#ifndef _3D_DISABLED void NavMap::compute_single_avoidance_step_3d(uint32_t index, NavAgent **agent) { (*(agent + index))->get_rvo_agent_3d()->computeNeighbors(&rvo_simulation_3d); (*(agent + index))->get_rvo_agent_3d()->computeNewVelocity(&rvo_simulation_3d); (*(agent + index))->get_rvo_agent_3d()->update(&rvo_simulation_3d); (*(agent + index))->update(); } +#endif void NavMap::step(real_t p_deltatime) { deltatime = p_deltatime; rvo_simulation_2d.setTimeStep(float(deltatime)); +#ifndef _3D_DISABLED rvo_simulation_3d.setTimeStep(float(deltatime)); +#endif if (active_2d_avoidance_agents.size() > 0) { if (use_threads && avoidance_use_multiple_threads) { @@ -828,6 +839,7 @@ void NavMap::step(real_t p_deltatime) { } } +#ifndef _3D_DISABLED if (active_3d_avoidance_agents.size() > 0) { if (use_threads && avoidance_use_multiple_threads) { WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_template_group_task(this, &NavMap::compute_single_avoidance_step_3d, active_3d_avoidance_agents.ptr(), active_3d_avoidance_agents.size(), -1, true, SNAME("RVOAvoidanceAgents3D")); @@ -841,6 +853,7 @@ void NavMap::step(real_t p_deltatime) { } } } +#endif } void NavMap::dispatch_callbacks() { @@ -848,9 +861,11 @@ void NavMap::dispatch_callbacks() { agent->dispatch_avoidance_callback(); } +#ifndef _3D_DISABLED for (NavAgent *agent : active_3d_avoidance_agents) { agent->dispatch_avoidance_callback(); } +#endif } void NavMap::_update_merge_rasterizer_cell_dimensions() { diff --git a/modules/navigation/nav_map.h b/modules/navigation/nav_map.h index b9120c04d9a0..158564166cc9 100644 --- a/modules/navigation/nav_map.h +++ b/modules/navigation/nav_map.h @@ -39,9 +39,11 @@ #include "servers/navigation/navigation_globals.h" #include -#include #include +#ifndef _3D_DISABLED +#include #include +#endif class NavLink; class NavRegion; @@ -87,11 +89,15 @@ class NavMap : public NavRid { /// RVO avoidance worlds RVO2D::RVOSimulator2D rvo_simulation_2d; +#ifndef _3D_DISABLED RVO3D::RVOSimulator3D rvo_simulation_3d; +#endif /// avoidance controlled agents LocalVector active_2d_avoidance_agents; +#ifndef _3D_DISABLED LocalVector active_3d_avoidance_agents; +#endif /// dirty flag when one of the agent's arrays are modified bool agents_dirty = true; @@ -230,12 +236,20 @@ class NavMap : public NavRid { void compute_single_step(uint32_t index, NavAgent **agent); void compute_single_avoidance_step_2d(uint32_t index, NavAgent **agent); +#ifdef _3D_DISABLED + void compute_single_avoidance_step_3d(uint32_t index, NavAgent **agent) {} +#else void compute_single_avoidance_step_3d(uint32_t index, NavAgent **agent); +#endif void _update_rvo_simulation(); void _update_rvo_obstacles_tree_2d(); void _update_rvo_agents_tree_2d(); +#ifdef _3D_DISABLED + void _update_rvo_agents_tree_3d() {} +#else void _update_rvo_agents_tree_3d(); +#endif void _update_merge_rasterizer_cell_dimensions(); }; diff --git a/modules/navigation/nav_obstacle.cpp b/modules/navigation/nav_obstacle.cpp index 14dfd4eae341..f36260f2fc66 100644 --- a/modules/navigation/nav_obstacle.cpp +++ b/modules/navigation/nav_obstacle.cpp @@ -58,6 +58,7 @@ void NavObstacle::set_avoidance_enabled(bool p_enabled) { internal_update_agent(); } +#ifndef _3D_DISABLED void NavObstacle::set_use_3d_avoidance(bool p_enabled) { if (use_3d_avoidance == p_enabled) { return; @@ -70,6 +71,7 @@ void NavObstacle::set_use_3d_avoidance(bool p_enabled) { agent->set_use_3d_avoidance(use_3d_avoidance); } } +#endif void NavObstacle::set_map(NavMap *p_map) { if (map == p_map) { @@ -192,7 +194,9 @@ void NavObstacle::internal_update_agent() { agent->set_position(position); agent->set_avoidance_layers(avoidance_layers); agent->set_avoidance_enabled(avoidance_enabled); +#ifndef _3D_DISABLED agent->set_use_3d_avoidance(use_3d_avoidance); +#endif } } diff --git a/modules/navigation/nav_obstacle.h b/modules/navigation/nav_obstacle.h index e231e83836c9..f1e12e68efaa 100644 --- a/modules/navigation/nav_obstacle.h +++ b/modules/navigation/nav_obstacle.h @@ -50,7 +50,9 @@ class NavObstacle : public NavRid { real_t height = 0.0; bool avoidance_enabled = false; +#ifndef _3D_DISABLED bool use_3d_avoidance = false; +#endif uint32_t avoidance_layers = 1; bool obstacle_dirty = true; @@ -65,8 +67,13 @@ class NavObstacle : public NavRid { void set_avoidance_enabled(bool p_enabled); bool is_avoidance_enabled() { return avoidance_enabled; } +#ifdef _3D_DISABLED + void set_use_3d_avoidance(bool p_enabled) {} + bool get_use_3d_avoidance() { return false; } +#else void set_use_3d_avoidance(bool p_enabled); bool get_use_3d_avoidance() { return use_3d_avoidance; } +#endif void set_map(NavMap *p_map); NavMap *get_map() { return map; }