Path: blob/master/modules/navigation_2d/nav_agent_2d.cpp
10277 views
/**************************************************************************/1/* nav_agent_2d.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_2d.h"3132#include "nav_map_2d.h"3334void NavAgent2D::set_avoidance_enabled(bool p_enabled) {35avoidance_enabled = p_enabled;36_update_rvo_agent_properties();37}3839void NavAgent2D::_update_rvo_agent_properties() {40rvo_agent.neighborDist_ = neighbor_distance;41rvo_agent.maxNeighbors_ = max_neighbors;42rvo_agent.timeHorizon_ = time_horizon_agents;43rvo_agent.timeHorizonObst_ = time_horizon_obstacles;44rvo_agent.radius_ = radius;45rvo_agent.maxSpeed_ = max_speed;46rvo_agent.position_ = RVO2D::Vector2(position.x, position.y);47// Replacing the internal velocity directly causes major jitter / bugs due to unpredictable velocity jumps, left line here for testing.48//rvo_agent.velocity_ = RVO2D::Vector2(velocity.x, velocity.y);49rvo_agent.prefVelocity_ = RVO2D::Vector2(velocity.x, velocity.y);50rvo_agent.avoidance_layers_ = avoidance_layers;51rvo_agent.avoidance_mask_ = avoidance_mask;52rvo_agent.avoidance_priority_ = avoidance_priority;5354if (map != nullptr) {55if (avoidance_enabled) {56map->set_agent_as_controlled(this);57} else {58map->remove_agent_as_controlled(this);59}60}61agent_dirty = true;6263request_sync();64}6566void NavAgent2D::set_map(NavMap2D *p_map) {67if (map == p_map) {68return;69}7071cancel_sync_request();7273if (map) {74map->remove_agent(this);75}7677map = p_map;78agent_dirty = true;7980if (map) {81map->add_agent(this);82if (avoidance_enabled) {83map->set_agent_as_controlled(this);84}8586request_sync();87}88}8990bool NavAgent2D::is_map_changed() {91if (map) {92bool is_changed = map->get_iteration_id() != last_map_iteration_id;93last_map_iteration_id = map->get_iteration_id();94return is_changed;95} else {96return false;97}98}99100void NavAgent2D::set_avoidance_callback(Callable p_callback) {101avoidance_callback = p_callback;102}103104bool NavAgent2D::has_avoidance_callback() const {105return avoidance_callback.is_valid();106}107108void NavAgent2D::dispatch_avoidance_callback() {109if (!avoidance_callback.is_valid()) {110return;111}112113Vector2 new_velocity;114115new_velocity = Vector2(rvo_agent.velocity_.x(), rvo_agent.velocity_.y());116117if (clamp_speed) {118new_velocity = new_velocity.limit_length(max_speed);119}120121// Invoke the callback with the new velocity.122avoidance_callback.call(new_velocity);123}124125void NavAgent2D::set_neighbor_distance(real_t p_neighbor_distance) {126neighbor_distance = p_neighbor_distance;127rvo_agent.neighborDist_ = neighbor_distance;128agent_dirty = true;129130request_sync();131}132133void NavAgent2D::set_max_neighbors(int p_max_neighbors) {134max_neighbors = p_max_neighbors;135rvo_agent.maxNeighbors_ = max_neighbors;136agent_dirty = true;137138request_sync();139}140141void NavAgent2D::set_time_horizon_agents(real_t p_time_horizon) {142time_horizon_agents = p_time_horizon;143rvo_agent.timeHorizon_ = time_horizon_agents;144agent_dirty = true;145146request_sync();147}148149void NavAgent2D::set_time_horizon_obstacles(real_t p_time_horizon) {150time_horizon_obstacles = p_time_horizon;151rvo_agent.timeHorizonObst_ = time_horizon_obstacles;152agent_dirty = true;153154request_sync();155}156157void NavAgent2D::set_radius(real_t p_radius) {158radius = p_radius;159rvo_agent.radius_ = radius;160agent_dirty = true;161162request_sync();163}164165void NavAgent2D::set_max_speed(real_t p_max_speed) {166max_speed = p_max_speed;167if (avoidance_enabled) {168rvo_agent.maxSpeed_ = max_speed;169}170agent_dirty = true;171172request_sync();173}174175void NavAgent2D::set_position(const Vector2 &p_position) {176position = p_position;177if (avoidance_enabled) {178rvo_agent.position_ = RVO2D::Vector2(p_position.x, p_position.y);179}180agent_dirty = true;181182request_sync();183}184185void NavAgent2D::set_target_position(const Vector2 &p_target_position) {186target_position = p_target_position;187}188189void NavAgent2D::set_velocity(const Vector2 &p_velocity) {190// Sets the "wanted" velocity for an agent as a suggestion191// This velocity is not guaranteed, RVO simulation will only try to fulfill it192velocity = p_velocity;193if (avoidance_enabled) {194rvo_agent.prefVelocity_ = RVO2D::Vector2(velocity.x, velocity.y);195}196agent_dirty = true;197198request_sync();199}200201void NavAgent2D::set_velocity_forced(const Vector2 &p_velocity) {202// This function replaces the internal rvo simulation velocity203// should only be used after the agent was teleported204// as it destroys consistency in movement in cramped situations205// use velocity instead to update with a safer "suggestion"206velocity_forced = p_velocity;207if (avoidance_enabled) {208rvo_agent.velocity_ = RVO2D::Vector2(p_velocity.x, p_velocity.y);209}210agent_dirty = true;211212request_sync();213}214215void NavAgent2D::update() {216// Updates this agent with the calculated results from the rvo simulation217if (avoidance_enabled) {218velocity = Vector2(rvo_agent.velocity_.x(), rvo_agent.velocity_.y());219}220}221222void NavAgent2D::set_avoidance_mask(uint32_t p_mask) {223avoidance_mask = p_mask;224rvo_agent.avoidance_mask_ = avoidance_mask;225agent_dirty = true;226227request_sync();228}229230void NavAgent2D::set_avoidance_layers(uint32_t p_layers) {231avoidance_layers = p_layers;232rvo_agent.avoidance_layers_ = avoidance_layers;233agent_dirty = true;234235request_sync();236}237238void NavAgent2D::set_avoidance_priority(real_t p_priority) {239ERR_FAIL_COND_MSG(p_priority < 0.0, "Avoidance priority must be between 0.0 and 1.0 inclusive.");240ERR_FAIL_COND_MSG(p_priority > 1.0, "Avoidance priority must be between 0.0 and 1.0 inclusive.");241avoidance_priority = p_priority;242rvo_agent.avoidance_priority_ = avoidance_priority;243agent_dirty = true;244245request_sync();246}247248bool NavAgent2D::is_dirty() const {249return agent_dirty;250}251252void NavAgent2D::sync() {253agent_dirty = false;254}255256const Dictionary NavAgent2D::get_avoidance_data() const {257// Returns debug data from RVO simulation internals of this agent.258Dictionary _avoidance_data;259260_avoidance_data["max_neighbors"] = int(rvo_agent.maxNeighbors_);261_avoidance_data["max_speed"] = float(rvo_agent.maxSpeed_);262_avoidance_data["neighbor_distance"] = float(rvo_agent.neighborDist_);263_avoidance_data["new_velocity"] = Vector2(rvo_agent.newVelocity_.x(), rvo_agent.newVelocity_.y());264_avoidance_data["velocity"] = Vector2(rvo_agent.velocity_.x(), rvo_agent.velocity_.y());265_avoidance_data["position"] = Vector2(rvo_agent.position_.x(), rvo_agent.position_.y());266_avoidance_data["preferred_velocity"] = Vector2(rvo_agent.prefVelocity_.x(), rvo_agent.prefVelocity_.y());267_avoidance_data["radius"] = float(rvo_agent.radius_);268_avoidance_data["time_horizon_agents"] = float(rvo_agent.timeHorizon_);269_avoidance_data["time_horizon_obstacles"] = float(rvo_agent.timeHorizonObst_);270_avoidance_data["avoidance_layers"] = int(rvo_agent.avoidance_layers_);271_avoidance_data["avoidance_mask"] = int(rvo_agent.avoidance_mask_);272_avoidance_data["avoidance_priority"] = float(rvo_agent.avoidance_priority_);273return _avoidance_data;274}275276void NavAgent2D::set_paused(bool p_paused) {277if (paused == p_paused) {278return;279}280281paused = p_paused;282283if (map) {284if (paused) {285map->remove_agent_as_controlled(this);286} else {287map->set_agent_as_controlled(this);288}289}290}291292bool NavAgent2D::get_paused() const {293return paused;294}295296void NavAgent2D::request_sync() {297if (map && !sync_dirty_request_list_element.in_list()) {298map->add_agent_sync_dirty_request(&sync_dirty_request_list_element);299}300}301302void NavAgent2D::cancel_sync_request() {303if (map && sync_dirty_request_list_element.in_list()) {304map->remove_agent_sync_dirty_request(&sync_dirty_request_list_element);305}306}307308NavAgent2D::NavAgent2D() :309sync_dirty_request_list_element(this) {310}311312NavAgent2D::~NavAgent2D() {313cancel_sync_request();314}315316317