navigation2d.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*************************************************************************/
  2. /* navigation2d.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 NAVIGATION_2D_H
  31. #define NAVIGATION_2D_H
  32. #include "scene/2d/navigation_polygon.h"
  33. #include "scene/2d/node_2d.h"
  34. class Navigation2D : public Node2D {
  35. GDCLASS(Navigation2D, Node2D);
  36. union Point {
  37. struct {
  38. int64_t x : 32;
  39. int64_t y : 32;
  40. };
  41. uint64_t key;
  42. bool operator<(const Point &p_key) const { return key < p_key.key; }
  43. };
  44. struct EdgeKey {
  45. Point a;
  46. Point b;
  47. bool operator<(const EdgeKey &p_key) const {
  48. return (a.key == p_key.a.key) ? (b.key < p_key.b.key) : (a.key < p_key.a.key);
  49. };
  50. EdgeKey(const Point &p_a = Point(), const Point &p_b = Point()) :
  51. a(p_a),
  52. b(p_b) {
  53. if (a.key > b.key) {
  54. SWAP(a, b);
  55. }
  56. }
  57. };
  58. struct NavMesh;
  59. struct Polygon;
  60. struct ConnectionPending {
  61. Polygon *polygon;
  62. int edge;
  63. };
  64. struct Polygon {
  65. struct Edge {
  66. Point point;
  67. Polygon *C; //connection
  68. int C_edge;
  69. List<ConnectionPending>::Element *P;
  70. Edge() {
  71. C = NULL;
  72. C_edge = -1;
  73. P = NULL;
  74. }
  75. };
  76. Vector<Edge> edges;
  77. Vector2 center;
  78. Vector2 entry;
  79. float distance;
  80. int prev_edge;
  81. bool clockwise;
  82. NavMesh *owner;
  83. };
  84. struct Connection {
  85. Polygon *A;
  86. int A_edge;
  87. Polygon *B;
  88. int B_edge;
  89. List<ConnectionPending> pending;
  90. Connection() {
  91. A = NULL;
  92. B = NULL;
  93. A_edge = -1;
  94. B_edge = -1;
  95. }
  96. };
  97. Map<EdgeKey, Connection> connections;
  98. struct NavMesh {
  99. Object *owner;
  100. Transform2D xform;
  101. bool linked;
  102. Ref<NavigationPolygon> navpoly;
  103. List<Polygon> polygons;
  104. };
  105. _FORCE_INLINE_ Point _get_point(const Vector2 &p_pos) const {
  106. int x = int(Math::floor(p_pos.x / cell_size));
  107. int y = int(Math::floor(p_pos.y / cell_size));
  108. Point p;
  109. p.key = 0;
  110. p.x = x;
  111. p.y = y;
  112. return p;
  113. }
  114. _FORCE_INLINE_ Vector2 _get_vertex(const Point &p_point) const {
  115. return Vector2(p_point.x, p_point.y) * cell_size;
  116. }
  117. void _navpoly_link(int p_id);
  118. void _navpoly_unlink(int p_id);
  119. float cell_size;
  120. Map<int, NavMesh> navpoly_map;
  121. int last_id;
  122. protected:
  123. static void _bind_methods();
  124. public:
  125. //API should be as dynamic as possible
  126. int navpoly_add(const Ref<NavigationPolygon> &p_mesh, const Transform2D &p_xform, Object *p_owner = NULL);
  127. void navpoly_set_transform(int p_id, const Transform2D &p_xform);
  128. void navpoly_remove(int p_id);
  129. Vector<Vector2> get_simple_path(const Vector2 &p_start, const Vector2 &p_end, bool p_optimize = true);
  130. Vector2 get_closest_point(const Vector2 &p_point);
  131. Object *get_closest_point_owner(const Vector2 &p_point);
  132. Navigation2D();
  133. };
  134. #endif // Navigation2D2D_H