nav_mesh_queries_3d.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /**************************************************************************/
  2. /* nav_mesh_queries_3d.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. #ifndef _3D_DISABLED
  31. #include "nav_mesh_queries_3d.h"
  32. #include "../nav_base.h"
  33. #include "core/math/geometry_3d.h"
  34. #define THREE_POINTS_CROSS_PRODUCT(m_a, m_b, m_c) (((m_c) - (m_a)).cross((m_b) - (m_a)))
  35. #define APPEND_METADATA(poly) \
  36. if (r_path_types) { \
  37. r_path_types->push_back(poly->owner->get_type()); \
  38. } \
  39. if (r_path_rids) { \
  40. r_path_rids->push_back(poly->owner->get_self()); \
  41. } \
  42. if (r_path_owners) { \
  43. r_path_owners->push_back(poly->owner->get_owner_id()); \
  44. }
  45. Vector3 NavMeshQueries3D::polygons_get_random_point(const LocalVector<gd::Polygon> &p_polygons, uint32_t p_navigation_layers, bool p_uniformly) {
  46. const LocalVector<gd::Polygon> &region_polygons = p_polygons;
  47. if (region_polygons.is_empty()) {
  48. return Vector3();
  49. }
  50. if (p_uniformly) {
  51. real_t accumulated_area = 0;
  52. RBMap<real_t, uint32_t> region_area_map;
  53. for (uint32_t rp_index = 0; rp_index < region_polygons.size(); rp_index++) {
  54. const gd::Polygon &region_polygon = region_polygons[rp_index];
  55. real_t polyon_area = region_polygon.surface_area;
  56. if (polyon_area == 0.0) {
  57. continue;
  58. }
  59. region_area_map[accumulated_area] = rp_index;
  60. accumulated_area += polyon_area;
  61. }
  62. if (region_area_map.is_empty() || accumulated_area == 0) {
  63. // All polygons have no real surface / no area.
  64. return Vector3();
  65. }
  66. real_t region_area_map_pos = Math::random(real_t(0), accumulated_area);
  67. RBMap<real_t, uint32_t>::Iterator region_E = region_area_map.find_closest(region_area_map_pos);
  68. ERR_FAIL_COND_V(!region_E, Vector3());
  69. uint32_t rrp_polygon_index = region_E->value;
  70. ERR_FAIL_UNSIGNED_INDEX_V(rrp_polygon_index, region_polygons.size(), Vector3());
  71. const gd::Polygon &rr_polygon = region_polygons[rrp_polygon_index];
  72. real_t accumulated_polygon_area = 0;
  73. RBMap<real_t, uint32_t> polygon_area_map;
  74. for (uint32_t rpp_index = 2; rpp_index < rr_polygon.points.size(); rpp_index++) {
  75. real_t face_area = Face3(rr_polygon.points[0].pos, rr_polygon.points[rpp_index - 1].pos, rr_polygon.points[rpp_index].pos).get_area();
  76. if (face_area == 0.0) {
  77. continue;
  78. }
  79. polygon_area_map[accumulated_polygon_area] = rpp_index;
  80. accumulated_polygon_area += face_area;
  81. }
  82. if (polygon_area_map.is_empty() || accumulated_polygon_area == 0) {
  83. // All faces have no real surface / no area.
  84. return Vector3();
  85. }
  86. real_t polygon_area_map_pos = Math::random(real_t(0), accumulated_polygon_area);
  87. RBMap<real_t, uint32_t>::Iterator polygon_E = polygon_area_map.find_closest(polygon_area_map_pos);
  88. ERR_FAIL_COND_V(!polygon_E, Vector3());
  89. uint32_t rrp_face_index = polygon_E->value;
  90. ERR_FAIL_UNSIGNED_INDEX_V(rrp_face_index, rr_polygon.points.size(), Vector3());
  91. const Face3 face(rr_polygon.points[0].pos, rr_polygon.points[rrp_face_index - 1].pos, rr_polygon.points[rrp_face_index].pos);
  92. Vector3 face_random_position = face.get_random_point_inside();
  93. return face_random_position;
  94. } else {
  95. uint32_t rrp_polygon_index = Math::random(int(0), region_polygons.size() - 1);
  96. const gd::Polygon &rr_polygon = region_polygons[rrp_polygon_index];
  97. uint32_t rrp_face_index = Math::random(int(2), rr_polygon.points.size() - 1);
  98. const Face3 face(rr_polygon.points[0].pos, rr_polygon.points[rrp_face_index - 1].pos, rr_polygon.points[rrp_face_index].pos);
  99. Vector3 face_random_position = face.get_random_point_inside();
  100. return face_random_position;
  101. }
  102. }
  103. Vector<Vector3> NavMeshQueries3D::polygons_get_path(const LocalVector<gd::Polygon> &p_polygons, Vector3 p_origin, Vector3 p_destination, bool p_optimize, uint32_t p_navigation_layers, Vector<int32_t> *r_path_types, TypedArray<RID> *r_path_rids, Vector<int64_t> *r_path_owners, const Vector3 &p_map_up, uint32_t p_link_polygons_size) {
  104. // Clear metadata outputs.
  105. if (r_path_types) {
  106. r_path_types->clear();
  107. }
  108. if (r_path_rids) {
  109. r_path_rids->clear();
  110. }
  111. if (r_path_owners) {
  112. r_path_owners->clear();
  113. }
  114. // Find the start poly and the end poly on this map.
  115. const gd::Polygon *begin_poly = nullptr;
  116. const gd::Polygon *end_poly = nullptr;
  117. Vector3 begin_point;
  118. Vector3 end_point;
  119. real_t begin_d = FLT_MAX;
  120. real_t end_d = FLT_MAX;
  121. // Find the initial poly and the end poly on this map.
  122. for (const gd::Polygon &p : p_polygons) {
  123. // Only consider the polygon if it in a region with compatible layers.
  124. if ((p_navigation_layers & p.owner->get_navigation_layers()) == 0) {
  125. continue;
  126. }
  127. // For each face check the distance between the origin/destination
  128. for (size_t point_id = 2; point_id < p.points.size(); point_id++) {
  129. const Face3 face(p.points[0].pos, p.points[point_id - 1].pos, p.points[point_id].pos);
  130. Vector3 point = face.get_closest_point_to(p_origin);
  131. real_t distance_to_point = point.distance_to(p_origin);
  132. if (distance_to_point < begin_d) {
  133. begin_d = distance_to_point;
  134. begin_poly = &p;
  135. begin_point = point;
  136. }
  137. point = face.get_closest_point_to(p_destination);
  138. distance_to_point = point.distance_to(p_destination);
  139. if (distance_to_point < end_d) {
  140. end_d = distance_to_point;
  141. end_poly = &p;
  142. end_point = point;
  143. }
  144. }
  145. }
  146. // Check for trivial cases
  147. if (!begin_poly || !end_poly) {
  148. return Vector<Vector3>();
  149. }
  150. if (begin_poly == end_poly) {
  151. if (r_path_types) {
  152. r_path_types->resize(2);
  153. r_path_types->write[0] = begin_poly->owner->get_type();
  154. r_path_types->write[1] = end_poly->owner->get_type();
  155. }
  156. if (r_path_rids) {
  157. r_path_rids->resize(2);
  158. (*r_path_rids)[0] = begin_poly->owner->get_self();
  159. (*r_path_rids)[1] = end_poly->owner->get_self();
  160. }
  161. if (r_path_owners) {
  162. r_path_owners->resize(2);
  163. r_path_owners->write[0] = begin_poly->owner->get_owner_id();
  164. r_path_owners->write[1] = end_poly->owner->get_owner_id();
  165. }
  166. Vector<Vector3> path;
  167. path.resize(2);
  168. path.write[0] = begin_point;
  169. path.write[1] = end_point;
  170. return path;
  171. }
  172. // List of all reachable navigation polys.
  173. LocalVector<gd::NavigationPoly> navigation_polys;
  174. navigation_polys.resize(p_polygons.size() + p_link_polygons_size);
  175. // Initialize the matching navigation polygon.
  176. gd::NavigationPoly &begin_navigation_poly = navigation_polys[begin_poly->id];
  177. begin_navigation_poly.poly = begin_poly;
  178. begin_navigation_poly.entry = begin_point;
  179. begin_navigation_poly.back_navigation_edge_pathway_start = begin_point;
  180. begin_navigation_poly.back_navigation_edge_pathway_end = begin_point;
  181. // Heap of polygons to travel next.
  182. gd::Heap<gd::NavigationPoly *, gd::NavPolyTravelCostGreaterThan, gd::NavPolyHeapIndexer>
  183. traversable_polys;
  184. traversable_polys.reserve(p_polygons.size() * 0.25);
  185. // This is an implementation of the A* algorithm.
  186. int least_cost_id = begin_poly->id;
  187. int prev_least_cost_id = -1;
  188. bool found_route = false;
  189. const gd::Polygon *reachable_end = nullptr;
  190. real_t distance_to_reachable_end = FLT_MAX;
  191. bool is_reachable = true;
  192. while (true) {
  193. // Takes the current least_cost_poly neighbors (iterating over its edges) and compute the traveled_distance.
  194. for (const gd::Edge &edge : navigation_polys[least_cost_id].poly->edges) {
  195. // Iterate over connections in this edge, then compute the new optimized travel distance assigned to this polygon.
  196. for (uint32_t connection_index = 0; connection_index < edge.connections.size(); connection_index++) {
  197. const gd::Edge::Connection &connection = edge.connections[connection_index];
  198. // Only consider the connection to another polygon if this polygon is in a region with compatible layers.
  199. if ((p_navigation_layers & connection.polygon->owner->get_navigation_layers()) == 0) {
  200. continue;
  201. }
  202. const gd::NavigationPoly &least_cost_poly = navigation_polys[least_cost_id];
  203. real_t poly_enter_cost = 0.0;
  204. real_t poly_travel_cost = least_cost_poly.poly->owner->get_travel_cost();
  205. if (prev_least_cost_id != -1 && navigation_polys[prev_least_cost_id].poly->owner->get_self() != least_cost_poly.poly->owner->get_self()) {
  206. poly_enter_cost = least_cost_poly.poly->owner->get_enter_cost();
  207. }
  208. prev_least_cost_id = least_cost_id;
  209. Vector3 pathway[2] = { connection.pathway_start, connection.pathway_end };
  210. const Vector3 new_entry = Geometry3D::get_closest_point_to_segment(least_cost_poly.entry, pathway);
  211. const real_t new_traveled_distance = least_cost_poly.entry.distance_to(new_entry) * poly_travel_cost + poly_enter_cost + least_cost_poly.traveled_distance;
  212. // Check if the neighbor polygon has already been processed.
  213. gd::NavigationPoly &neighbor_poly = navigation_polys[connection.polygon->id];
  214. if (neighbor_poly.poly != nullptr) {
  215. // If the neighbor polygon hasn't been traversed yet and the new path leading to
  216. // it is shorter, update the polygon.
  217. if (neighbor_poly.traversable_poly_index < traversable_polys.size() &&
  218. new_traveled_distance < neighbor_poly.traveled_distance) {
  219. neighbor_poly.back_navigation_poly_id = least_cost_id;
  220. neighbor_poly.back_navigation_edge = connection.edge;
  221. neighbor_poly.back_navigation_edge_pathway_start = connection.pathway_start;
  222. neighbor_poly.back_navigation_edge_pathway_end = connection.pathway_end;
  223. neighbor_poly.traveled_distance = new_traveled_distance;
  224. neighbor_poly.distance_to_destination =
  225. new_entry.distance_to(end_point) *
  226. neighbor_poly.poly->owner->get_travel_cost();
  227. neighbor_poly.entry = new_entry;
  228. // Update the priority of the polygon in the heap.
  229. traversable_polys.shift(neighbor_poly.traversable_poly_index);
  230. }
  231. } else {
  232. // Initialize the matching navigation polygon.
  233. neighbor_poly.poly = connection.polygon;
  234. neighbor_poly.back_navigation_poly_id = least_cost_id;
  235. neighbor_poly.back_navigation_edge = connection.edge;
  236. neighbor_poly.back_navigation_edge_pathway_start = connection.pathway_start;
  237. neighbor_poly.back_navigation_edge_pathway_end = connection.pathway_end;
  238. neighbor_poly.traveled_distance = new_traveled_distance;
  239. neighbor_poly.distance_to_destination =
  240. new_entry.distance_to(end_point) *
  241. neighbor_poly.poly->owner->get_travel_cost();
  242. neighbor_poly.entry = new_entry;
  243. // Add the polygon to the heap of polygons to traverse next.
  244. traversable_polys.push(&neighbor_poly);
  245. }
  246. }
  247. }
  248. // When the heap of traversable polygons is empty at this point it means the end polygon is
  249. // unreachable.
  250. if (traversable_polys.is_empty()) {
  251. // Thus use the further reachable polygon
  252. ERR_BREAK_MSG(is_reachable == false, "It's not expect to not find the most reachable polygons");
  253. is_reachable = false;
  254. if (reachable_end == nullptr) {
  255. // The path is not found and there is not a way out.
  256. break;
  257. }
  258. // Set as end point the furthest reachable point.
  259. end_poly = reachable_end;
  260. end_d = FLT_MAX;
  261. for (size_t point_id = 2; point_id < end_poly->points.size(); point_id++) {
  262. Face3 f(end_poly->points[0].pos, end_poly->points[point_id - 1].pos, end_poly->points[point_id].pos);
  263. Vector3 spoint = f.get_closest_point_to(p_destination);
  264. real_t dpoint = spoint.distance_to(p_destination);
  265. if (dpoint < end_d) {
  266. end_point = spoint;
  267. end_d = dpoint;
  268. }
  269. }
  270. // Search all faces of start polygon as well.
  271. bool closest_point_on_start_poly = false;
  272. for (size_t point_id = 2; point_id < begin_poly->points.size(); point_id++) {
  273. Face3 f(begin_poly->points[0].pos, begin_poly->points[point_id - 1].pos, begin_poly->points[point_id].pos);
  274. Vector3 spoint = f.get_closest_point_to(p_destination);
  275. real_t dpoint = spoint.distance_to(p_destination);
  276. if (dpoint < end_d) {
  277. end_point = spoint;
  278. end_d = dpoint;
  279. closest_point_on_start_poly = true;
  280. }
  281. }
  282. if (closest_point_on_start_poly) {
  283. // No point to run PostProcessing when start and end convex polygon is the same.
  284. if (r_path_types) {
  285. r_path_types->resize(2);
  286. r_path_types->write[0] = begin_poly->owner->get_type();
  287. r_path_types->write[1] = begin_poly->owner->get_type();
  288. }
  289. if (r_path_rids) {
  290. r_path_rids->resize(2);
  291. (*r_path_rids)[0] = begin_poly->owner->get_self();
  292. (*r_path_rids)[1] = begin_poly->owner->get_self();
  293. }
  294. if (r_path_owners) {
  295. r_path_owners->resize(2);
  296. r_path_owners->write[0] = begin_poly->owner->get_owner_id();
  297. r_path_owners->write[1] = begin_poly->owner->get_owner_id();
  298. }
  299. Vector<Vector3> path;
  300. path.resize(2);
  301. path.write[0] = begin_point;
  302. path.write[1] = end_point;
  303. return path;
  304. }
  305. for (gd::NavigationPoly &nav_poly : navigation_polys) {
  306. nav_poly.poly = nullptr;
  307. }
  308. navigation_polys[begin_poly->id].poly = begin_poly;
  309. least_cost_id = begin_poly->id;
  310. prev_least_cost_id = -1;
  311. reachable_end = nullptr;
  312. continue;
  313. }
  314. // Pop the polygon with the lowest travel cost from the heap of traversable polygons.
  315. least_cost_id = traversable_polys.pop()->poly->id;
  316. // Store the farthest reachable end polygon in case our goal is not reachable.
  317. if (is_reachable) {
  318. real_t distance = navigation_polys[least_cost_id].entry.distance_to(p_destination);
  319. if (distance_to_reachable_end > distance) {
  320. distance_to_reachable_end = distance;
  321. reachable_end = navigation_polys[least_cost_id].poly;
  322. }
  323. }
  324. // Check if we reached the end
  325. if (navigation_polys[least_cost_id].poly == end_poly) {
  326. found_route = true;
  327. break;
  328. }
  329. }
  330. // We did not find a route but we have both a start polygon and an end polygon at this point.
  331. // Usually this happens because there was not a single external or internal connected edge, e.g. our start polygon is an isolated, single convex polygon.
  332. if (!found_route) {
  333. end_d = FLT_MAX;
  334. // Search all faces of the start polygon for the closest point to our target position.
  335. for (size_t point_id = 2; point_id < begin_poly->points.size(); point_id++) {
  336. Face3 f(begin_poly->points[0].pos, begin_poly->points[point_id - 1].pos, begin_poly->points[point_id].pos);
  337. Vector3 spoint = f.get_closest_point_to(p_destination);
  338. real_t dpoint = spoint.distance_to(p_destination);
  339. if (dpoint < end_d) {
  340. end_point = spoint;
  341. end_d = dpoint;
  342. }
  343. }
  344. if (r_path_types) {
  345. r_path_types->resize(2);
  346. r_path_types->write[0] = begin_poly->owner->get_type();
  347. r_path_types->write[1] = begin_poly->owner->get_type();
  348. }
  349. if (r_path_rids) {
  350. r_path_rids->resize(2);
  351. (*r_path_rids)[0] = begin_poly->owner->get_self();
  352. (*r_path_rids)[1] = begin_poly->owner->get_self();
  353. }
  354. if (r_path_owners) {
  355. r_path_owners->resize(2);
  356. r_path_owners->write[0] = begin_poly->owner->get_owner_id();
  357. r_path_owners->write[1] = begin_poly->owner->get_owner_id();
  358. }
  359. Vector<Vector3> path;
  360. path.resize(2);
  361. path.write[0] = begin_point;
  362. path.write[1] = end_point;
  363. return path;
  364. }
  365. Vector<Vector3> path;
  366. // Optimize the path.
  367. if (p_optimize) {
  368. // Set the apex poly/point to the end point
  369. gd::NavigationPoly *apex_poly = &navigation_polys[least_cost_id];
  370. Vector3 back_pathway[2] = { apex_poly->back_navigation_edge_pathway_start, apex_poly->back_navigation_edge_pathway_end };
  371. const Vector3 back_edge_closest_point = Geometry3D::get_closest_point_to_segment(end_point, back_pathway);
  372. if (end_point.is_equal_approx(back_edge_closest_point)) {
  373. // The end point is basically on top of the last crossed edge, funneling around the corners would at best do nothing.
  374. // At worst it would add an unwanted path point before the last point due to precision issues so skip to the next polygon.
  375. if (apex_poly->back_navigation_poly_id != -1) {
  376. apex_poly = &navigation_polys[apex_poly->back_navigation_poly_id];
  377. }
  378. }
  379. Vector3 apex_point = end_point;
  380. gd::NavigationPoly *left_poly = apex_poly;
  381. Vector3 left_portal = apex_point;
  382. gd::NavigationPoly *right_poly = apex_poly;
  383. Vector3 right_portal = apex_point;
  384. gd::NavigationPoly *p = apex_poly;
  385. path.push_back(end_point);
  386. APPEND_METADATA(end_poly);
  387. while (p) {
  388. // Set left and right points of the pathway between polygons.
  389. Vector3 left = p->back_navigation_edge_pathway_start;
  390. Vector3 right = p->back_navigation_edge_pathway_end;
  391. if (THREE_POINTS_CROSS_PRODUCT(apex_point, left, right).dot(p_map_up) < 0) {
  392. SWAP(left, right);
  393. }
  394. bool skip = false;
  395. if (THREE_POINTS_CROSS_PRODUCT(apex_point, left_portal, left).dot(p_map_up) >= 0) {
  396. //process
  397. if (left_portal == apex_point || THREE_POINTS_CROSS_PRODUCT(apex_point, left, right_portal).dot(p_map_up) > 0) {
  398. left_poly = p;
  399. left_portal = left;
  400. } else {
  401. clip_path(navigation_polys, path, apex_poly, right_portal, right_poly, r_path_types, r_path_rids, r_path_owners, p_map_up);
  402. apex_point = right_portal;
  403. p = right_poly;
  404. left_poly = p;
  405. apex_poly = p;
  406. left_portal = apex_point;
  407. right_portal = apex_point;
  408. path.push_back(apex_point);
  409. APPEND_METADATA(apex_poly->poly);
  410. skip = true;
  411. }
  412. }
  413. if (!skip && THREE_POINTS_CROSS_PRODUCT(apex_point, right_portal, right).dot(p_map_up) <= 0) {
  414. //process
  415. if (right_portal == apex_point || THREE_POINTS_CROSS_PRODUCT(apex_point, right, left_portal).dot(p_map_up) < 0) {
  416. right_poly = p;
  417. right_portal = right;
  418. } else {
  419. clip_path(navigation_polys, path, apex_poly, left_portal, left_poly, r_path_types, r_path_rids, r_path_owners, p_map_up);
  420. apex_point = left_portal;
  421. p = left_poly;
  422. right_poly = p;
  423. apex_poly = p;
  424. right_portal = apex_point;
  425. left_portal = apex_point;
  426. path.push_back(apex_point);
  427. APPEND_METADATA(apex_poly->poly);
  428. }
  429. }
  430. // Go to the previous polygon.
  431. if (p->back_navigation_poly_id != -1) {
  432. p = &navigation_polys[p->back_navigation_poly_id];
  433. } else {
  434. // The end
  435. p = nullptr;
  436. }
  437. }
  438. // If the last point is not the begin point, add it to the list.
  439. if (path[path.size() - 1] != begin_point) {
  440. path.push_back(begin_point);
  441. APPEND_METADATA(begin_poly);
  442. }
  443. path.reverse();
  444. if (r_path_types) {
  445. r_path_types->reverse();
  446. }
  447. if (r_path_rids) {
  448. r_path_rids->reverse();
  449. }
  450. if (r_path_owners) {
  451. r_path_owners->reverse();
  452. }
  453. } else {
  454. path.push_back(end_point);
  455. APPEND_METADATA(end_poly);
  456. // Add mid points
  457. int np_id = least_cost_id;
  458. while (np_id != -1 && navigation_polys[np_id].back_navigation_poly_id != -1) {
  459. if (navigation_polys[np_id].back_navigation_edge != -1) {
  460. int prev = navigation_polys[np_id].back_navigation_edge;
  461. int prev_n = (navigation_polys[np_id].back_navigation_edge + 1) % navigation_polys[np_id].poly->points.size();
  462. Vector3 point = (navigation_polys[np_id].poly->points[prev].pos + navigation_polys[np_id].poly->points[prev_n].pos) * 0.5;
  463. path.push_back(point);
  464. APPEND_METADATA(navigation_polys[np_id].poly);
  465. } else {
  466. path.push_back(navigation_polys[np_id].entry);
  467. APPEND_METADATA(navigation_polys[np_id].poly);
  468. }
  469. np_id = navigation_polys[np_id].back_navigation_poly_id;
  470. }
  471. path.push_back(begin_point);
  472. APPEND_METADATA(begin_poly);
  473. path.reverse();
  474. if (r_path_types) {
  475. r_path_types->reverse();
  476. }
  477. if (r_path_rids) {
  478. r_path_rids->reverse();
  479. }
  480. if (r_path_owners) {
  481. r_path_owners->reverse();
  482. }
  483. }
  484. // Ensure post conditions (path arrays MUST match in size).
  485. CRASH_COND(r_path_types && path.size() != r_path_types->size());
  486. CRASH_COND(r_path_rids && path.size() != r_path_rids->size());
  487. CRASH_COND(r_path_owners && path.size() != r_path_owners->size());
  488. return path;
  489. }
  490. Vector3 NavMeshQueries3D::polygons_get_closest_point_to_segment(const LocalVector<gd::Polygon> &p_polygons, const Vector3 &p_from, const Vector3 &p_to, const bool p_use_collision) {
  491. bool use_collision = p_use_collision;
  492. Vector3 closest_point;
  493. real_t closest_point_distance = FLT_MAX;
  494. for (const gd::Polygon &polygon : p_polygons) {
  495. // For each face check the distance to the segment.
  496. for (size_t point_id = 2; point_id < polygon.points.size(); point_id += 1) {
  497. const Face3 face(polygon.points[0].pos, polygon.points[point_id - 1].pos, polygon.points[point_id].pos);
  498. Vector3 intersection_point;
  499. if (face.intersects_segment(p_from, p_to, &intersection_point)) {
  500. const real_t d = p_from.distance_to(intersection_point);
  501. if (!use_collision) {
  502. closest_point = intersection_point;
  503. use_collision = true;
  504. closest_point_distance = d;
  505. } else if (closest_point_distance > d) {
  506. closest_point = intersection_point;
  507. closest_point_distance = d;
  508. }
  509. }
  510. // If segment does not itersect face, check the distance from segment's endpoints.
  511. else if (!use_collision) {
  512. const Vector3 p_from_closest = face.get_closest_point_to(p_from);
  513. const real_t d_p_from = p_from.distance_to(p_from_closest);
  514. if (closest_point_distance > d_p_from) {
  515. closest_point = p_from_closest;
  516. closest_point_distance = d_p_from;
  517. }
  518. const Vector3 p_to_closest = face.get_closest_point_to(p_to);
  519. const real_t d_p_to = p_to.distance_to(p_to_closest);
  520. if (closest_point_distance > d_p_to) {
  521. closest_point = p_to_closest;
  522. closest_point_distance = d_p_to;
  523. }
  524. }
  525. }
  526. // Finally, check for a case when shortest distance is between some point located on a face's edge and some point located on a line segment.
  527. if (!use_collision) {
  528. for (size_t point_id = 0; point_id < polygon.points.size(); point_id += 1) {
  529. Vector3 a, b;
  530. Geometry3D::get_closest_points_between_segments(
  531. p_from,
  532. p_to,
  533. polygon.points[point_id].pos,
  534. polygon.points[(point_id + 1) % polygon.points.size()].pos,
  535. a,
  536. b);
  537. const real_t d = a.distance_to(b);
  538. if (d < closest_point_distance) {
  539. closest_point_distance = d;
  540. closest_point = b;
  541. }
  542. }
  543. }
  544. }
  545. return closest_point;
  546. }
  547. Vector3 NavMeshQueries3D::polygons_get_closest_point(const LocalVector<gd::Polygon> &p_polygons, const Vector3 &p_point) {
  548. gd::ClosestPointQueryResult cp = polygons_get_closest_point_info(p_polygons, p_point);
  549. return cp.point;
  550. }
  551. Vector3 NavMeshQueries3D::polygons_get_closest_point_normal(const LocalVector<gd::Polygon> &p_polygons, const Vector3 &p_point) {
  552. gd::ClosestPointQueryResult cp = polygons_get_closest_point_info(p_polygons, p_point);
  553. return cp.normal;
  554. }
  555. gd::ClosestPointQueryResult NavMeshQueries3D::polygons_get_closest_point_info(const LocalVector<gd::Polygon> &p_polygons, const Vector3 &p_point) {
  556. gd::ClosestPointQueryResult result;
  557. real_t closest_point_distance_squared = FLT_MAX;
  558. for (const gd::Polygon &polygon : p_polygons) {
  559. for (size_t point_id = 2; point_id < polygon.points.size(); point_id += 1) {
  560. const Face3 face(polygon.points[0].pos, polygon.points[point_id - 1].pos, polygon.points[point_id].pos);
  561. const Vector3 closest_point_on_face = face.get_closest_point_to(p_point);
  562. const real_t distance_squared_to_point = closest_point_on_face.distance_squared_to(p_point);
  563. if (distance_squared_to_point < closest_point_distance_squared) {
  564. result.point = closest_point_on_face;
  565. result.normal = face.get_plane().normal;
  566. result.owner = polygon.owner->get_self();
  567. closest_point_distance_squared = distance_squared_to_point;
  568. }
  569. }
  570. }
  571. return result;
  572. }
  573. RID NavMeshQueries3D::polygons_get_closest_point_owner(const LocalVector<gd::Polygon> &p_polygons, const Vector3 &p_point) {
  574. gd::ClosestPointQueryResult cp = polygons_get_closest_point_info(p_polygons, p_point);
  575. return cp.owner;
  576. }
  577. void NavMeshQueries3D::clip_path(const LocalVector<gd::NavigationPoly> &p_navigation_polys, Vector<Vector3> &path, const gd::NavigationPoly *from_poly, const Vector3 &p_to_point, const gd::NavigationPoly *p_to_poly, Vector<int32_t> *r_path_types, TypedArray<RID> *r_path_rids, Vector<int64_t> *r_path_owners, const Vector3 &p_map_up) {
  578. Vector3 from = path[path.size() - 1];
  579. if (from.is_equal_approx(p_to_point)) {
  580. return;
  581. }
  582. Plane cut_plane;
  583. cut_plane.normal = (from - p_to_point).cross(p_map_up);
  584. if (cut_plane.normal == Vector3()) {
  585. return;
  586. }
  587. cut_plane.normal.normalize();
  588. cut_plane.d = cut_plane.normal.dot(from);
  589. while (from_poly != p_to_poly) {
  590. Vector3 pathway_start = from_poly->back_navigation_edge_pathway_start;
  591. Vector3 pathway_end = from_poly->back_navigation_edge_pathway_end;
  592. ERR_FAIL_COND(from_poly->back_navigation_poly_id == -1);
  593. from_poly = &p_navigation_polys[from_poly->back_navigation_poly_id];
  594. if (!pathway_start.is_equal_approx(pathway_end)) {
  595. Vector3 inters;
  596. if (cut_plane.intersects_segment(pathway_start, pathway_end, &inters)) {
  597. if (!inters.is_equal_approx(p_to_point) && !inters.is_equal_approx(path[path.size() - 1])) {
  598. path.push_back(inters);
  599. APPEND_METADATA(from_poly->poly);
  600. }
  601. }
  602. }
  603. }
  604. }
  605. #endif // _3D_DISABLED