nav_map.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. /// Map Up
  46. Vector3 up = Vector3(0, 1, 0);
  47. /// To find the polygons edges the vertices are displaced in a grid where
  48. /// each cell has the following cell_size and cell_height.
  49. real_t cell_size = 0.25; // Must match ProjectSettings default 3D cell_size and NavigationMesh cell_size.
  50. real_t cell_height = 0.25; // Must match ProjectSettings default 3D cell_height and NavigationMesh cell_height.
  51. bool use_edge_connections = true;
  52. /// This value is used to detect the near edges to connect.
  53. real_t edge_connection_margin = 0.25;
  54. /// This value is used to limit how far links search to find polygons to connect to.
  55. real_t link_connection_radius = 1.0;
  56. bool regenerate_polygons = true;
  57. bool regenerate_links = true;
  58. /// Map regions
  59. LocalVector<NavRegion *> regions;
  60. /// Map links
  61. LocalVector<NavLink *> links;
  62. LocalVector<gd::Polygon> link_polygons;
  63. /// Map polygons
  64. LocalVector<gd::Polygon> polygons;
  65. /// RVO avoidance worlds
  66. RVO2D::RVOSimulator2D rvo_simulation_2d;
  67. RVO3D::RVOSimulator3D rvo_simulation_3d;
  68. /// avoidance controlled agents
  69. LocalVector<NavAgent *> active_2d_avoidance_agents;
  70. LocalVector<NavAgent *> active_3d_avoidance_agents;
  71. /// dirty flag when one of the agent's arrays are modified
  72. bool agents_dirty = true;
  73. /// All the Agents (even the controlled one)
  74. LocalVector<NavAgent *> agents;
  75. /// All the avoidance obstacles (both static and dynamic)
  76. LocalVector<NavObstacle *> obstacles;
  77. /// Are rvo obstacles modified?
  78. bool obstacles_dirty = true;
  79. /// Physics delta time
  80. real_t deltatime = 0.0;
  81. /// Change the id each time the map is updated.
  82. uint32_t map_update_id = 0;
  83. bool use_threads = true;
  84. bool avoidance_use_multiple_threads = true;
  85. bool avoidance_use_high_priority_threads = true;
  86. // Performance Monitor
  87. int pm_region_count = 0;
  88. int pm_agent_count = 0;
  89. int pm_link_count = 0;
  90. int pm_polygon_count = 0;
  91. int pm_edge_count = 0;
  92. int pm_edge_merge_count = 0;
  93. int pm_edge_connection_count = 0;
  94. int pm_edge_free_count = 0;
  95. public:
  96. NavMap();
  97. ~NavMap();
  98. void set_up(Vector3 p_up);
  99. Vector3 get_up() const {
  100. return up;
  101. }
  102. void set_cell_size(real_t p_cell_size);
  103. real_t get_cell_size() const {
  104. return cell_size;
  105. }
  106. void set_cell_height(real_t p_cell_height);
  107. real_t get_cell_height() const { return cell_height; }
  108. void set_use_edge_connections(bool p_enabled);
  109. bool get_use_edge_connections() const {
  110. return use_edge_connections;
  111. }
  112. void set_edge_connection_margin(real_t p_edge_connection_margin);
  113. real_t get_edge_connection_margin() const {
  114. return edge_connection_margin;
  115. }
  116. void set_link_connection_radius(real_t p_link_connection_radius);
  117. real_t get_link_connection_radius() const {
  118. return link_connection_radius;
  119. }
  120. gd::PointKey get_point_key(const Vector3 &p_pos) const;
  121. 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;
  122. Vector3 get_closest_point_to_segment(const Vector3 &p_from, const Vector3 &p_to, const bool p_use_collision) const;
  123. Vector3 get_closest_point(const Vector3 &p_point) const;
  124. Vector3 get_closest_point_normal(const Vector3 &p_point) const;
  125. gd::ClosestPointQueryResult get_closest_point_info(const Vector3 &p_point) const;
  126. RID get_closest_point_owner(const Vector3 &p_point) const;
  127. void add_region(NavRegion *p_region);
  128. void remove_region(NavRegion *p_region);
  129. const LocalVector<NavRegion *> &get_regions() const {
  130. return regions;
  131. }
  132. void add_link(NavLink *p_link);
  133. void remove_link(NavLink *p_link);
  134. const LocalVector<NavLink *> &get_links() const {
  135. return links;
  136. }
  137. bool has_agent(NavAgent *agent) const;
  138. void add_agent(NavAgent *agent);
  139. void remove_agent(NavAgent *agent);
  140. const LocalVector<NavAgent *> &get_agents() const {
  141. return agents;
  142. }
  143. void set_agent_as_controlled(NavAgent *agent);
  144. void remove_agent_as_controlled(NavAgent *agent);
  145. bool has_obstacle(NavObstacle *obstacle) const;
  146. void add_obstacle(NavObstacle *obstacle);
  147. void remove_obstacle(NavObstacle *obstacle);
  148. const LocalVector<NavObstacle *> &get_obstacles() const {
  149. return obstacles;
  150. }
  151. uint32_t get_map_update_id() const {
  152. return map_update_id;
  153. }
  154. void sync();
  155. void step(real_t p_deltatime);
  156. void dispatch_callbacks();
  157. // Performance Monitor
  158. int get_pm_region_count() const { return pm_region_count; }
  159. int get_pm_agent_count() const { return pm_agent_count; }
  160. int get_pm_link_count() const { return pm_link_count; }
  161. int get_pm_polygon_count() const { return pm_polygon_count; }
  162. int get_pm_edge_count() const { return pm_edge_count; }
  163. int get_pm_edge_merge_count() const { return pm_edge_merge_count; }
  164. int get_pm_edge_connection_count() const { return pm_edge_connection_count; }
  165. int get_pm_edge_free_count() const { return pm_edge_free_count; }
  166. private:
  167. void compute_single_step(uint32_t index, NavAgent **agent);
  168. void compute_single_avoidance_step_2d(uint32_t index, NavAgent **agent);
  169. void compute_single_avoidance_step_3d(uint32_t index, NavAgent **agent);
  170. 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;
  171. void _update_rvo_simulation();
  172. void _update_rvo_obstacles_tree_2d();
  173. void _update_rvo_agents_tree_2d();
  174. void _update_rvo_agents_tree_3d();
  175. };
  176. #endif // NAV_MAP_H