navigation_agent_2d.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /**************************************************************************/
  2. /* navigation_agent_2d.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef NAVIGATION_AGENT_2D_H
  31. #define NAVIGATION_AGENT_2D_H
  32. #include "scene/main/node.h"
  33. #include "servers/navigation/navigation_path_query_parameters_2d.h"
  34. #include "servers/navigation/navigation_path_query_result_2d.h"
  35. class Node2D;
  36. class NavigationAgent2D : public Node {
  37. GDCLASS(NavigationAgent2D, Node);
  38. Node2D *agent_parent = nullptr;
  39. RID agent;
  40. RID map_override;
  41. bool avoidance_enabled = false;
  42. uint32_t avoidance_layers = 1;
  43. uint32_t avoidance_mask = 1;
  44. real_t avoidance_priority = 1.0;
  45. uint32_t navigation_layers = 1;
  46. NavigationPathQueryParameters2D::PathfindingAlgorithm pathfinding_algorithm = NavigationPathQueryParameters2D::PathfindingAlgorithm::PATHFINDING_ALGORITHM_ASTAR;
  47. NavigationPathQueryParameters2D::PathPostProcessing path_postprocessing = NavigationPathQueryParameters2D::PathPostProcessing::PATH_POSTPROCESSING_CORRIDORFUNNEL;
  48. BitField<NavigationPathQueryParameters2D::PathMetadataFlags> path_metadata_flags = NavigationPathQueryParameters2D::PathMetadataFlags::PATH_METADATA_INCLUDE_ALL;
  49. real_t path_desired_distance = 20.0;
  50. real_t target_desired_distance = 10.0;
  51. real_t radius = 10.0;
  52. real_t neighbor_distance = 500.0;
  53. int max_neighbors = 10;
  54. real_t time_horizon_agents = 1.0;
  55. real_t time_horizon_obstacles = 0.0;
  56. real_t max_speed = 100.0;
  57. real_t path_max_distance = 100.0;
  58. bool simplify_path = false;
  59. real_t simplify_epsilon = 0.0;
  60. Vector2 target_position;
  61. Ref<NavigationPathQueryParameters2D> navigation_query;
  62. Ref<NavigationPathQueryResult2D> navigation_result;
  63. int navigation_path_index = 0;
  64. // the velocity result of the avoidance simulation step
  65. Vector2 safe_velocity;
  66. /// The submitted target velocity, sets the "wanted" rvo agent velocity on the next update
  67. // this velocity is not guaranteed, the simulation will try to fulfill it if possible
  68. // if other agents or obstacles interfere it will be changed accordingly
  69. Vector2 velocity;
  70. bool velocity_submitted = false;
  71. /// The submitted forced velocity, overrides the rvo agent velocity on the next update
  72. // should only be used very intentionally and not every frame as it interferes with the simulation stability
  73. Vector2 velocity_forced;
  74. bool velocity_forced_submitted = false;
  75. bool target_position_submitted = false;
  76. bool target_reached = false;
  77. bool navigation_finished = true;
  78. bool last_waypoint_reached = false;
  79. // Debug properties for exposed bindings
  80. bool debug_enabled = false;
  81. float debug_path_custom_point_size = 4.0;
  82. float debug_path_custom_line_width = -1.0;
  83. bool debug_use_custom = false;
  84. Color debug_path_custom_color = Color(1.0, 1.0, 1.0, 1.0);
  85. #ifdef DEBUG_ENABLED
  86. // Debug properties internal only
  87. bool debug_path_dirty = true;
  88. RID debug_path_instance;
  89. #endif // DEBUG_ENABLED
  90. protected:
  91. static void _bind_methods();
  92. void _notification(int p_what);
  93. #ifndef DISABLE_DEPRECATED
  94. bool _set(const StringName &p_name, const Variant &p_value);
  95. bool _get(const StringName &p_name, Variant &r_ret) const;
  96. #endif // DISABLE_DEPRECATED
  97. public:
  98. NavigationAgent2D();
  99. virtual ~NavigationAgent2D();
  100. RID get_rid() const { return agent; }
  101. void set_avoidance_enabled(bool p_enabled);
  102. bool get_avoidance_enabled() const;
  103. void set_agent_parent(Node *p_agent_parent);
  104. void set_navigation_layers(uint32_t p_navigation_layers);
  105. uint32_t get_navigation_layers() const;
  106. void set_navigation_layer_value(int p_layer_number, bool p_value);
  107. bool get_navigation_layer_value(int p_layer_number) const;
  108. void set_pathfinding_algorithm(const NavigationPathQueryParameters2D::PathfindingAlgorithm p_pathfinding_algorithm);
  109. NavigationPathQueryParameters2D::PathfindingAlgorithm get_pathfinding_algorithm() const {
  110. return pathfinding_algorithm;
  111. }
  112. void set_path_postprocessing(const NavigationPathQueryParameters2D::PathPostProcessing p_path_postprocessing);
  113. NavigationPathQueryParameters2D::PathPostProcessing get_path_postprocessing() const {
  114. return path_postprocessing;
  115. }
  116. void set_path_metadata_flags(BitField<NavigationPathQueryParameters2D::PathMetadataFlags> p_flags);
  117. BitField<NavigationPathQueryParameters2D::PathMetadataFlags> get_path_metadata_flags() const {
  118. return path_metadata_flags;
  119. }
  120. void set_navigation_map(RID p_navigation_map);
  121. RID get_navigation_map() const;
  122. void set_path_desired_distance(real_t p_dd);
  123. real_t get_path_desired_distance() const { return path_desired_distance; }
  124. void set_target_desired_distance(real_t p_dd);
  125. real_t get_target_desired_distance() const { return target_desired_distance; }
  126. void set_radius(real_t p_radius);
  127. real_t get_radius() const { return radius; }
  128. void set_neighbor_distance(real_t p_distance);
  129. real_t get_neighbor_distance() const { return neighbor_distance; }
  130. void set_max_neighbors(int p_count);
  131. int get_max_neighbors() const { return max_neighbors; }
  132. void set_time_horizon_agents(real_t p_time_horizon);
  133. real_t get_time_horizon_agents() const { return time_horizon_agents; }
  134. void set_time_horizon_obstacles(real_t p_time_horizon);
  135. real_t get_time_horizon_obstacles() const { return time_horizon_obstacles; }
  136. void set_max_speed(real_t p_max_speed);
  137. real_t get_max_speed() const { return max_speed; }
  138. void set_path_max_distance(real_t p_pmd);
  139. real_t get_path_max_distance();
  140. void set_target_position(Vector2 p_position);
  141. Vector2 get_target_position() const;
  142. void set_simplify_path(bool p_enabled);
  143. bool get_simplify_path() const;
  144. void set_simplify_epsilon(real_t p_epsilon);
  145. real_t get_simplify_epsilon() const;
  146. Vector2 get_next_path_position();
  147. Ref<NavigationPathQueryResult2D> get_current_navigation_result() const { return navigation_result; }
  148. const Vector<Vector2> &get_current_navigation_path() const { return navigation_result->get_path(); }
  149. int get_current_navigation_path_index() const { return navigation_path_index; }
  150. real_t distance_to_target() const;
  151. bool is_target_reached() const;
  152. bool is_target_reachable();
  153. bool is_navigation_finished();
  154. Vector2 get_final_position();
  155. void set_velocity(const Vector2 p_velocity);
  156. Vector2 get_velocity() { return velocity; }
  157. void set_velocity_forced(const Vector2 p_velocity);
  158. void _avoidance_done(Vector3 p_new_velocity);
  159. PackedStringArray get_configuration_warnings() const override;
  160. void set_avoidance_layers(uint32_t p_layers);
  161. uint32_t get_avoidance_layers() const;
  162. void set_avoidance_mask(uint32_t p_mask);
  163. uint32_t get_avoidance_mask() const;
  164. void set_avoidance_layer_value(int p_layer_number, bool p_value);
  165. bool get_avoidance_layer_value(int p_layer_number) const;
  166. void set_avoidance_mask_value(int p_mask_number, bool p_value);
  167. bool get_avoidance_mask_value(int p_mask_number) const;
  168. void set_avoidance_priority(real_t p_priority);
  169. real_t get_avoidance_priority() const;
  170. void set_debug_enabled(bool p_enabled);
  171. bool get_debug_enabled() const;
  172. void set_debug_use_custom(bool p_enabled);
  173. bool get_debug_use_custom() const;
  174. void set_debug_path_custom_color(Color p_color);
  175. Color get_debug_path_custom_color() const;
  176. void set_debug_path_custom_point_size(float p_point_size);
  177. float get_debug_path_custom_point_size() const;
  178. void set_debug_path_custom_line_width(float p_line_width);
  179. float get_debug_path_custom_line_width() const;
  180. private:
  181. bool _is_target_reachable() const;
  182. Vector2 _get_final_position() const;
  183. void _update_navigation();
  184. void _advance_waypoints(const Vector2 &p_origin);
  185. void _request_repath();
  186. bool _is_last_waypoint() const;
  187. void _move_to_next_waypoint();
  188. bool _is_within_waypoint_distance(const Vector2 &p_origin) const;
  189. bool _is_within_target_distance(const Vector2 &p_origin) const;
  190. void _trigger_waypoint_reached();
  191. void _transition_to_navigation_finished();
  192. void _transition_to_target_reached();
  193. #ifdef DEBUG_ENABLED
  194. void _navigation_debug_changed();
  195. void _update_debug_path();
  196. #endif // DEBUG_ENABLED
  197. };
  198. #endif // NAVIGATION_AGENT_2D_H