navigation_agent_3d.h 11 KB

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