Skip to content

Commit

Permalink
Remove some 3D only codepaths
Browse files Browse the repository at this point in the history
  • Loading branch information
AThousandShips committed Feb 26, 2024
1 parent ee9fcdd commit 524900e
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 21 deletions.
4 changes: 2 additions & 2 deletions modules/navigation/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,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 @@ -51,7 +51,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 Down
87 changes: 70 additions & 17 deletions modules/navigation/nav_agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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());
}

Expand All @@ -150,59 +158,77 @@ 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;
}

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;
}

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;
}

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;
}

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;
}

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 @@ -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;
}
}
Expand All @@ -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);
}
Expand All @@ -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);
}
}
Expand All @@ -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);
}
}
Expand All @@ -270,29 +308,38 @@ 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;
}

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 @@ -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;
Expand All @@ -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_);
Expand All @@ -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_);
Expand Down
11 changes: 11 additions & 0 deletions modules/navigation/nav_agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
#include "core/templates/local_vector.h"

#include <Agent2d.h>
#ifndef _3D_DISABLED
#include <Agent3d.h>
#endif

class NavMap;

Expand All @@ -60,8 +62,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;
Expand All @@ -81,16 +85,23 @@ 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; }

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;
Expand Down
Loading

0 comments on commit 524900e

Please sign in to comment.