nav_region.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**************************************************************************/
  2. /* nav_region.cpp */
  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. #include "nav_region.h"
  31. #include "nav_map.h"
  32. void NavRegion::set_map(NavMap *p_map) {
  33. if (map == p_map) {
  34. return;
  35. }
  36. if (map) {
  37. map->remove_region(this);
  38. }
  39. map = p_map;
  40. polygons_dirty = true;
  41. connections.clear();
  42. if (map) {
  43. map->add_region(this);
  44. }
  45. }
  46. void NavRegion::set_use_edge_connections(bool p_enabled) {
  47. if (use_edge_connections != p_enabled) {
  48. use_edge_connections = p_enabled;
  49. polygons_dirty = true;
  50. }
  51. }
  52. void NavRegion::set_transform(Transform3D p_transform) {
  53. if (transform == p_transform) {
  54. return;
  55. }
  56. transform = p_transform;
  57. polygons_dirty = true;
  58. }
  59. void NavRegion::set_mesh(Ref<NavigationMesh> p_mesh) {
  60. mesh = p_mesh;
  61. polygons_dirty = true;
  62. }
  63. int NavRegion::get_connections_count() const {
  64. if (!map) {
  65. return 0;
  66. }
  67. return connections.size();
  68. }
  69. Vector3 NavRegion::get_connection_pathway_start(int p_connection_id) const {
  70. ERR_FAIL_COND_V(!map, Vector3());
  71. ERR_FAIL_INDEX_V(p_connection_id, connections.size(), Vector3());
  72. return connections[p_connection_id].pathway_start;
  73. }
  74. Vector3 NavRegion::get_connection_pathway_end(int p_connection_id) const {
  75. ERR_FAIL_COND_V(!map, Vector3());
  76. ERR_FAIL_INDEX_V(p_connection_id, connections.size(), Vector3());
  77. return connections[p_connection_id].pathway_end;
  78. }
  79. bool NavRegion::sync() {
  80. bool something_changed = polygons_dirty /* || something_dirty? */;
  81. update_polygons();
  82. return something_changed;
  83. }
  84. void NavRegion::update_polygons() {
  85. if (!polygons_dirty) {
  86. return;
  87. }
  88. polygons.clear();
  89. polygons_dirty = false;
  90. if (map == nullptr) {
  91. return;
  92. }
  93. if (mesh.is_null()) {
  94. return;
  95. }
  96. #ifdef DEBUG_ENABLED
  97. if (!Math::is_equal_approx(double(map->get_cell_size()), double(mesh->get_cell_size()))) {
  98. ERR_PRINT_ONCE(vformat("Navigation map synchronization error. Attempted to update a navigation region with a navigation mesh that uses a `cell_size` of %s while assigned to a navigation map set to a `cell_size` of %s. The cell size for navigation maps can be changed by using the NavigationServer map_set_cell_size() function. The cell size for default navigation maps can also be changed in the ProjectSettings.", double(map->get_cell_size()), double(mesh->get_cell_size())));
  99. }
  100. if (!Math::is_equal_approx(double(map->get_cell_height()), double(mesh->get_cell_height()))) {
  101. ERR_PRINT_ONCE(vformat("Navigation map synchronization error. Attempted to update a navigation region with a navigation mesh that uses a `cell_height` of %s while assigned to a navigation map set to a `cell_height` of %s. The cell height for navigation maps can be changed by using the NavigationServer map_set_cell_height() function. The cell height for default navigation maps can also be changed in the ProjectSettings.", double(map->get_cell_height()), double(mesh->get_cell_height())));
  102. }
  103. if (map && Math::rad_to_deg(map->get_up().angle_to(transform.basis.get_column(1))) >= 90.0f) {
  104. ERR_PRINT_ONCE("Navigation map synchronization error. Attempted to update a navigation region transform rotated 90 degrees or more away from the current navigation map UP orientation.");
  105. }
  106. #endif // DEBUG_ENABLED
  107. Vector<Vector3> vertices = mesh->get_vertices();
  108. int len = vertices.size();
  109. if (len == 0) {
  110. return;
  111. }
  112. const Vector3 *vertices_r = vertices.ptr();
  113. polygons.resize(mesh->get_polygon_count());
  114. // Build
  115. for (size_t i(0); i < polygons.size(); i++) {
  116. gd::Polygon &p = polygons[i];
  117. p.owner = this;
  118. Vector<int> mesh_poly = mesh->get_polygon(i);
  119. const int *indices = mesh_poly.ptr();
  120. bool valid(true);
  121. p.points.resize(mesh_poly.size());
  122. p.edges.resize(mesh_poly.size());
  123. Vector3 center;
  124. real_t sum(0);
  125. for (int j(0); j < mesh_poly.size(); j++) {
  126. int idx = indices[j];
  127. if (idx < 0 || idx >= len) {
  128. valid = false;
  129. break;
  130. }
  131. Vector3 point_position = transform.xform(vertices_r[idx]);
  132. p.points[j].pos = point_position;
  133. p.points[j].key = map->get_point_key(point_position);
  134. center += point_position; // Composing the center of the polygon
  135. if (j >= 2) {
  136. Vector3 epa = transform.xform(vertices_r[indices[j - 2]]);
  137. Vector3 epb = transform.xform(vertices_r[indices[j - 1]]);
  138. sum += map->get_up().dot((epb - epa).cross(point_position - epa));
  139. }
  140. }
  141. if (!valid) {
  142. ERR_BREAK_MSG(!valid, "The navigation mesh set in this region is not valid!");
  143. }
  144. p.clockwise = sum > 0;
  145. if (mesh_poly.size() != 0) {
  146. p.center = center / real_t(mesh_poly.size());
  147. }
  148. }
  149. }