nav_map_3d.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /**************************************************************************/
  2. /* nav_map_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. #pragma once
  31. #include "3d/nav_map_iteration_3d.h"
  32. #include "3d/nav_mesh_queries_3d.h"
  33. #include "nav_rid_3d.h"
  34. #include "nav_utils_3d.h"
  35. #include "core/math/math_defs.h"
  36. #include "core/object/worker_thread_pool.h"
  37. #include "servers/navigation/navigation_globals.h"
  38. #include <KdTree2d.h>
  39. #include <KdTree3d.h>
  40. #include <RVOSimulator2d.h>
  41. #include <RVOSimulator3d.h>
  42. class NavLink3D;
  43. class NavRegion3D;
  44. class NavAgent3D;
  45. class NavObstacle3D;
  46. class NavMap3D : public NavRid3D {
  47. /// Map Up
  48. Vector3 up = Vector3(0, 1, 0);
  49. /// To find the polygons edges the vertices are displaced in a grid where
  50. /// each cell has the following cell_size and cell_height.
  51. real_t cell_size = NavigationDefaults3D::navmesh_cell_size;
  52. real_t cell_height = NavigationDefaults3D::navmesh_cell_height;
  53. // For the inter-region merging to work, internal rasterization is performed.
  54. Vector3 merge_rasterizer_cell_size = Vector3(cell_size, cell_height, cell_size);
  55. // This value is used to control sensitivity of internal rasterizer.
  56. float merge_rasterizer_cell_scale = 1.0;
  57. bool use_edge_connections = true;
  58. /// This value is used to detect the near edges to connect.
  59. real_t edge_connection_margin = NavigationDefaults3D::edge_connection_margin;
  60. /// This value is used to limit how far links search to find polygons to connect to.
  61. real_t link_connection_radius = NavigationDefaults3D::link_connection_radius;
  62. bool map_settings_dirty = true;
  63. /// Map regions
  64. LocalVector<NavRegion3D *> regions;
  65. /// Map links
  66. LocalVector<NavLink3D *> links;
  67. /// RVO avoidance worlds
  68. RVO2D::RVOSimulator2D rvo_simulation_2d;
  69. RVO3D::RVOSimulator3D rvo_simulation_3d;
  70. /// avoidance controlled agents
  71. LocalVector<NavAgent3D *> active_2d_avoidance_agents;
  72. LocalVector<NavAgent3D *> active_3d_avoidance_agents;
  73. /// dirty flag when one of the agent's arrays are modified
  74. bool agents_dirty = true;
  75. /// All the Agents (even the controlled one)
  76. LocalVector<NavAgent3D *> agents;
  77. /// All the avoidance obstacles (both static and dynamic)
  78. LocalVector<NavObstacle3D *> obstacles;
  79. /// Are rvo obstacles modified?
  80. bool obstacles_dirty = true;
  81. /// Physics delta time
  82. real_t deltatime = 0.0;
  83. /// Change the id each time the map is updated.
  84. uint32_t iteration_id = 0;
  85. bool use_threads = true;
  86. bool avoidance_use_multiple_threads = true;
  87. bool avoidance_use_high_priority_threads = true;
  88. // Performance Monitor
  89. Nav3D::PerformanceData performance_data;
  90. struct {
  91. SelfList<NavRegion3D>::List regions;
  92. SelfList<NavLink3D>::List links;
  93. SelfList<NavAgent3D>::List agents;
  94. SelfList<NavObstacle3D>::List obstacles;
  95. } sync_dirty_requests;
  96. int path_query_slots_max = 4;
  97. bool use_async_iterations = true;
  98. uint32_t iteration_slot_index = 0;
  99. LocalVector<NavMapIteration3D> iteration_slots;
  100. mutable RWLock iteration_slot_rwlock;
  101. NavMapIterationBuild3D iteration_build;
  102. bool iteration_build_use_threads = false;
  103. WorkerThreadPool::TaskID iteration_build_thread_task_id = WorkerThreadPool::INVALID_TASK_ID;
  104. static void _build_iteration_threaded(void *p_arg);
  105. bool iteration_dirty = true;
  106. bool iteration_building = false;
  107. bool iteration_ready = false;
  108. void _build_iteration();
  109. void _sync_iteration();
  110. public:
  111. NavMap3D();
  112. ~NavMap3D();
  113. uint32_t get_iteration_id() const { return iteration_id; }
  114. void set_up(Vector3 p_up);
  115. Vector3 get_up() const {
  116. return up;
  117. }
  118. void set_cell_size(real_t p_cell_size);
  119. real_t get_cell_size() const {
  120. return cell_size;
  121. }
  122. void set_cell_height(real_t p_cell_height);
  123. real_t get_cell_height() const { return cell_height; }
  124. void set_merge_rasterizer_cell_scale(float p_value);
  125. float get_merge_rasterizer_cell_scale() const {
  126. return merge_rasterizer_cell_scale;
  127. }
  128. void set_use_edge_connections(bool p_enabled);
  129. bool get_use_edge_connections() const {
  130. return use_edge_connections;
  131. }
  132. void set_edge_connection_margin(real_t p_edge_connection_margin);
  133. real_t get_edge_connection_margin() const {
  134. return edge_connection_margin;
  135. }
  136. void set_link_connection_radius(real_t p_link_connection_radius);
  137. real_t get_link_connection_radius() const {
  138. return link_connection_radius;
  139. }
  140. Nav3D::PointKey get_point_key(const Vector3 &p_pos) const;
  141. const Vector3 &get_merge_rasterizer_cell_size() const;
  142. void query_path(NavMeshQueries3D::NavMeshPathQueryTask3D &p_query_task);
  143. Vector3 get_closest_point_to_segment(const Vector3 &p_from, const Vector3 &p_to, const bool p_use_collision) const;
  144. Vector3 get_closest_point(const Vector3 &p_point) const;
  145. Vector3 get_closest_point_normal(const Vector3 &p_point) const;
  146. Nav3D::ClosestPointQueryResult get_closest_point_info(const Vector3 &p_point) const;
  147. RID get_closest_point_owner(const Vector3 &p_point) const;
  148. void add_region(NavRegion3D *p_region);
  149. void remove_region(NavRegion3D *p_region);
  150. const LocalVector<NavRegion3D *> &get_regions() const {
  151. return regions;
  152. }
  153. void add_link(NavLink3D *p_link);
  154. void remove_link(NavLink3D *p_link);
  155. const LocalVector<NavLink3D *> &get_links() const {
  156. return links;
  157. }
  158. bool has_agent(NavAgent3D *agent) const;
  159. void add_agent(NavAgent3D *agent);
  160. void remove_agent(NavAgent3D *agent);
  161. const LocalVector<NavAgent3D *> &get_agents() const {
  162. return agents;
  163. }
  164. void set_agent_as_controlled(NavAgent3D *agent);
  165. void remove_agent_as_controlled(NavAgent3D *agent);
  166. bool has_obstacle(NavObstacle3D *obstacle) const;
  167. void add_obstacle(NavObstacle3D *obstacle);
  168. void remove_obstacle(NavObstacle3D *obstacle);
  169. const LocalVector<NavObstacle3D *> &get_obstacles() const {
  170. return obstacles;
  171. }
  172. Vector3 get_random_point(uint32_t p_navigation_layers, bool p_uniformly) const;
  173. void sync();
  174. void step(real_t p_deltatime);
  175. void dispatch_callbacks();
  176. // Performance Monitor
  177. int get_pm_region_count() const { return performance_data.pm_region_count; }
  178. int get_pm_agent_count() const { return performance_data.pm_agent_count; }
  179. int get_pm_link_count() const { return performance_data.pm_link_count; }
  180. int get_pm_polygon_count() const { return performance_data.pm_polygon_count; }
  181. int get_pm_edge_count() const { return performance_data.pm_edge_count; }
  182. int get_pm_edge_merge_count() const { return performance_data.pm_edge_merge_count; }
  183. int get_pm_edge_connection_count() const { return performance_data.pm_edge_connection_count; }
  184. int get_pm_edge_free_count() const { return performance_data.pm_edge_free_count; }
  185. int get_pm_obstacle_count() const { return performance_data.pm_obstacle_count; }
  186. int get_region_connections_count(NavRegion3D *p_region) const;
  187. Vector3 get_region_connection_pathway_start(NavRegion3D *p_region, int p_connection_id) const;
  188. Vector3 get_region_connection_pathway_end(NavRegion3D *p_region, int p_connection_id) const;
  189. void add_region_sync_dirty_request(SelfList<NavRegion3D> *p_sync_request);
  190. void add_link_sync_dirty_request(SelfList<NavLink3D> *p_sync_request);
  191. void add_agent_sync_dirty_request(SelfList<NavAgent3D> *p_sync_request);
  192. void add_obstacle_sync_dirty_request(SelfList<NavObstacle3D> *p_sync_request);
  193. void remove_region_sync_dirty_request(SelfList<NavRegion3D> *p_sync_request);
  194. void remove_link_sync_dirty_request(SelfList<NavLink3D> *p_sync_request);
  195. void remove_agent_sync_dirty_request(SelfList<NavAgent3D> *p_sync_request);
  196. void remove_obstacle_sync_dirty_request(SelfList<NavObstacle3D> *p_sync_request);
  197. void set_use_async_iterations(bool p_enabled);
  198. bool get_use_async_iterations() const;
  199. private:
  200. void _sync_dirty_map_update_requests();
  201. void _sync_dirty_avoidance_update_requests();
  202. void compute_single_step(uint32_t index, NavAgent3D **agent);
  203. void compute_single_avoidance_step_2d(uint32_t index, NavAgent3D **agent);
  204. void compute_single_avoidance_step_3d(uint32_t index, NavAgent3D **agent);
  205. void _sync_avoidance();
  206. void _update_rvo_simulation();
  207. void _update_rvo_obstacles_tree_2d();
  208. void _update_rvo_agents_tree_2d();
  209. void _update_rvo_agents_tree_3d();
  210. void _update_merge_rasterizer_cell_dimensions();
  211. };