Path: blob/master/scene/2d/navigation/navigation_agent_2d.h
9904 views
/**************************************************************************/1/* navigation_agent_2d.h */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#pragma once3132#include "scene/main/node.h"33#include "servers/navigation/navigation_globals.h"34#include "servers/navigation/navigation_path_query_parameters_2d.h"35#include "servers/navigation/navigation_path_query_result_2d.h"3637class Node2D;3839class NavigationAgent2D : public Node {40GDCLASS(NavigationAgent2D, Node);4142Node2D *agent_parent = nullptr;4344RID agent;45RID map_override;4647bool avoidance_enabled = false;48uint32_t avoidance_layers = 1;49uint32_t avoidance_mask = 1;50real_t avoidance_priority = 1.0;51uint32_t navigation_layers = 1;52NavigationPathQueryParameters2D::PathfindingAlgorithm pathfinding_algorithm = NavigationPathQueryParameters2D::PathfindingAlgorithm::PATHFINDING_ALGORITHM_ASTAR;53NavigationPathQueryParameters2D::PathPostProcessing path_postprocessing = NavigationPathQueryParameters2D::PathPostProcessing::PATH_POSTPROCESSING_CORRIDORFUNNEL;54BitField<NavigationPathQueryParameters2D::PathMetadataFlags> path_metadata_flags = NavigationPathQueryParameters2D::PathMetadataFlags::PATH_METADATA_INCLUDE_ALL;5556real_t path_desired_distance = 20.0;57real_t target_desired_distance = 10.0;58real_t radius = NavigationDefaults2D::AVOIDANCE_AGENT_RADIUS;59real_t neighbor_distance = NavigationDefaults2D::AVOIDANCE_AGENT_NEIGHBOR_DISTANCE;60int max_neighbors = NavigationDefaults2D::AVOIDANCE_AGENT_MAX_NEIGHBORS;61real_t time_horizon_agents = NavigationDefaults2D::AVOIDANCE_AGENT_TIME_HORIZON_AGENTS;62real_t time_horizon_obstacles = NavigationDefaults2D::AVOIDANCE_AGENT_TIME_HORIZON_OBSTACLES;63real_t max_speed = NavigationDefaults2D::AVOIDANCE_AGENT_MAX_SPEED;64real_t path_max_distance = 100.0;65bool simplify_path = false;66real_t simplify_epsilon = 0.0;67float path_return_max_length = 0.0;68float path_return_max_radius = 0.0;69int path_search_max_polygons = NavigationDefaults2D::path_search_max_polygons;70float path_search_max_distance = 0.0;7172Vector2 target_position;7374Ref<NavigationPathQueryParameters2D> navigation_query;75Ref<NavigationPathQueryResult2D> navigation_result;76int navigation_path_index = 0;7778// the velocity result of the avoidance simulation step79Vector2 safe_velocity;8081/// The submitted target velocity, sets the "wanted" rvo agent velocity on the next update82// this velocity is not guaranteed, the simulation will try to fulfill it if possible83// if other agents or obstacles interfere it will be changed accordingly84Vector2 velocity;85bool velocity_submitted = false;8687/// The submitted forced velocity, overrides the rvo agent velocity on the next update88// should only be used very intentionally and not every frame as it interferes with the simulation stability89Vector2 velocity_forced;90bool velocity_forced_submitted = false;9192bool target_position_submitted = false;9394bool target_reached = false;95bool navigation_finished = true;96bool last_waypoint_reached = false;9798// Debug properties for exposed bindings99bool debug_enabled = false;100float debug_path_custom_point_size = 4.0;101float debug_path_custom_line_width = -1.0;102bool debug_use_custom = false;103Color debug_path_custom_color = Color(1.0, 1.0, 1.0, 1.0);104105#ifdef DEBUG_ENABLED106// Debug properties internal only107bool debug_path_dirty = true;108RID debug_path_instance;109#endif // DEBUG_ENABLED110111protected:112static void _bind_methods();113void _notification(int p_what);114115#ifndef DISABLE_DEPRECATED116bool _set(const StringName &p_name, const Variant &p_value);117bool _get(const StringName &p_name, Variant &r_ret) const;118#endif // DISABLE_DEPRECATED119120public:121NavigationAgent2D();122virtual ~NavigationAgent2D();123124RID get_rid() const { return agent; }125126void set_avoidance_enabled(bool p_enabled);127bool get_avoidance_enabled() const;128129void set_agent_parent(Node *p_agent_parent);130131void set_navigation_layers(uint32_t p_navigation_layers);132uint32_t get_navigation_layers() const;133134void set_navigation_layer_value(int p_layer_number, bool p_value);135bool get_navigation_layer_value(int p_layer_number) const;136137void set_pathfinding_algorithm(const NavigationPathQueryParameters2D::PathfindingAlgorithm p_pathfinding_algorithm);138NavigationPathQueryParameters2D::PathfindingAlgorithm get_pathfinding_algorithm() const {139return pathfinding_algorithm;140}141142void set_path_postprocessing(const NavigationPathQueryParameters2D::PathPostProcessing p_path_postprocessing);143NavigationPathQueryParameters2D::PathPostProcessing get_path_postprocessing() const {144return path_postprocessing;145}146147void set_path_metadata_flags(BitField<NavigationPathQueryParameters2D::PathMetadataFlags> p_flags);148BitField<NavigationPathQueryParameters2D::PathMetadataFlags> get_path_metadata_flags() const {149return path_metadata_flags;150}151152void set_navigation_map(RID p_navigation_map);153RID get_navigation_map() const;154155void set_path_desired_distance(real_t p_dd);156real_t get_path_desired_distance() const { return path_desired_distance; }157158void set_target_desired_distance(real_t p_dd);159real_t get_target_desired_distance() const { return target_desired_distance; }160161void set_radius(real_t p_radius);162real_t get_radius() const { return radius; }163164void set_neighbor_distance(real_t p_distance);165real_t get_neighbor_distance() const { return neighbor_distance; }166167void set_max_neighbors(int p_count);168int get_max_neighbors() const { return max_neighbors; }169170void set_time_horizon_agents(real_t p_time_horizon);171real_t get_time_horizon_agents() const { return time_horizon_agents; }172173void set_time_horizon_obstacles(real_t p_time_horizon);174real_t get_time_horizon_obstacles() const { return time_horizon_obstacles; }175176void set_max_speed(real_t p_max_speed);177real_t get_max_speed() const { return max_speed; }178179void set_path_max_distance(real_t p_pmd);180real_t get_path_max_distance();181182void set_target_position(Vector2 p_position);183Vector2 get_target_position() const;184185void set_simplify_path(bool p_enabled);186bool get_simplify_path() const;187188void set_simplify_epsilon(real_t p_epsilon);189real_t get_simplify_epsilon() const;190191void set_path_return_max_length(float p_length);192float get_path_return_max_length() const;193194void set_path_return_max_radius(float p_radius);195float get_path_return_max_radius() const;196197void set_path_search_max_polygons(int p_max_polygons);198int get_path_search_max_polygons() const;199200void set_path_search_max_distance(float p_distance);201float get_path_search_max_distance() const;202203float get_path_length() const;204205Vector2 get_next_path_position();206207Ref<NavigationPathQueryResult2D> get_current_navigation_result() const { return navigation_result; }208209const Vector<Vector2> &get_current_navigation_path() const { return navigation_result->get_path(); }210211int get_current_navigation_path_index() const { return navigation_path_index; }212213real_t distance_to_target() const;214bool is_target_reached() const;215bool is_target_reachable();216bool is_navigation_finished();217Vector2 get_final_position();218219void set_velocity(const Vector2 p_velocity);220Vector2 get_velocity() { return velocity; }221222void set_velocity_forced(const Vector2 p_velocity);223224void _avoidance_done(Vector2 p_new_velocity);225226PackedStringArray get_configuration_warnings() const override;227228void set_avoidance_layers(uint32_t p_layers);229uint32_t get_avoidance_layers() const;230231void set_avoidance_mask(uint32_t p_mask);232uint32_t get_avoidance_mask() const;233234void set_avoidance_layer_value(int p_layer_number, bool p_value);235bool get_avoidance_layer_value(int p_layer_number) const;236237void set_avoidance_mask_value(int p_mask_number, bool p_value);238bool get_avoidance_mask_value(int p_mask_number) const;239240void set_avoidance_priority(real_t p_priority);241real_t get_avoidance_priority() const;242243void set_debug_enabled(bool p_enabled);244bool get_debug_enabled() const;245246void set_debug_use_custom(bool p_enabled);247bool get_debug_use_custom() const;248249void set_debug_path_custom_color(Color p_color);250Color get_debug_path_custom_color() const;251252void set_debug_path_custom_point_size(float p_point_size);253float get_debug_path_custom_point_size() const;254255void set_debug_path_custom_line_width(float p_line_width);256float get_debug_path_custom_line_width() const;257258private:259bool _is_target_reachable() const;260Vector2 _get_final_position() const;261262void _update_navigation();263void _advance_waypoints(const Vector2 &p_origin);264void _request_repath();265266bool _is_last_waypoint() const;267void _move_to_next_waypoint();268bool _is_within_waypoint_distance(const Vector2 &p_origin) const;269bool _is_within_target_distance(const Vector2 &p_origin) const;270271void _trigger_waypoint_reached();272void _transition_to_navigation_finished();273void _transition_to_target_reached();274275#ifdef DEBUG_ENABLED276void _navigation_debug_changed();277void _update_debug_path();278#endif // DEBUG_ENABLED279};280281282