nav_map.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*************************************************************************/
  2. /* nav_map.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 RVO_SPACE_H
  31. #define RVO_SPACE_H
  32. #include "nav_rid.h"
  33. #include "core/math/math_defs.h"
  34. #include "nav_utils.h"
  35. #include <KdTree.h>
  36. /**
  37. @author AndreaCatania
  38. */
  39. class NavRegion;
  40. class RvoAgent;
  41. class NavRegion;
  42. class NavMap : public NavRid {
  43. /// Map Up
  44. Vector3 up = Vector3(0, 1, 0);
  45. /// To find the polygons edges the vertices are displaced in a grid where
  46. /// each cell has the following cell_size.
  47. real_t cell_size = 0.3;
  48. /// This value is used to detect the near edges to connect.
  49. real_t edge_connection_margin = 5.0;
  50. bool regenerate_polygons = true;
  51. bool regenerate_links = true;
  52. std::vector<NavRegion *> regions;
  53. /// Map polygons
  54. std::vector<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. std::vector<RvoAgent *> agents;
  61. /// Controlled agents
  62. std::vector<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. public:
  68. NavMap() {}
  69. void set_up(Vector3 p_up);
  70. Vector3 get_up() const {
  71. return up;
  72. }
  73. void set_cell_size(float p_cell_size);
  74. float get_cell_size() const {
  75. return cell_size;
  76. }
  77. void set_edge_connection_margin(float p_edge_connection_margin);
  78. float get_edge_connection_margin() const {
  79. return edge_connection_margin;
  80. }
  81. gd::PointKey get_point_key(const Vector3 &p_pos) const;
  82. Vector<Vector3> get_path(Vector3 p_origin, Vector3 p_destination, bool p_optimize) const;
  83. Vector3 get_closest_point_to_segment(const Vector3 &p_from, const Vector3 &p_to, const bool p_use_collision) const;
  84. Vector3 get_closest_point(const Vector3 &p_point) const;
  85. Vector3 get_closest_point_normal(const Vector3 &p_point) const;
  86. RID get_closest_point_owner(const Vector3 &p_point) const;
  87. void add_region(NavRegion *p_region);
  88. void remove_region(NavRegion *p_region);
  89. const std::vector<NavRegion *> &get_regions() const {
  90. return regions;
  91. }
  92. bool has_agent(RvoAgent *agent) const;
  93. void add_agent(RvoAgent *agent);
  94. void remove_agent(RvoAgent *agent);
  95. const std::vector<RvoAgent *> &get_agents() const {
  96. return agents;
  97. }
  98. void set_agent_as_controlled(RvoAgent *agent);
  99. void remove_agent_as_controlled(RvoAgent *agent);
  100. uint32_t get_map_update_id() const {
  101. return map_update_id;
  102. }
  103. void sync();
  104. void step(real_t p_deltatime);
  105. void dispatch_callbacks();
  106. private:
  107. void compute_single_step(uint32_t index, RvoAgent **agent);
  108. void clip_path(const std::vector<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;
  109. };
  110. #endif // RVO_SPACE_H