navigation.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*************************************************************************/
  2. /* navigation.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_H
  31. #define NAVIGATION_H
  32. #include "scene/3d/navigation_mesh.h"
  33. #include "scene/3d/spatial.h"
  34. class Navigation : public Spatial {
  35. GDCLASS(Navigation, Spatial);
  36. union Point {
  37. struct {
  38. int64_t x : 21;
  39. int64_t y : 22;
  40. int64_t z : 21;
  41. };
  42. uint64_t key;
  43. bool operator<(const Point &p_key) const { return key < p_key.key; }
  44. };
  45. struct EdgeKey {
  46. Point a;
  47. Point 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 Point &p_a = Point(), const Point &p_b = Point()) :
  52. a(p_a),
  53. b(p_b) {
  54. if (a.key > b.key) {
  55. SWAP(a, b);
  56. }
  57. }
  58. };
  59. struct NavMesh;
  60. struct Polygon;
  61. struct ConnectionPending {
  62. Polygon *polygon;
  63. int edge;
  64. };
  65. struct Polygon {
  66. struct Edge {
  67. Point point;
  68. Polygon *C; //connection
  69. int C_edge;
  70. List<ConnectionPending>::Element *P;
  71. Edge() {
  72. C = NULL;
  73. C_edge = -1;
  74. P = NULL;
  75. }
  76. };
  77. Vector<Edge> edges;
  78. Vector3 center;
  79. Vector3 entry;
  80. float distance;
  81. int prev_edge;
  82. bool clockwise;
  83. NavMesh *owner;
  84. };
  85. struct Connection {
  86. Polygon *A;
  87. int A_edge;
  88. Polygon *B;
  89. int B_edge;
  90. List<ConnectionPending> pending;
  91. Connection() {
  92. A = NULL;
  93. B = NULL;
  94. A_edge = -1;
  95. B_edge = -1;
  96. }
  97. };
  98. Map<EdgeKey, Connection> connections;
  99. struct NavMesh {
  100. Object *owner;
  101. Transform xform;
  102. bool linked;
  103. Ref<NavigationMesh> navmesh;
  104. List<Polygon> polygons;
  105. };
  106. _FORCE_INLINE_ Point _get_point(const Vector3 &p_pos) const {
  107. int x = int(Math::floor(p_pos.x / cell_size));
  108. int y = int(Math::floor(p_pos.y / cell_size));
  109. int z = int(Math::floor(p_pos.z / cell_size));
  110. Point p;
  111. p.key = 0;
  112. p.x = x;
  113. p.y = y;
  114. p.z = z;
  115. return p;
  116. }
  117. _FORCE_INLINE_ Vector3 _get_vertex(const Point &p_point) const {
  118. return Vector3(p_point.x, p_point.y, p_point.z) * cell_size;
  119. }
  120. void _navmesh_link(int p_id);
  121. void _navmesh_unlink(int p_id);
  122. float cell_size;
  123. Map<int, NavMesh> navmesh_map;
  124. int last_id;
  125. Vector3 up;
  126. void _clip_path(Vector<Vector3> &path, Polygon *from_poly, const Vector3 &p_to_point, Polygon *p_to_poly);
  127. protected:
  128. static void _bind_methods();
  129. public:
  130. void set_up_vector(const Vector3 &p_up);
  131. Vector3 get_up_vector() const;
  132. //API should be as dynamic as possible
  133. int navmesh_add(const Ref<NavigationMesh> &p_mesh, const Transform &p_xform, Object *p_owner = NULL);
  134. void navmesh_set_transform(int p_id, const Transform &p_xform);
  135. void navmesh_remove(int p_id);
  136. Vector<Vector3> get_simple_path(const Vector3 &p_start, const Vector3 &p_end, bool p_optimize = true);
  137. Vector3 get_closest_point_to_segment(const Vector3 &p_from, const Vector3 &p_to, const bool &p_use_collision = false);
  138. Vector3 get_closest_point(const Vector3 &p_point);
  139. Vector3 get_closest_point_normal(const Vector3 &p_point);
  140. Object *get_closest_point_owner(const Vector3 &p_point);
  141. Navigation();
  142. };
  143. #endif // NAVIGATION_H