Path: blob/master/modules/navigation_3d/nav_agent_3d.cpp
10277 views
/**************************************************************************/1/* nav_agent_3d.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "nav_agent_3d.h"3132#include "nav_map_3d.h"3334void NavAgent3D::set_avoidance_enabled(bool p_enabled) {35avoidance_enabled = p_enabled;36_update_rvo_agent_properties();37}3839void NavAgent3D::set_use_3d_avoidance(bool p_enabled) {40use_3d_avoidance = p_enabled;41_update_rvo_agent_properties();42}4344void NavAgent3D::_update_rvo_agent_properties() {45if (use_3d_avoidance) {46rvo_agent_3d.neighborDist_ = neighbor_distance;47rvo_agent_3d.maxNeighbors_ = max_neighbors;48rvo_agent_3d.timeHorizon_ = time_horizon_agents;49rvo_agent_3d.timeHorizonObst_ = time_horizon_obstacles;50rvo_agent_3d.radius_ = radius;51rvo_agent_3d.maxSpeed_ = max_speed;52rvo_agent_3d.position_ = RVO3D::Vector3(position.x, position.y, position.z);53// Replacing the internal velocity directly causes major jitter / bugs due to unpredictable velocity jumps, left line here for testing.54//rvo_agent_3d.velocity_ = RVO3D::Vector3(velocity.x, velocity.y ,velocity.z);55rvo_agent_3d.prefVelocity_ = RVO3D::Vector3(velocity.x, velocity.y, velocity.z);56rvo_agent_3d.height_ = height;57rvo_agent_3d.avoidance_layers_ = avoidance_layers;58rvo_agent_3d.avoidance_mask_ = avoidance_mask;59rvo_agent_3d.avoidance_priority_ = avoidance_priority;60} else {61rvo_agent_2d.neighborDist_ = neighbor_distance;62rvo_agent_2d.maxNeighbors_ = max_neighbors;63rvo_agent_2d.timeHorizon_ = time_horizon_agents;64rvo_agent_2d.timeHorizonObst_ = time_horizon_obstacles;65rvo_agent_2d.radius_ = radius;66rvo_agent_2d.maxSpeed_ = max_speed;67rvo_agent_2d.position_ = RVO2D::Vector2(position.x, position.z);68rvo_agent_2d.elevation_ = position.y;69// Replacing the internal velocity directly causes major jitter / bugs due to unpredictable velocity jumps, left line here for testing.70//rvo_agent_2d.velocity_ = RVO2D::Vector2(velocity.x, velocity.z);71rvo_agent_2d.prefVelocity_ = RVO2D::Vector2(velocity.x, velocity.z);72rvo_agent_2d.height_ = height;73rvo_agent_2d.avoidance_layers_ = avoidance_layers;74rvo_agent_2d.avoidance_mask_ = avoidance_mask;75rvo_agent_2d.avoidance_priority_ = avoidance_priority;76}7778if (map != nullptr) {79if (avoidance_enabled) {80map->set_agent_as_controlled(this);81} else {82map->remove_agent_as_controlled(this);83}84}85agent_dirty = true;8687request_sync();88}8990void NavAgent3D::set_map(NavMap3D *p_map) {91if (map == p_map) {92return;93}9495cancel_sync_request();9697if (map) {98map->remove_agent(this);99}100101map = p_map;102agent_dirty = true;103104if (map) {105map->add_agent(this);106if (avoidance_enabled) {107map->set_agent_as_controlled(this);108}109110request_sync();111}112}113114bool NavAgent3D::is_map_changed() {115if (map) {116bool is_changed = map->get_iteration_id() != last_map_iteration_id;117last_map_iteration_id = map->get_iteration_id();118return is_changed;119} else {120return false;121}122}123124void NavAgent3D::set_avoidance_callback(Callable p_callback) {125avoidance_callback = p_callback;126}127128bool NavAgent3D::has_avoidance_callback() const {129return avoidance_callback.is_valid();130}131132void NavAgent3D::dispatch_avoidance_callback() {133if (!avoidance_callback.is_valid()) {134return;135}136137Vector3 new_velocity;138139if (use_3d_avoidance) {140new_velocity = Vector3(rvo_agent_3d.velocity_.x(), rvo_agent_3d.velocity_.y(), rvo_agent_3d.velocity_.z());141} else {142new_velocity = Vector3(rvo_agent_2d.velocity_.x(), 0.0, rvo_agent_2d.velocity_.y());143}144145if (clamp_speed) {146new_velocity = new_velocity.limit_length(max_speed);147}148149// Invoke the callback with the new velocity.150avoidance_callback.call(new_velocity);151}152153void NavAgent3D::set_neighbor_distance(real_t p_neighbor_distance) {154neighbor_distance = p_neighbor_distance;155if (use_3d_avoidance) {156rvo_agent_3d.neighborDist_ = neighbor_distance;157} else {158rvo_agent_2d.neighborDist_ = neighbor_distance;159}160agent_dirty = true;161162request_sync();163}164165void NavAgent3D::set_max_neighbors(int p_max_neighbors) {166max_neighbors = p_max_neighbors;167if (use_3d_avoidance) {168rvo_agent_3d.maxNeighbors_ = max_neighbors;169} else {170rvo_agent_2d.maxNeighbors_ = max_neighbors;171}172agent_dirty = true;173174request_sync();175}176177void NavAgent3D::set_time_horizon_agents(real_t p_time_horizon) {178time_horizon_agents = p_time_horizon;179if (use_3d_avoidance) {180rvo_agent_3d.timeHorizon_ = time_horizon_agents;181} else {182rvo_agent_2d.timeHorizon_ = time_horizon_agents;183}184agent_dirty = true;185186request_sync();187}188189void NavAgent3D::set_time_horizon_obstacles(real_t p_time_horizon) {190time_horizon_obstacles = p_time_horizon;191if (use_3d_avoidance) {192rvo_agent_3d.timeHorizonObst_ = time_horizon_obstacles;193} else {194rvo_agent_2d.timeHorizonObst_ = time_horizon_obstacles;195}196agent_dirty = true;197198request_sync();199}200201void NavAgent3D::set_radius(real_t p_radius) {202radius = p_radius;203if (use_3d_avoidance) {204rvo_agent_3d.radius_ = radius;205} else {206rvo_agent_2d.radius_ = radius;207}208agent_dirty = true;209210request_sync();211}212213void NavAgent3D::set_height(real_t p_height) {214height = p_height;215if (use_3d_avoidance) {216rvo_agent_3d.height_ = height;217} else {218rvo_agent_2d.height_ = height;219}220agent_dirty = true;221222request_sync();223}224225void NavAgent3D::set_max_speed(real_t p_max_speed) {226max_speed = p_max_speed;227if (avoidance_enabled) {228if (use_3d_avoidance) {229rvo_agent_3d.maxSpeed_ = max_speed;230} else {231rvo_agent_2d.maxSpeed_ = max_speed;232}233}234agent_dirty = true;235236request_sync();237}238239void NavAgent3D::set_position(const Vector3 p_position) {240position = p_position;241if (avoidance_enabled) {242if (use_3d_avoidance) {243rvo_agent_3d.position_ = RVO3D::Vector3(p_position.x, p_position.y, p_position.z);244} else {245rvo_agent_2d.elevation_ = p_position.y;246rvo_agent_2d.position_ = RVO2D::Vector2(p_position.x, p_position.z);247}248}249agent_dirty = true;250251request_sync();252}253254void NavAgent3D::set_target_position(const Vector3 p_target_position) {255target_position = p_target_position;256}257258void NavAgent3D::set_velocity(const Vector3 p_velocity) {259// Sets the "wanted" velocity for an agent as a suggestion260// This velocity is not guaranteed, RVO simulation will only try to fulfill it261velocity = p_velocity;262if (avoidance_enabled) {263if (use_3d_avoidance) {264rvo_agent_3d.prefVelocity_ = RVO3D::Vector3(velocity.x, velocity.y, velocity.z);265} else {266rvo_agent_2d.prefVelocity_ = RVO2D::Vector2(velocity.x, velocity.z);267}268}269agent_dirty = true;270271request_sync();272}273274void NavAgent3D::set_velocity_forced(const Vector3 p_velocity) {275// This function replaces the internal rvo simulation velocity276// should only be used after the agent was teleported277// as it destroys consistency in movement in cramped situations278// use velocity instead to update with a safer "suggestion"279velocity_forced = p_velocity;280if (avoidance_enabled) {281if (use_3d_avoidance) {282rvo_agent_3d.velocity_ = RVO3D::Vector3(p_velocity.x, p_velocity.y, p_velocity.z);283} else {284rvo_agent_2d.velocity_ = RVO2D::Vector2(p_velocity.x, p_velocity.z);285}286}287agent_dirty = true;288289request_sync();290}291292void NavAgent3D::update() {293// Updates this agent with the calculated results from the rvo simulation294if (avoidance_enabled) {295if (use_3d_avoidance) {296velocity = Vector3(rvo_agent_3d.velocity_.x(), rvo_agent_3d.velocity_.y(), rvo_agent_3d.velocity_.z());297} else {298velocity = Vector3(rvo_agent_2d.velocity_.x(), 0.0, rvo_agent_2d.velocity_.y());299}300}301}302303void NavAgent3D::set_avoidance_mask(uint32_t p_mask) {304avoidance_mask = p_mask;305if (use_3d_avoidance) {306rvo_agent_3d.avoidance_mask_ = avoidance_mask;307} else {308rvo_agent_2d.avoidance_mask_ = avoidance_mask;309}310agent_dirty = true;311312request_sync();313}314315void NavAgent3D::set_avoidance_layers(uint32_t p_layers) {316avoidance_layers = p_layers;317if (use_3d_avoidance) {318rvo_agent_3d.avoidance_layers_ = avoidance_layers;319} else {320rvo_agent_2d.avoidance_layers_ = avoidance_layers;321}322agent_dirty = true;323324request_sync();325}326327void NavAgent3D::set_avoidance_priority(real_t p_priority) {328ERR_FAIL_COND_MSG(p_priority < 0.0, "Avoidance priority must be between 0.0 and 1.0 inclusive.");329ERR_FAIL_COND_MSG(p_priority > 1.0, "Avoidance priority must be between 0.0 and 1.0 inclusive.");330avoidance_priority = p_priority;331if (use_3d_avoidance) {332rvo_agent_3d.avoidance_priority_ = avoidance_priority;333} else {334rvo_agent_2d.avoidance_priority_ = avoidance_priority;335}336agent_dirty = true;337338request_sync();339}340341bool NavAgent3D::is_dirty() const {342return agent_dirty;343}344345void NavAgent3D::sync() {346agent_dirty = false;347}348349const Dictionary NavAgent3D::get_avoidance_data() const {350// Returns debug data from RVO simulation internals of this agent.351Dictionary _avoidance_data;352if (use_3d_avoidance) {353_avoidance_data["max_neighbors"] = int(rvo_agent_3d.maxNeighbors_);354_avoidance_data["max_speed"] = float(rvo_agent_3d.maxSpeed_);355_avoidance_data["neighbor_distance"] = float(rvo_agent_3d.neighborDist_);356_avoidance_data["new_velocity"] = Vector3(rvo_agent_3d.newVelocity_.x(), rvo_agent_3d.newVelocity_.y(), rvo_agent_3d.newVelocity_.z());357_avoidance_data["velocity"] = Vector3(rvo_agent_3d.velocity_.x(), rvo_agent_3d.velocity_.y(), rvo_agent_3d.velocity_.z());358_avoidance_data["position"] = Vector3(rvo_agent_3d.position_.x(), rvo_agent_3d.position_.y(), rvo_agent_3d.position_.z());359_avoidance_data["preferred_velocity"] = Vector3(rvo_agent_3d.prefVelocity_.x(), rvo_agent_3d.prefVelocity_.y(), rvo_agent_3d.prefVelocity_.z());360_avoidance_data["radius"] = float(rvo_agent_3d.radius_);361_avoidance_data["time_horizon_agents"] = float(rvo_agent_3d.timeHorizon_);362_avoidance_data["time_horizon_obstacles"] = 0.0;363_avoidance_data["height"] = float(rvo_agent_3d.height_);364_avoidance_data["avoidance_layers"] = int(rvo_agent_3d.avoidance_layers_);365_avoidance_data["avoidance_mask"] = int(rvo_agent_3d.avoidance_mask_);366_avoidance_data["avoidance_priority"] = float(rvo_agent_3d.avoidance_priority_);367} else {368_avoidance_data["max_neighbors"] = int(rvo_agent_2d.maxNeighbors_);369_avoidance_data["max_speed"] = float(rvo_agent_2d.maxSpeed_);370_avoidance_data["neighbor_distance"] = float(rvo_agent_2d.neighborDist_);371_avoidance_data["new_velocity"] = Vector3(rvo_agent_2d.newVelocity_.x(), 0.0, rvo_agent_2d.newVelocity_.y());372_avoidance_data["velocity"] = Vector3(rvo_agent_2d.velocity_.x(), 0.0, rvo_agent_2d.velocity_.y());373_avoidance_data["position"] = Vector3(rvo_agent_2d.position_.x(), 0.0, rvo_agent_2d.position_.y());374_avoidance_data["preferred_velocity"] = Vector3(rvo_agent_2d.prefVelocity_.x(), 0.0, rvo_agent_2d.prefVelocity_.y());375_avoidance_data["radius"] = float(rvo_agent_2d.radius_);376_avoidance_data["time_horizon_agents"] = float(rvo_agent_2d.timeHorizon_);377_avoidance_data["time_horizon_obstacles"] = float(rvo_agent_2d.timeHorizonObst_);378_avoidance_data["height"] = float(rvo_agent_2d.height_);379_avoidance_data["avoidance_layers"] = int(rvo_agent_2d.avoidance_layers_);380_avoidance_data["avoidance_mask"] = int(rvo_agent_2d.avoidance_mask_);381_avoidance_data["avoidance_priority"] = float(rvo_agent_2d.avoidance_priority_);382}383return _avoidance_data;384}385386void NavAgent3D::set_paused(bool p_paused) {387if (paused == p_paused) {388return;389}390391paused = p_paused;392393if (map) {394if (paused) {395map->remove_agent_as_controlled(this);396} else {397map->set_agent_as_controlled(this);398}399}400}401402bool NavAgent3D::get_paused() const {403return paused;404}405406void NavAgent3D::request_sync() {407if (map && !sync_dirty_request_list_element.in_list()) {408map->add_agent_sync_dirty_request(&sync_dirty_request_list_element);409}410}411412void NavAgent3D::cancel_sync_request() {413if (map && sync_dirty_request_list_element.in_list()) {414map->remove_agent_sync_dirty_request(&sync_dirty_request_list_element);415}416}417418NavAgent3D::NavAgent3D() :419sync_dirty_request_list_element(this) {420}421422NavAgent3D::~NavAgent3D() {423cancel_sync_request();424}425426427