nav_map.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 "core/map.h"
  34. #include "core/math/math_defs.h"
  35. #include "core/os/thread_work_pool.h"
  36. #include "nav_utils.h"
  37. #include <KdTree.h>
  38. class NavRegion;
  39. class RvoAgent;
  40. class NavRegion;
  41. class NavMap : public NavRid {
  42. /// Map Up
  43. Vector3 up = Vector3(0, 1, 0);
  44. /// To find the polygons edges the vertices are displaced in a grid where
  45. /// each cell has the following cell_size and cell_height.
  46. real_t cell_size = 0.25;
  47. real_t cell_height = 0.25;
  48. /// This value is used to detect the near edges to connect.
  49. real_t edge_connection_margin = 0.25;
  50. bool regenerate_polygons = true;
  51. bool regenerate_links = true;
  52. LocalVector<NavRegion *> regions;
  53. /// Map polygons
  54. LocalVector<gd::Polygon> polygons;
  55. /// Rvo world
  56. RVO::KdTree rvo;
  57. /// Is agent array modified?
  58. bool agents_dirty = false;
  59. /// All the Agents (even the controlled one)
  60. LocalVector<RvoAgent *> agents;
  61. /// Controlled agents
  62. LocalVector<RvoAgent *> controlled_agents;
  63. /// Physics delta time
  64. real_t deltatime = 0.0;
  65. /// Change the id each time the map is updated.
  66. uint32_t map_update_id = 0;
  67. #ifndef NO_THREADS
  68. /// Pooled threads for computing steps
  69. ThreadWorkPool step_work_pool;
  70. #endif // NO_THREADS
  71. public:
  72. NavMap();
  73. ~NavMap();
  74. void set_up(Vector3 p_up);
  75. Vector3 get_up() const {
  76. return up;
  77. }
  78. void set_cell_size(float p_cell_size);
  79. float get_cell_size() const {
  80. return cell_size;
  81. }
  82. void set_cell_height(float p_cell_height);
  83. float get_cell_height() const {
  84. return cell_height;
  85. }
  86. void set_edge_connection_margin(float p_edge_connection_margin);
  87. float get_edge_connection_margin() const {
  88. return edge_connection_margin;
  89. }
  90. gd::PointKey get_point_key(const Vector3 &p_pos) const;
  91. Vector<Vector3> get_path(Vector3 p_origin, Vector3 p_destination, bool p_optimize, uint32_t p_navigation_layers = 1) const;
  92. Vector3 get_closest_point_to_segment(const Vector3 &p_from, const Vector3 &p_to, const bool p_use_collision) const;
  93. Vector3 get_closest_point(const Vector3 &p_point) const;
  94. Vector3 get_closest_point_normal(const Vector3 &p_point) const;
  95. gd::ClosestPointQueryResult get_closest_point_info(const Vector3 &p_point) const;
  96. RID get_closest_point_owner(const Vector3 &p_point) const;
  97. void add_region(NavRegion *p_region);
  98. void remove_region(NavRegion *p_region);
  99. const LocalVector<NavRegion *> &get_regions() const {
  100. return regions;
  101. }
  102. bool has_agent(RvoAgent *agent) const;
  103. void add_agent(RvoAgent *agent);
  104. void remove_agent(RvoAgent *agent);
  105. const LocalVector<RvoAgent *> &get_agents() const {
  106. return agents;
  107. }
  108. void set_agent_as_controlled(RvoAgent *agent);
  109. void remove_agent_as_controlled(RvoAgent *agent);
  110. uint32_t get_map_update_id() const {
  111. return map_update_id;
  112. }
  113. void sync();
  114. void step(real_t p_deltatime);
  115. void dispatch_callbacks();
  116. private:
  117. void compute_single_step(uint32_t index, RvoAgent **agent);
  118. 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) const;
  119. };
  120. #endif // NAV_MAP_H