nav_utils.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**************************************************************************/
  2. /* nav_utils.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_UTILS_H
  31. #define NAV_UTILS_H
  32. #include "core/math/vector3.h"
  33. #include <vector>
  34. class NavRegion;
  35. namespace gd {
  36. struct Polygon;
  37. union PointKey {
  38. struct {
  39. int64_t x : 21;
  40. int64_t y : 22;
  41. int64_t z : 21;
  42. };
  43. uint64_t key = 0;
  44. };
  45. struct EdgeKey {
  46. PointKey a;
  47. PointKey b;
  48. bool operator<(const EdgeKey &p_key) const {
  49. return (a.key == p_key.a.key) ? (b.key < p_key.b.key) : (a.key < p_key.a.key);
  50. }
  51. EdgeKey(const PointKey &p_a = PointKey(), const PointKey &p_b = PointKey()) :
  52. a(p_a),
  53. b(p_b) {
  54. if (a.key > b.key) {
  55. SWAP(a, b);
  56. }
  57. }
  58. };
  59. struct Point {
  60. Vector3 pos;
  61. PointKey key;
  62. };
  63. struct Edge {
  64. /// This edge ID
  65. int this_edge = -1;
  66. /// The gateway in the edge, as, in some case, the whole edge might not be navigable.
  67. struct Connection {
  68. Polygon *polygon = nullptr;
  69. int edge = -1;
  70. Vector3 pathway_start;
  71. Vector3 pathway_end;
  72. };
  73. Vector<Connection> connections;
  74. };
  75. struct Polygon {
  76. NavRegion *owner = nullptr;
  77. /// The points of this `Polygon`
  78. LocalVector<Point> points;
  79. /// Are the points clockwise ?
  80. bool clockwise;
  81. /// The edges of this `Polygon`
  82. LocalVector<Edge> edges;
  83. /// The center of this `Polygon`
  84. Vector3 center;
  85. };
  86. struct NavigationPoly {
  87. uint32_t self_id = 0;
  88. /// This poly.
  89. const Polygon *poly;
  90. /// Those 4 variables are used to travel the path backwards.
  91. int back_navigation_poly_id = -1;
  92. uint32_t back_navigation_edge = UINT32_MAX;
  93. Vector3 back_navigation_edge_pathway_start;
  94. Vector3 back_navigation_edge_pathway_end;
  95. /// The entry location of this poly.
  96. Vector3 entry;
  97. /// The distance to the destination.
  98. float traveled_distance = 0.0;
  99. NavigationPoly() { poly = nullptr; }
  100. NavigationPoly(const Polygon *p_poly) :
  101. poly(p_poly) {}
  102. bool operator==(const NavigationPoly &other) const {
  103. return this->poly == other.poly;
  104. }
  105. bool operator!=(const NavigationPoly &other) const {
  106. return !operator==(other);
  107. }
  108. };
  109. struct ClosestPointQueryResult {
  110. Vector3 point;
  111. Vector3 normal;
  112. RID owner;
  113. };
  114. } // namespace gd
  115. #endif // NAV_UTILS_H