nav_map.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**************************************************************************/
  2. /* nav_map.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 NAV_MAP_H
  31. #define NAV_MAP_H
  32. #include "nav_rid.h"
  33. #include "nav_utils.h"
  34. #include "core/math/math_defs.h"
  35. #include "core/object/worker_thread_pool.h"
  36. #include <KdTree2d.h>
  37. #include <KdTree3d.h>
  38. #include <RVOSimulator2d.h>
  39. #include <RVOSimulator3d.h>
  40. class NavLink;
  41. class NavRegion;
  42. class NavAgent;
  43. class NavObstacle;
  44. class NavMap : public NavRid {
  45. RWLock map_rwlock;
  46. /// Map Up
  47. Vector3 up = Vector3(0, 1, 0);
  48. /// To find the polygons edges the vertices are displaced in a grid where
  49. /// each cell has the following cell_size and cell_height.
  50. real_t cell_size = 0.25; // Must match ProjectSettings default 3D cell_size and NavigationMesh cell_size.
  51. real_t cell_height = 0.25; // Must match ProjectSettings default 3D cell_height and NavigationMesh cell_height.
  52. // For the inter-region merging to work, internal rasterization is performed.
  53. float merge_rasterizer_cell_size = 0.25;
  54. float merge_rasterizer_cell_height = 0.25;
  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 = 0.25;
  60. /// This value is used to limit how far links search to find polygons to connect to.
  61. real_t link_connection_radius = 1.0;
  62. bool regenerate_polygons = true;
  63. bool regenerate_links = true;
  64. /// Map regions
  65. LocalVector<NavRegion *> regions;
  66. /// Map links
  67. LocalVector<NavLink *> links;
  68. LocalVector<gd::Polygon> link_polygons;
  69. /// Map polygons
  70. LocalVector<gd::Polygon> polygons;
  71. /// RVO avoidance worlds
  72. RVO2D::RVOSimulator2D rvo_simulation_2d;
  73. RVO3D::RVOSimulator3D rvo_simulation_3d;
  74. /// avoidance controlled agents
  75. LocalVector<NavAgent *> active_2d_avoidance_agents;
  76. LocalVector<NavAgent *> active_3d_avoidance_agents;
  77. /// dirty flag when one of the agent's arrays are modified
  78. bool agents_dirty = true;
  79. /// All the Agents (even the controlled one)
  80. LocalVector<NavAgent *> agents;
  81. /// All the avoidance obstacles (both static and dynamic)
  82. LocalVector<NavObstacle *> obstacles;
  83. /// Are rvo obstacles modified?
  84. bool obstacles_dirty = true;
  85. /// Physics delta time
  86. real_t deltatime = 0.0;
  87. /// Change the id each time the map is updated.
  88. uint32_t iteration_id = 0;
  89. bool use_threads = true;
  90. bool avoidance_use_multiple_threads = true;
  91. bool avoidance_use_high_priority_threads = true;
  92. // Performance Monitor
  93. int pm_region_count = 0;
  94. int pm_agent_count = 0;
  95. int pm_link_count = 0;
  96. int pm_polygon_count = 0;
  97. int pm_edge_count = 0;
  98. int pm_edge_merge_count = 0;
  99. int pm_edge_connection_count = 0;
  100. int pm_edge_free_count = 0;
  101. public:
  102. NavMap();
  103. ~NavMap();
  104. uint32_t get_iteration_id() const { return iteration_id; }
  105. void set_up(Vector3 p_up);
  106. Vector3 get_up() const {
  107. return up;
  108. }
  109. void set_cell_size(real_t p_cell_size);
  110. real_t get_cell_size() const {
  111. return cell_size;
  112. }
  113. void set_cell_height(real_t p_cell_height);
  114. real_t get_cell_height() const { return cell_height; }
  115. void set_merge_rasterizer_cell_scale(float p_value);
  116. float get_merge_rasterizer_cell_scale() const {
  117. return merge_rasterizer_cell_scale;
  118. }
  119. void set_use_edge_connections(bool p_enabled);
  120. bool get_use_edge_connections() const {
  121. return use_edge_connections;
  122. }
  123. void set_edge_connection_margin(real_t p_edge_connection_margin);
  124. real_t get_edge_connection_margin() const {
  125. return edge_connection_margin;
  126. }
  127. void set_link_connection_radius(real_t p_link_connection_radius);
  128. real_t get_link_connection_radius() const {
  129. return link_connection_radius;
  130. }
  131. gd::PointKey get_point_key(const Vector3 &p_pos) const;
  132. Vector<Vector3> get_path(Vector3 p_origin, Vector3 p_destination, bool p_optimize, uint32_t p_navigation_layers, Vector<int32_t> *r_path_types, TypedArray<RID> *r_path_rids, Vector<int64_t> *r_path_owners) const;
  133. Vector3 get_closest_point_to_segment(const Vector3 &p_from, const Vector3 &p_to, const bool p_use_collision) const;
  134. Vector3 get_closest_point(const Vector3 &p_point) const;
  135. Vector3 get_closest_point_normal(const Vector3 &p_point) const;
  136. gd::ClosestPointQueryResult get_closest_point_info(const Vector3 &p_point) const;
  137. RID get_closest_point_owner(const Vector3 &p_point) const;
  138. void add_region(NavRegion *p_region);
  139. void remove_region(NavRegion *p_region);
  140. const LocalVector<NavRegion *> &get_regions() const {
  141. return regions;
  142. }
  143. void add_link(NavLink *p_link);
  144. void remove_link(NavLink *p_link);
  145. const LocalVector<NavLink *> &get_links() const {
  146. return links;
  147. }
  148. bool has_agent(NavAgent *agent) const;
  149. void add_agent(NavAgent *agent);
  150. void remove_agent(NavAgent *agent);
  151. const LocalVector<NavAgent *> &get_agents() const {
  152. return agents;
  153. }
  154. void set_agent_as_controlled(NavAgent *agent);
  155. void remove_agent_as_controlled(NavAgent *agent);
  156. bool has_obstacle(NavObstacle *obstacle) const;
  157. void add_obstacle(NavObstacle *obstacle);
  158. void remove_obstacle(NavObstacle *obstacle);
  159. const LocalVector<NavObstacle *> &get_obstacles() const {
  160. return obstacles;
  161. }
  162. Vector3 get_random_point(uint32_t p_navigation_layers, bool p_uniformly) const;
  163. void sync();
  164. void step(real_t p_deltatime);
  165. void dispatch_callbacks();
  166. // Performance Monitor
  167. int get_pm_region_count() const { return pm_region_count; }
  168. int get_pm_agent_count() const { return pm_agent_count; }
  169. int get_pm_link_count() const { return pm_link_count; }
  170. int get_pm_polygon_count() const { return pm_polygon_count; }
  171. int get_pm_edge_count() const { return pm_edge_count; }
  172. int get_pm_edge_merge_count() const { return pm_edge_merge_count; }
  173. int get_pm_edge_connection_count() const { return pm_edge_connection_count; }
  174. int get_pm_edge_free_count() const { return pm_edge_free_count; }
  175. private:
  176. void compute_single_step(uint32_t index, NavAgent **agent);
  177. void compute_single_avoidance_step_2d(uint32_t index, NavAgent **agent);
  178. void compute_single_avoidance_step_3d(uint32_t index, NavAgent **agent);
  179. void clip_path(const LocalVector<gd::NavigationPoly> &p_navigation_polys, Vector<Vector3> &path, const gd::NavigationPoly *from_poly, const Vector3 &p_to_point, const gd::NavigationPoly *p_to_poly, Vector<int32_t> *r_path_types, TypedArray<RID> *r_path_rids, Vector<int64_t> *r_path_owners) const;
  180. void _update_rvo_simulation();
  181. void _update_rvo_obstacles_tree_2d();
  182. void _update_rvo_agents_tree_2d();
  183. void _update_rvo_agents_tree_3d();
  184. void _update_merge_rasterizer_cell_dimensions();
  185. };
  186. #endif // NAV_MAP_H