nav_region.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. map = p_map;
  34. polygons_dirty = true;
  35. if (!map) {
  36. connections.clear();
  37. }
  38. }
  39. void NavRegion::set_transform(Transform3D p_transform) {
  40. transform = p_transform;
  41. polygons_dirty = true;
  42. }
  43. void NavRegion::set_mesh(Ref<NavigationMesh> p_mesh) {
  44. mesh = p_mesh;
  45. polygons_dirty = true;
  46. }
  47. int NavRegion::get_connections_count() const {
  48. if (!map) {
  49. return 0;
  50. }
  51. return connections.size();
  52. }
  53. Vector3 NavRegion::get_connection_pathway_start(int p_connection_id) const {
  54. ERR_FAIL_COND_V(!map, Vector3());
  55. ERR_FAIL_INDEX_V(p_connection_id, connections.size(), Vector3());
  56. return connections[p_connection_id].pathway_start;
  57. }
  58. Vector3 NavRegion::get_connection_pathway_end(int p_connection_id) const {
  59. ERR_FAIL_COND_V(!map, Vector3());
  60. ERR_FAIL_INDEX_V(p_connection_id, connections.size(), Vector3());
  61. return connections[p_connection_id].pathway_end;
  62. }
  63. bool NavRegion::sync() {
  64. bool something_changed = polygons_dirty /* || something_dirty? */;
  65. update_polygons();
  66. return something_changed;
  67. }
  68. void NavRegion::update_polygons() {
  69. if (!polygons_dirty) {
  70. return;
  71. }
  72. polygons.clear();
  73. polygons_dirty = false;
  74. if (map == nullptr) {
  75. return;
  76. }
  77. if (mesh.is_null()) {
  78. return;
  79. }
  80. Vector<Vector3> vertices = mesh->get_vertices();
  81. int len = vertices.size();
  82. if (len == 0) {
  83. return;
  84. }
  85. const Vector3 *vertices_r = vertices.ptr();
  86. polygons.resize(mesh->get_polygon_count());
  87. // Build
  88. for (size_t i(0); i < polygons.size(); i++) {
  89. gd::Polygon &p = polygons[i];
  90. p.owner = this;
  91. Vector<int> mesh_poly = mesh->get_polygon(i);
  92. const int *indices = mesh_poly.ptr();
  93. bool valid(true);
  94. p.points.resize(mesh_poly.size());
  95. p.edges.resize(mesh_poly.size());
  96. Vector3 center;
  97. float sum(0);
  98. for (int j(0); j < mesh_poly.size(); j++) {
  99. int idx = indices[j];
  100. if (idx < 0 || idx >= len) {
  101. valid = false;
  102. break;
  103. }
  104. Vector3 point_position = transform.xform(vertices_r[idx]);
  105. p.points[j].pos = point_position;
  106. p.points[j].key = map->get_point_key(point_position);
  107. center += point_position; // Composing the center of the polygon
  108. if (j >= 2) {
  109. Vector3 epa = transform.xform(vertices_r[indices[j - 2]]);
  110. Vector3 epb = transform.xform(vertices_r[indices[j - 1]]);
  111. sum += map->get_up().dot((epb - epa).cross(point_position - epa));
  112. }
  113. }
  114. if (!valid) {
  115. ERR_BREAK_MSG(!valid, "The navigation mesh set in this region is not valid!");
  116. }
  117. p.clockwise = sum > 0;
  118. if (mesh_poly.size() != 0) {
  119. p.center = center / float(mesh_poly.size());
  120. }
  121. }
  122. }