navigation_agent_3d.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**************************************************************************/
  2. /* navigation_agent_3d.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_3D_H
  31. #define NAVIGATION_AGENT_3D_H
  32. #include "scene/main/node.h"
  33. #include "servers/navigation/navigation_path_query_parameters_3d.h"
  34. #include "servers/navigation/navigation_path_query_result_3d.h"
  35. class Node3D;
  36. class NavigationAgent3D : public Node {
  37. GDCLASS(NavigationAgent3D, Node);
  38. Node3D *agent_parent = nullptr;
  39. RID agent;
  40. RID map_before_pause;
  41. RID map_override;
  42. bool avoidance_enabled = false;
  43. uint32_t navigation_layers = 1;
  44. NavigationPathQueryParameters3D::PathfindingAlgorithm pathfinding_algorithm = NavigationPathQueryParameters3D::PathfindingAlgorithm::PATHFINDING_ALGORITHM_ASTAR;
  45. NavigationPathQueryParameters3D::PathPostProcessing path_postprocessing = NavigationPathQueryParameters3D::PathPostProcessing::PATH_POSTPROCESSING_CORRIDORFUNNEL;
  46. BitField<NavigationPathQueryParameters3D::PathMetadataFlags> path_metadata_flags = NavigationPathQueryParameters3D::PathMetadataFlags::PATH_METADATA_INCLUDE_ALL;
  47. real_t path_desired_distance = 1.0;
  48. real_t target_desired_distance = 1.0;
  49. real_t radius = 0.5;
  50. real_t navigation_height_offset = 0.0;
  51. bool ignore_y = true;
  52. real_t neighbor_distance = 50.0;
  53. int max_neighbors = 10;
  54. real_t time_horizon = 1.0;
  55. real_t max_speed = 10.0;
  56. real_t path_max_distance = 5.0;
  57. Vector3 target_position;
  58. bool target_position_submitted = false;
  59. Ref<NavigationPathQueryParameters3D> navigation_query;
  60. Ref<NavigationPathQueryResult3D> navigation_result;
  61. int navigation_path_index = 0;
  62. bool velocity_submitted = false;
  63. Vector3 prev_safe_velocity;
  64. /// The submitted target velocity
  65. Vector3 target_velocity;
  66. bool target_reached = false;
  67. bool navigation_finished = true;
  68. // No initialized on purpose
  69. uint32_t update_frame_id = 0;
  70. // Debug properties for exposed bindings
  71. bool debug_enabled = false;
  72. float debug_path_custom_point_size = 4.0;
  73. bool debug_use_custom = false;
  74. Color debug_path_custom_color = Color(1.0, 1.0, 1.0, 1.0);
  75. #ifdef DEBUG_ENABLED
  76. // Debug properties internal only
  77. bool debug_path_dirty = true;
  78. RID debug_path_instance;
  79. Ref<ArrayMesh> debug_path_mesh;
  80. Ref<StandardMaterial3D> debug_agent_path_line_custom_material;
  81. Ref<StandardMaterial3D> debug_agent_path_point_custom_material;
  82. private:
  83. void _navigation_debug_changed();
  84. void _update_debug_path();
  85. #endif // DEBUG_ENABLED
  86. protected:
  87. static void _bind_methods();
  88. void _notification(int p_what);
  89. public:
  90. NavigationAgent3D();
  91. virtual ~NavigationAgent3D();
  92. RID get_rid() const {
  93. return agent;
  94. }
  95. void set_avoidance_enabled(bool p_enabled);
  96. bool get_avoidance_enabled() const;
  97. void set_agent_parent(Node *p_agent_parent);
  98. void set_navigation_layers(uint32_t p_navigation_layers);
  99. uint32_t get_navigation_layers() const;
  100. void set_navigation_layer_value(int p_layer_number, bool p_value);
  101. bool get_navigation_layer_value(int p_layer_number) const;
  102. void set_pathfinding_algorithm(const NavigationPathQueryParameters3D::PathfindingAlgorithm p_pathfinding_algorithm);
  103. NavigationPathQueryParameters3D::PathfindingAlgorithm get_pathfinding_algorithm() const {
  104. return pathfinding_algorithm;
  105. }
  106. void set_path_postprocessing(const NavigationPathQueryParameters3D::PathPostProcessing p_path_postprocessing);
  107. NavigationPathQueryParameters3D::PathPostProcessing get_path_postprocessing() const {
  108. return path_postprocessing;
  109. }
  110. void set_path_metadata_flags(BitField<NavigationPathQueryParameters3D::PathMetadataFlags> p_flags);
  111. BitField<NavigationPathQueryParameters3D::PathMetadataFlags> get_path_metadata_flags() const {
  112. return path_metadata_flags;
  113. }
  114. void set_navigation_map(RID p_navigation_map);
  115. RID get_navigation_map() const;
  116. void set_path_desired_distance(real_t p_dd);
  117. real_t get_path_desired_distance() const {
  118. return path_desired_distance;
  119. }
  120. void set_target_desired_distance(real_t p_dd);
  121. real_t get_target_desired_distance() const {
  122. return target_desired_distance;
  123. }
  124. void set_radius(real_t p_radius);
  125. real_t get_radius() const {
  126. return radius;
  127. }
  128. void set_agent_height_offset(real_t p_hh);
  129. real_t get_agent_height_offset() const {
  130. return navigation_height_offset;
  131. }
  132. void set_ignore_y(bool p_ignore_y);
  133. bool get_ignore_y() const {
  134. return ignore_y;
  135. }
  136. void set_neighbor_distance(real_t p_distance);
  137. real_t get_neighbor_distance() const {
  138. return neighbor_distance;
  139. }
  140. void set_max_neighbors(int p_count);
  141. int get_max_neighbors() const {
  142. return max_neighbors;
  143. }
  144. void set_time_horizon(real_t p_time);
  145. real_t get_time_horizon() const {
  146. return time_horizon;
  147. }
  148. void set_max_speed(real_t p_max_speed);
  149. real_t get_max_speed() const {
  150. return max_speed;
  151. }
  152. void set_path_max_distance(real_t p_pmd);
  153. real_t get_path_max_distance();
  154. void set_target_position(Vector3 p_position);
  155. Vector3 get_target_position() const;
  156. Vector3 get_next_path_position();
  157. Ref<NavigationPathQueryResult3D> get_current_navigation_result() const {
  158. return navigation_result;
  159. }
  160. const Vector<Vector3> &get_current_navigation_path() const {
  161. return navigation_result->get_path();
  162. }
  163. int get_current_navigation_path_index() const {
  164. return navigation_path_index;
  165. }
  166. real_t distance_to_target() const;
  167. bool is_target_reached() const;
  168. bool is_target_reachable();
  169. bool is_navigation_finished();
  170. Vector3 get_final_position();
  171. void set_velocity(Vector3 p_velocity);
  172. void _avoidance_done(Vector3 p_new_velocity);
  173. PackedStringArray get_configuration_warnings() const override;
  174. void set_debug_enabled(bool p_enabled);
  175. bool get_debug_enabled() const;
  176. void set_debug_use_custom(bool p_enabled);
  177. bool get_debug_use_custom() const;
  178. void set_debug_path_custom_color(Color p_color);
  179. Color get_debug_path_custom_color() const;
  180. void set_debug_path_custom_point_size(float p_point_size);
  181. float get_debug_path_custom_point_size() const;
  182. private:
  183. void update_navigation();
  184. void _request_repath();
  185. void _check_distance_to_target();
  186. };
  187. #endif // NAVIGATION_AGENT_3D_H