navigation_agent_3d.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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_override;
  41. bool avoidance_enabled = false;
  42. bool use_3d_avoidance = false;
  43. uint32_t avoidance_layers = 1;
  44. uint32_t avoidance_mask = 1;
  45. real_t avoidance_priority = 1.0;
  46. uint32_t navigation_layers = 1;
  47. NavigationPathQueryParameters3D::PathfindingAlgorithm pathfinding_algorithm = NavigationPathQueryParameters3D::PathfindingAlgorithm::PATHFINDING_ALGORITHM_ASTAR;
  48. NavigationPathQueryParameters3D::PathPostProcessing path_postprocessing = NavigationPathQueryParameters3D::PathPostProcessing::PATH_POSTPROCESSING_CORRIDORFUNNEL;
  49. BitField<NavigationPathQueryParameters3D::PathMetadataFlags> path_metadata_flags = NavigationPathQueryParameters3D::PathMetadataFlags::PATH_METADATA_INCLUDE_ALL;
  50. real_t path_desired_distance = 1.0;
  51. real_t target_desired_distance = 1.0;
  52. real_t height = 1.0;
  53. real_t radius = 0.5;
  54. real_t path_height_offset = 0.0;
  55. real_t neighbor_distance = 50.0;
  56. int max_neighbors = 10;
  57. real_t time_horizon_agents = 1.0;
  58. real_t time_horizon_obstacles = 0.0;
  59. real_t max_speed = 10.0;
  60. real_t path_max_distance = 5.0;
  61. Vector3 target_position;
  62. Ref<NavigationPathQueryParameters3D> navigation_query;
  63. Ref<NavigationPathQueryResult3D> navigation_result;
  64. int navigation_path_index = 0;
  65. // the velocity result of the avoidance simulation step
  66. Vector3 safe_velocity;
  67. /// The submitted target velocity, sets the "wanted" rvo agent velocity on the next update
  68. // this velocity is not guaranteed, the simulation will try to fulfill it if possible
  69. // if other agents or obstacles interfere it will be changed accordingly
  70. Vector3 velocity;
  71. bool velocity_submitted = false;
  72. /// The submitted forced velocity, overrides the rvo agent velocity on the next update
  73. // should only be used very intentionally and not every frame as it interferes with the simulation stability
  74. Vector3 velocity_forced;
  75. bool velocity_forced_submitted = false;
  76. // 2D avoidance has no y-axis. This stores and reapplies the y-axis velocity to the agent before and after the avoidance step.
  77. // While not perfect it at least looks way better than agent's that clip through everything that is not a flat surface
  78. bool keep_y_velocity = true;
  79. float stored_y_velocity = 0.0;
  80. bool target_position_submitted = false;
  81. bool target_reached = false;
  82. bool navigation_finished = true;
  83. // No initialized on purpose
  84. uint32_t update_frame_id = 0;
  85. // Debug properties for exposed bindings
  86. bool debug_enabled = false;
  87. float debug_path_custom_point_size = 4.0;
  88. bool debug_use_custom = false;
  89. Color debug_path_custom_color = Color(1.0, 1.0, 1.0, 1.0);
  90. #ifdef DEBUG_ENABLED
  91. // Debug properties internal only
  92. bool debug_path_dirty = true;
  93. RID debug_path_instance;
  94. Ref<ArrayMesh> debug_path_mesh;
  95. Ref<StandardMaterial3D> debug_agent_path_line_custom_material;
  96. Ref<StandardMaterial3D> debug_agent_path_point_custom_material;
  97. #endif // DEBUG_ENABLED
  98. protected:
  99. static void _bind_methods();
  100. void _notification(int p_what);
  101. void _validate_property(PropertyInfo &p_property) const;
  102. #ifndef DISABLE_DEPRECATED
  103. bool _set(const StringName &p_name, const Variant &p_value);
  104. bool _get(const StringName &p_name, Variant &r_ret) const;
  105. #endif // DISABLE_DEPRECATED
  106. public:
  107. NavigationAgent3D();
  108. virtual ~NavigationAgent3D();
  109. RID get_rid() const { return agent; }
  110. void set_avoidance_enabled(bool p_enabled);
  111. bool get_avoidance_enabled() const;
  112. void set_agent_parent(Node *p_agent_parent);
  113. void set_navigation_layers(uint32_t p_navigation_layers);
  114. uint32_t get_navigation_layers() const;
  115. void set_navigation_layer_value(int p_layer_number, bool p_value);
  116. bool get_navigation_layer_value(int p_layer_number) const;
  117. void set_pathfinding_algorithm(const NavigationPathQueryParameters3D::PathfindingAlgorithm p_pathfinding_algorithm);
  118. NavigationPathQueryParameters3D::PathfindingAlgorithm get_pathfinding_algorithm() const {
  119. return pathfinding_algorithm;
  120. }
  121. void set_path_postprocessing(const NavigationPathQueryParameters3D::PathPostProcessing p_path_postprocessing);
  122. NavigationPathQueryParameters3D::PathPostProcessing get_path_postprocessing() const {
  123. return path_postprocessing;
  124. }
  125. void set_path_metadata_flags(BitField<NavigationPathQueryParameters3D::PathMetadataFlags> p_flags);
  126. BitField<NavigationPathQueryParameters3D::PathMetadataFlags> get_path_metadata_flags() const {
  127. return path_metadata_flags;
  128. }
  129. void set_navigation_map(RID p_navigation_map);
  130. RID get_navigation_map() const;
  131. void set_path_desired_distance(real_t p_dd);
  132. real_t get_path_desired_distance() const { return path_desired_distance; }
  133. void set_target_desired_distance(real_t p_dd);
  134. real_t get_target_desired_distance() const { return target_desired_distance; }
  135. void set_radius(real_t p_radius);
  136. real_t get_radius() const { return radius; }
  137. void set_height(real_t p_height);
  138. real_t get_height() const { return height; }
  139. void set_path_height_offset(real_t p_path_height_offset);
  140. real_t get_path_height_offset() const { return path_height_offset; }
  141. void set_use_3d_avoidance(bool p_use_3d_avoidance);
  142. bool get_use_3d_avoidance() const { return use_3d_avoidance; }
  143. void set_keep_y_velocity(bool p_enabled);
  144. bool get_keep_y_velocity() const;
  145. void set_neighbor_distance(real_t p_distance);
  146. real_t get_neighbor_distance() const { return neighbor_distance; }
  147. void set_max_neighbors(int p_count);
  148. int get_max_neighbors() const { return max_neighbors; }
  149. void set_time_horizon_agents(real_t p_time_horizon);
  150. real_t get_time_horizon_agents() const { return time_horizon_agents; }
  151. void set_time_horizon_obstacles(real_t p_time_horizon);
  152. real_t get_time_horizon_obstacles() const { return time_horizon_obstacles; }
  153. void set_max_speed(real_t p_max_speed);
  154. real_t get_max_speed() const { return max_speed; }
  155. void set_path_max_distance(real_t p_pmd);
  156. real_t get_path_max_distance();
  157. void set_target_position(Vector3 p_position);
  158. Vector3 get_target_position() const;
  159. Vector3 get_next_path_position();
  160. Ref<NavigationPathQueryResult3D> get_current_navigation_result() const { return navigation_result; }
  161. const Vector<Vector3> &get_current_navigation_path() const { return navigation_result->get_path(); }
  162. int get_current_navigation_path_index() const { return navigation_path_index; }
  163. real_t distance_to_target() const;
  164. bool is_target_reached() const;
  165. bool is_target_reachable();
  166. bool is_navigation_finished();
  167. Vector3 get_final_position();
  168. void set_velocity(const Vector3 p_velocity);
  169. Vector3 get_velocity() { return velocity; }
  170. void set_velocity_forced(const Vector3 p_velocity);
  171. void _avoidance_done(Vector3 p_new_velocity);
  172. PackedStringArray get_configuration_warnings() const override;
  173. void set_avoidance_layers(uint32_t p_layers);
  174. uint32_t get_avoidance_layers() const;
  175. void set_avoidance_mask(uint32_t p_mask);
  176. uint32_t get_avoidance_mask() const;
  177. void set_avoidance_layer_value(int p_layer_number, bool p_value);
  178. bool get_avoidance_layer_value(int p_layer_number) const;
  179. void set_avoidance_mask_value(int p_mask_number, bool p_value);
  180. bool get_avoidance_mask_value(int p_mask_number) const;
  181. void set_avoidance_priority(real_t p_priority);
  182. real_t get_avoidance_priority() const;
  183. void set_debug_enabled(bool p_enabled);
  184. bool get_debug_enabled() const;
  185. void set_debug_use_custom(bool p_enabled);
  186. bool get_debug_use_custom() const;
  187. void set_debug_path_custom_color(Color p_color);
  188. Color get_debug_path_custom_color() const;
  189. void set_debug_path_custom_point_size(float p_point_size);
  190. float get_debug_path_custom_point_size() const;
  191. private:
  192. void update_navigation();
  193. void _request_repath();
  194. void _check_distance_to_target();
  195. #ifdef DEBUG_ENABLED
  196. void _navigation_debug_changed();
  197. void _update_debug_path();
  198. #endif // DEBUG_ENABLED
  199. };
  200. #endif // NAVIGATION_AGENT_3D_H