nav_region.cpp 5.0 KB

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