nav_map.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /*************************************************************************/
  2. /* nav_map.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. #include "nav_map.h"
  31. #include "core/os/threaded_array_processor.h"
  32. #include "nav_region.h"
  33. #include "rvo_agent.h"
  34. #include <algorithm>
  35. /**
  36. @author AndreaCatania
  37. */
  38. #define USE_ENTRY_POINT
  39. void NavMap::set_up(Vector3 p_up) {
  40. up = p_up;
  41. regenerate_polygons = true;
  42. }
  43. void NavMap::set_cell_size(float p_cell_size) {
  44. cell_size = p_cell_size;
  45. regenerate_polygons = true;
  46. }
  47. void NavMap::set_edge_connection_margin(float p_edge_connection_margin) {
  48. edge_connection_margin = p_edge_connection_margin;
  49. regenerate_links = true;
  50. }
  51. gd::PointKey NavMap::get_point_key(const Vector3 &p_pos) const {
  52. const int x = int(Math::floor(p_pos.x / cell_size));
  53. const int y = int(Math::floor(p_pos.y / cell_size));
  54. const int z = int(Math::floor(p_pos.z / cell_size));
  55. gd::PointKey p;
  56. p.key = 0;
  57. p.x = x;
  58. p.y = y;
  59. p.z = z;
  60. return p;
  61. }
  62. Vector<Vector3> NavMap::get_path(Vector3 p_origin, Vector3 p_destination, bool p_optimize) const {
  63. const gd::Polygon *begin_poly = nullptr;
  64. const gd::Polygon *end_poly = nullptr;
  65. Vector3 begin_point;
  66. Vector3 end_point;
  67. float begin_d = 1e20;
  68. float end_d = 1e20;
  69. // Find the initial poly and the end poly on this map.
  70. for (size_t i(0); i < polygons.size(); i++) {
  71. const gd::Polygon &p = polygons[i];
  72. // For each point cast a face and check the distance between the origin/destination
  73. for (size_t point_id = 2; point_id < p.points.size(); point_id++) {
  74. Face3 f(p.points[point_id - 2].pos, p.points[point_id - 1].pos, p.points[point_id].pos);
  75. Vector3 spoint = f.get_closest_point_to(p_origin);
  76. float dpoint = spoint.distance_to(p_origin);
  77. if (dpoint < begin_d) {
  78. begin_d = dpoint;
  79. begin_poly = &p;
  80. begin_point = spoint;
  81. }
  82. spoint = f.get_closest_point_to(p_destination);
  83. dpoint = spoint.distance_to(p_destination);
  84. if (dpoint < end_d) {
  85. end_d = dpoint;
  86. end_poly = &p;
  87. end_point = spoint;
  88. }
  89. }
  90. }
  91. if (!begin_poly || !end_poly) {
  92. // No path
  93. return Vector<Vector3>();
  94. }
  95. if (begin_poly == end_poly) {
  96. Vector<Vector3> path;
  97. path.resize(2);
  98. path.write[0] = begin_point;
  99. path.write[1] = end_point;
  100. return path;
  101. }
  102. std::vector<gd::NavigationPoly> navigation_polys;
  103. navigation_polys.reserve(polygons.size() * 0.75);
  104. // The elements indices in the `navigation_polys`.
  105. int least_cost_id(-1);
  106. List<uint32_t> open_list;
  107. bool found_route = false;
  108. navigation_polys.push_back(gd::NavigationPoly(begin_poly));
  109. {
  110. least_cost_id = 0;
  111. gd::NavigationPoly *least_cost_poly = &navigation_polys[least_cost_id];
  112. least_cost_poly->self_id = least_cost_id;
  113. least_cost_poly->entry = begin_point;
  114. }
  115. open_list.push_back(0);
  116. const gd::Polygon *reachable_end = nullptr;
  117. float reachable_d = 1e30;
  118. bool is_reachable = true;
  119. while (found_route == false) {
  120. {
  121. // Takes the current least_cost_poly neighbors and compute the traveled_distance of each
  122. for (size_t i = 0; i < navigation_polys[least_cost_id].poly->edges.size(); i++) {
  123. gd::NavigationPoly *least_cost_poly = &navigation_polys[least_cost_id];
  124. const gd::Edge &edge = least_cost_poly->poly->edges[i];
  125. if (!edge.other_polygon) {
  126. continue;
  127. }
  128. #ifdef USE_ENTRY_POINT
  129. Vector3 edge_line[2] = {
  130. least_cost_poly->poly->points[i].pos,
  131. least_cost_poly->poly->points[(i + 1) % least_cost_poly->poly->points.size()].pos
  132. };
  133. const Vector3 new_entry = Geometry3D::get_closest_point_to_segment(least_cost_poly->entry, edge_line);
  134. const float new_distance = least_cost_poly->entry.distance_to(new_entry) + least_cost_poly->traveled_distance;
  135. #else
  136. const float new_distance = least_cost_poly->poly->center.distance_to(edge.other_polygon->center) + least_cost_poly->traveled_distance;
  137. #endif
  138. auto it = std::find(
  139. navigation_polys.begin(),
  140. navigation_polys.end(),
  141. gd::NavigationPoly(edge.other_polygon));
  142. if (it != navigation_polys.end()) {
  143. // Oh this was visited already, can we win the cost?
  144. if (it->traveled_distance > new_distance) {
  145. it->prev_navigation_poly_id = least_cost_id;
  146. it->back_navigation_edge = edge.other_edge;
  147. it->traveled_distance = new_distance;
  148. #ifdef USE_ENTRY_POINT
  149. it->entry = new_entry;
  150. #endif
  151. }
  152. } else {
  153. // Add to open neighbours
  154. navigation_polys.push_back(gd::NavigationPoly(edge.other_polygon));
  155. gd::NavigationPoly *np = &navigation_polys[navigation_polys.size() - 1];
  156. np->self_id = navigation_polys.size() - 1;
  157. np->prev_navigation_poly_id = least_cost_id;
  158. np->back_navigation_edge = edge.other_edge;
  159. np->traveled_distance = new_distance;
  160. #ifdef USE_ENTRY_POINT
  161. np->entry = new_entry;
  162. #endif
  163. open_list.push_back(navigation_polys.size() - 1);
  164. }
  165. }
  166. }
  167. // Removes the least cost polygon from the open list so we can advance.
  168. open_list.erase(least_cost_id);
  169. if (open_list.size() == 0) {
  170. // When the open list is empty at this point the End Polygon is not reachable
  171. // so use the further reachable polygon
  172. ERR_BREAK_MSG(is_reachable == false, "It's not expect to not find the most reachable polygons");
  173. is_reachable = false;
  174. if (reachable_end == nullptr) {
  175. // The path is not found and there is not a way out.
  176. break;
  177. }
  178. // Set as end point the furthest reachable point.
  179. end_poly = reachable_end;
  180. end_d = 1e20;
  181. for (size_t point_id = 2; point_id < end_poly->points.size(); point_id++) {
  182. Face3 f(end_poly->points[point_id - 2].pos, end_poly->points[point_id - 1].pos, end_poly->points[point_id].pos);
  183. Vector3 spoint = f.get_closest_point_to(p_destination);
  184. float dpoint = spoint.distance_to(p_destination);
  185. if (dpoint < end_d) {
  186. end_point = spoint;
  187. end_d = dpoint;
  188. }
  189. }
  190. // Reset open and navigation_polys
  191. gd::NavigationPoly np = navigation_polys[0];
  192. navigation_polys.clear();
  193. navigation_polys.push_back(np);
  194. open_list.clear();
  195. open_list.push_back(0);
  196. reachable_end = nullptr;
  197. continue;
  198. }
  199. // Now take the new least_cost_poly from the open list.
  200. least_cost_id = -1;
  201. float least_cost = 1e30;
  202. for (auto element = open_list.front(); element != nullptr; element = element->next()) {
  203. gd::NavigationPoly *np = &navigation_polys[element->get()];
  204. float cost = np->traveled_distance;
  205. #ifdef USE_ENTRY_POINT
  206. cost += np->entry.distance_to(end_point);
  207. #else
  208. cost += np->poly->center.distance_to(end_point);
  209. #endif
  210. if (cost < least_cost) {
  211. least_cost_id = np->self_id;
  212. least_cost = cost;
  213. }
  214. }
  215. // Stores the further reachable end polygon, in case our goal is not reachable.
  216. if (is_reachable) {
  217. float d = navigation_polys[least_cost_id].entry.distance_to(p_destination);
  218. if (reachable_d > d) {
  219. reachable_d = d;
  220. reachable_end = navigation_polys[least_cost_id].poly;
  221. }
  222. }
  223. ERR_BREAK(least_cost_id == -1);
  224. // Check if we reached the end
  225. if (navigation_polys[least_cost_id].poly == end_poly) {
  226. // Yep, done!!
  227. found_route = true;
  228. break;
  229. }
  230. }
  231. if (found_route) {
  232. Vector<Vector3> path;
  233. if (p_optimize) {
  234. // String pulling
  235. gd::NavigationPoly *apex_poly = &navigation_polys[least_cost_id];
  236. Vector3 apex_point = end_point;
  237. Vector3 portal_left = apex_point;
  238. Vector3 portal_right = apex_point;
  239. gd::NavigationPoly *left_poly = apex_poly;
  240. gd::NavigationPoly *right_poly = apex_poly;
  241. gd::NavigationPoly *p = apex_poly;
  242. path.push_back(end_point);
  243. while (p) {
  244. Vector3 left;
  245. Vector3 right;
  246. #define CLOCK_TANGENT(m_a, m_b, m_c) (((m_a) - (m_c)).cross((m_a) - (m_b)))
  247. if (p->poly == begin_poly) {
  248. left = begin_point;
  249. right = begin_point;
  250. } else {
  251. int prev = p->back_navigation_edge;
  252. int prev_n = (p->back_navigation_edge + 1) % p->poly->points.size();
  253. left = p->poly->points[prev].pos;
  254. right = p->poly->points[prev_n].pos;
  255. if (p->poly->clockwise) {
  256. SWAP(left, right);
  257. }
  258. }
  259. bool skip = false;
  260. if (CLOCK_TANGENT(apex_point, portal_left, left).dot(up) >= 0) {
  261. //process
  262. if (portal_left == apex_point || CLOCK_TANGENT(apex_point, left, portal_right).dot(up) > 0) {
  263. left_poly = p;
  264. portal_left = left;
  265. } else {
  266. clip_path(navigation_polys, path, apex_poly, portal_right, right_poly);
  267. apex_point = portal_right;
  268. p = right_poly;
  269. left_poly = p;
  270. apex_poly = p;
  271. portal_left = apex_point;
  272. portal_right = apex_point;
  273. path.push_back(apex_point);
  274. skip = true;
  275. }
  276. }
  277. if (!skip && CLOCK_TANGENT(apex_point, portal_right, right).dot(up) <= 0) {
  278. //process
  279. if (portal_right == apex_point || CLOCK_TANGENT(apex_point, right, portal_left).dot(up) < 0) {
  280. right_poly = p;
  281. portal_right = right;
  282. } else {
  283. clip_path(navigation_polys, path, apex_poly, portal_left, left_poly);
  284. apex_point = portal_left;
  285. p = left_poly;
  286. right_poly = p;
  287. apex_poly = p;
  288. portal_right = apex_point;
  289. portal_left = apex_point;
  290. path.push_back(apex_point);
  291. }
  292. }
  293. if (p->prev_navigation_poly_id != -1) {
  294. p = &navigation_polys[p->prev_navigation_poly_id];
  295. } else {
  296. // The end
  297. p = nullptr;
  298. }
  299. }
  300. if (path[path.size() - 1] != begin_point) {
  301. path.push_back(begin_point);
  302. }
  303. path.invert();
  304. } else {
  305. path.push_back(end_point);
  306. // Add mid points
  307. int np_id = least_cost_id;
  308. while (np_id != -1) {
  309. #ifdef USE_ENTRY_POINT
  310. Vector3 point = navigation_polys[np_id].entry;
  311. #else
  312. int prev = navigation_polys[np_id].back_navigation_edge;
  313. int prev_n = (navigation_polys[np_id].back_navigation_edge + 1) % navigation_polys[np_id].poly->points.size();
  314. Vector3 point = (navigation_polys[np_id].poly->points[prev].pos + navigation_polys[np_id].poly->points[prev_n].pos) * 0.5;
  315. #endif
  316. path.push_back(point);
  317. np_id = navigation_polys[np_id].prev_navigation_poly_id;
  318. }
  319. path.invert();
  320. }
  321. return path;
  322. }
  323. return Vector<Vector3>();
  324. }
  325. Vector3 NavMap::get_closest_point_to_segment(const Vector3 &p_from, const Vector3 &p_to, const bool p_use_collision) const {
  326. bool use_collision = p_use_collision;
  327. Vector3 closest_point;
  328. real_t closest_point_d = 1e20;
  329. // Find the initial poly and the end poly on this map.
  330. for (size_t i(0); i < polygons.size(); i++) {
  331. const gd::Polygon &p = polygons[i];
  332. // For each point cast a face and check the distance to the segment
  333. for (size_t point_id = 2; point_id < p.points.size(); point_id += 1) {
  334. const Face3 f(p.points[point_id - 2].pos, p.points[point_id - 1].pos, p.points[point_id].pos);
  335. Vector3 inters;
  336. if (f.intersects_segment(p_from, p_to, &inters)) {
  337. const real_t d = closest_point_d = p_from.distance_to(inters);
  338. if (use_collision == false) {
  339. closest_point = inters;
  340. use_collision = true;
  341. closest_point_d = d;
  342. } else if (closest_point_d > d) {
  343. closest_point = inters;
  344. closest_point_d = d;
  345. }
  346. }
  347. }
  348. if (use_collision == false) {
  349. for (size_t point_id = 0; point_id < p.points.size(); point_id += 1) {
  350. Vector3 a, b;
  351. Geometry3D::get_closest_points_between_segments(
  352. p_from,
  353. p_to,
  354. p.points[point_id].pos,
  355. p.points[(point_id + 1) % p.points.size()].pos,
  356. a,
  357. b);
  358. const real_t d = a.distance_to(b);
  359. if (d < closest_point_d) {
  360. closest_point_d = d;
  361. closest_point = b;
  362. }
  363. }
  364. }
  365. }
  366. return closest_point;
  367. }
  368. Vector3 NavMap::get_closest_point(const Vector3 &p_point) const {
  369. // TODO this is really not optimal, please redesign the API to directly return all this data
  370. Vector3 closest_point;
  371. real_t closest_point_d = 1e20;
  372. // Find the initial poly and the end poly on this map.
  373. for (size_t i(0); i < polygons.size(); i++) {
  374. const gd::Polygon &p = polygons[i];
  375. // For each point cast a face and check the distance to the point
  376. for (size_t point_id = 2; point_id < p.points.size(); point_id += 1) {
  377. const Face3 f(p.points[point_id - 2].pos, p.points[point_id - 1].pos, p.points[point_id].pos);
  378. const Vector3 inters = f.get_closest_point_to(p_point);
  379. const real_t d = inters.distance_to(p_point);
  380. if (d < closest_point_d) {
  381. closest_point = inters;
  382. closest_point_d = d;
  383. }
  384. }
  385. }
  386. return closest_point;
  387. }
  388. Vector3 NavMap::get_closest_point_normal(const Vector3 &p_point) const {
  389. // TODO this is really not optimal, please redesign the API to directly return all this data
  390. Vector3 closest_point;
  391. Vector3 closest_point_normal;
  392. real_t closest_point_d = 1e20;
  393. // Find the initial poly and the end poly on this map.
  394. for (size_t i(0); i < polygons.size(); i++) {
  395. const gd::Polygon &p = polygons[i];
  396. // For each point cast a face and check the distance to the point
  397. for (size_t point_id = 2; point_id < p.points.size(); point_id += 1) {
  398. const Face3 f(p.points[point_id - 2].pos, p.points[point_id - 1].pos, p.points[point_id].pos);
  399. const Vector3 inters = f.get_closest_point_to(p_point);
  400. const real_t d = inters.distance_to(p_point);
  401. if (d < closest_point_d) {
  402. closest_point = inters;
  403. closest_point_normal = f.get_plane().normal;
  404. closest_point_d = d;
  405. }
  406. }
  407. }
  408. return closest_point_normal;
  409. }
  410. RID NavMap::get_closest_point_owner(const Vector3 &p_point) const {
  411. // TODO this is really not optimal, please redesign the API to directly return all this data
  412. Vector3 closest_point;
  413. RID closest_point_owner;
  414. real_t closest_point_d = 1e20;
  415. // Find the initial poly and the end poly on this map.
  416. for (size_t i(0); i < polygons.size(); i++) {
  417. const gd::Polygon &p = polygons[i];
  418. // For each point cast a face and check the distance to the point
  419. for (size_t point_id = 2; point_id < p.points.size(); point_id += 1) {
  420. const Face3 f(p.points[point_id - 2].pos, p.points[point_id - 1].pos, p.points[point_id].pos);
  421. const Vector3 inters = f.get_closest_point_to(p_point);
  422. const real_t d = inters.distance_to(p_point);
  423. if (d < closest_point_d) {
  424. closest_point = inters;
  425. closest_point_owner = p.owner->get_self();
  426. closest_point_d = d;
  427. }
  428. }
  429. }
  430. return closest_point_owner;
  431. }
  432. void NavMap::add_region(NavRegion *p_region) {
  433. regions.push_back(p_region);
  434. regenerate_links = true;
  435. }
  436. void NavMap::remove_region(NavRegion *p_region) {
  437. std::vector<NavRegion *>::iterator it = std::find(regions.begin(), regions.end(), p_region);
  438. if (it != regions.end()) {
  439. regions.erase(it);
  440. regenerate_links = true;
  441. }
  442. }
  443. bool NavMap::has_agent(RvoAgent *agent) const {
  444. return std::find(agents.begin(), agents.end(), agent) != agents.end();
  445. }
  446. void NavMap::add_agent(RvoAgent *agent) {
  447. if (!has_agent(agent)) {
  448. agents.push_back(agent);
  449. agents_dirty = true;
  450. }
  451. }
  452. void NavMap::remove_agent(RvoAgent *agent) {
  453. remove_agent_as_controlled(agent);
  454. auto it = std::find(agents.begin(), agents.end(), agent);
  455. if (it != agents.end()) {
  456. agents.erase(it);
  457. agents_dirty = true;
  458. }
  459. }
  460. void NavMap::set_agent_as_controlled(RvoAgent *agent) {
  461. const bool exist = std::find(controlled_agents.begin(), controlled_agents.end(), agent) != controlled_agents.end();
  462. if (!exist) {
  463. ERR_FAIL_COND(!has_agent(agent));
  464. controlled_agents.push_back(agent);
  465. }
  466. }
  467. void NavMap::remove_agent_as_controlled(RvoAgent *agent) {
  468. auto it = std::find(controlled_agents.begin(), controlled_agents.end(), agent);
  469. if (it != controlled_agents.end()) {
  470. controlled_agents.erase(it);
  471. }
  472. }
  473. void NavMap::sync() {
  474. if (regenerate_polygons) {
  475. for (size_t r(0); r < regions.size(); r++) {
  476. regions[r]->scratch_polygons();
  477. }
  478. regenerate_links = true;
  479. }
  480. for (size_t r(0); r < regions.size(); r++) {
  481. if (regions[r]->sync()) {
  482. regenerate_links = true;
  483. }
  484. }
  485. if (regenerate_links) {
  486. // Copy all region polygons in the map.
  487. int count = 0;
  488. for (size_t r(0); r < regions.size(); r++) {
  489. count += regions[r]->get_polygons().size();
  490. }
  491. polygons.resize(count);
  492. count = 0;
  493. for (size_t r(0); r < regions.size(); r++) {
  494. std::copy(
  495. regions[r]->get_polygons().data(),
  496. regions[r]->get_polygons().data() + regions[r]->get_polygons().size(),
  497. polygons.begin() + count);
  498. count += regions[r]->get_polygons().size();
  499. }
  500. // Connects the `Edges` of all the `Polygons` of all `Regions` each other.
  501. Map<gd::EdgeKey, gd::Connection> connections;
  502. for (size_t poly_id(0); poly_id < polygons.size(); poly_id++) {
  503. gd::Polygon &poly(polygons[poly_id]);
  504. for (size_t p(0); p < poly.points.size(); p++) {
  505. int next_point = (p + 1) % poly.points.size();
  506. gd::EdgeKey ek(poly.points[p].key, poly.points[next_point].key);
  507. Map<gd::EdgeKey, gd::Connection>::Element *connection = connections.find(ek);
  508. if (!connection) {
  509. // Nothing yet
  510. gd::Connection c;
  511. c.A = &poly;
  512. c.A_edge = p;
  513. c.B = nullptr;
  514. c.B_edge = -1;
  515. connections[ek] = c;
  516. } else if (connection->get().B == nullptr) {
  517. CRASH_COND(connection->get().A == nullptr); // Unreachable
  518. // Connect the two Polygons by this edge
  519. connection->get().B = &poly;
  520. connection->get().B_edge = p;
  521. connection->get().A->edges[connection->get().A_edge].this_edge = connection->get().A_edge;
  522. connection->get().A->edges[connection->get().A_edge].other_polygon = connection->get().B;
  523. connection->get().A->edges[connection->get().A_edge].other_edge = connection->get().B_edge;
  524. connection->get().B->edges[connection->get().B_edge].this_edge = connection->get().B_edge;
  525. connection->get().B->edges[connection->get().B_edge].other_polygon = connection->get().A;
  526. connection->get().B->edges[connection->get().B_edge].other_edge = connection->get().A_edge;
  527. } else {
  528. // The edge is already connected with another edge, skip.
  529. ERR_PRINT("Attempted to merge a navigation mesh triangle edge with another already-merged edge. This happens when the Navigation3D's `cell_size` is different from the one used to generate the navigation mesh. This will cause navigation problem.");
  530. }
  531. }
  532. }
  533. // Takes all the free edges.
  534. std::vector<gd::FreeEdge> free_edges;
  535. free_edges.reserve(connections.size());
  536. for (auto connection_element = connections.front(); connection_element; connection_element = connection_element->next()) {
  537. if (connection_element->get().B == nullptr) {
  538. CRASH_COND(connection_element->get().A == nullptr); // Unreachable
  539. CRASH_COND(connection_element->get().A_edge < 0); // Unreachable
  540. // This is a free edge
  541. uint32_t id(free_edges.size());
  542. free_edges.push_back(gd::FreeEdge());
  543. free_edges[id].is_free = true;
  544. free_edges[id].poly = connection_element->get().A;
  545. free_edges[id].edge_id = connection_element->get().A_edge;
  546. uint32_t point_0(free_edges[id].edge_id);
  547. uint32_t point_1((free_edges[id].edge_id + 1) % free_edges[id].poly->points.size());
  548. Vector3 pos_0 = free_edges[id].poly->points[point_0].pos;
  549. Vector3 pos_1 = free_edges[id].poly->points[point_1].pos;
  550. Vector3 relative = pos_1 - pos_0;
  551. free_edges[id].edge_center = (pos_0 + pos_1) / 2.0;
  552. free_edges[id].edge_dir = relative.normalized();
  553. free_edges[id].edge_len_squared = relative.length_squared();
  554. }
  555. }
  556. const float ecm_squared(edge_connection_margin * edge_connection_margin);
  557. #define LEN_TOLLERANCE 0.1
  558. #define DIR_TOLLERANCE 0.9
  559. // In front of tolerance
  560. #define IFO_TOLLERANCE 0.5
  561. // Find the compatible near edges.
  562. //
  563. // Note:
  564. // Considering that the edges must be compatible (for obvious reasons)
  565. // to be connected, create new polygons to remove that small gap is
  566. // not really useful and would result in wasteful computation during
  567. // connection, integration and path finding.
  568. for (size_t i(0); i < free_edges.size(); i++) {
  569. if (!free_edges[i].is_free) {
  570. continue;
  571. }
  572. gd::FreeEdge &edge = free_edges[i];
  573. for (size_t y(0); y < free_edges.size(); y++) {
  574. gd::FreeEdge &other_edge = free_edges[y];
  575. if (i == y || !other_edge.is_free || edge.poly->owner == other_edge.poly->owner) {
  576. continue;
  577. }
  578. Vector3 rel_centers = other_edge.edge_center - edge.edge_center;
  579. if (ecm_squared > rel_centers.length_squared() // Are enough closer?
  580. && ABS(edge.edge_len_squared - other_edge.edge_len_squared) < LEN_TOLLERANCE // Are the same length?
  581. && ABS(edge.edge_dir.dot(other_edge.edge_dir)) > DIR_TOLLERANCE // Are aligned?
  582. && ABS(rel_centers.normalized().dot(edge.edge_dir)) < IFO_TOLLERANCE // Are one in front the other?
  583. ) {
  584. // The edges can be connected
  585. edge.is_free = false;
  586. other_edge.is_free = false;
  587. edge.poly->edges[edge.edge_id].this_edge = edge.edge_id;
  588. edge.poly->edges[edge.edge_id].other_edge = other_edge.edge_id;
  589. edge.poly->edges[edge.edge_id].other_polygon = other_edge.poly;
  590. other_edge.poly->edges[other_edge.edge_id].this_edge = other_edge.edge_id;
  591. other_edge.poly->edges[other_edge.edge_id].other_edge = edge.edge_id;
  592. other_edge.poly->edges[other_edge.edge_id].other_polygon = edge.poly;
  593. }
  594. }
  595. }
  596. }
  597. if (regenerate_links) {
  598. map_update_id = (map_update_id + 1) % 9999999;
  599. }
  600. if (agents_dirty) {
  601. std::vector<RVO::Agent *> raw_agents;
  602. raw_agents.reserve(agents.size());
  603. for (size_t i(0); i < agents.size(); i++) {
  604. raw_agents.push_back(agents[i]->get_agent());
  605. }
  606. rvo.buildAgentTree(raw_agents);
  607. }
  608. regenerate_polygons = false;
  609. regenerate_links = false;
  610. agents_dirty = false;
  611. }
  612. void NavMap::compute_single_step(uint32_t index, RvoAgent **agent) {
  613. (*(agent + index))->get_agent()->computeNeighbors(&rvo);
  614. (*(agent + index))->get_agent()->computeNewVelocity(deltatime);
  615. }
  616. void NavMap::step(real_t p_deltatime) {
  617. deltatime = p_deltatime;
  618. if (controlled_agents.size() > 0) {
  619. thread_process_array(
  620. controlled_agents.size(),
  621. this,
  622. &NavMap::compute_single_step,
  623. controlled_agents.data());
  624. }
  625. }
  626. void NavMap::dispatch_callbacks() {
  627. for (int i(0); i < static_cast<int>(controlled_agents.size()); i++) {
  628. controlled_agents[i]->dispatch_callback();
  629. }
  630. }
  631. void NavMap::clip_path(const std::vector<gd::NavigationPoly> &p_navigation_polys, Vector<Vector3> &path, const gd::NavigationPoly *from_poly, const Vector3 &p_to_point, const gd::NavigationPoly *p_to_poly) const {
  632. Vector3 from = path[path.size() - 1];
  633. if (from.distance_to(p_to_point) < CMP_EPSILON) {
  634. return;
  635. }
  636. Plane cut_plane;
  637. cut_plane.normal = (from - p_to_point).cross(up);
  638. if (cut_plane.normal == Vector3()) {
  639. return;
  640. }
  641. cut_plane.normal.normalize();
  642. cut_plane.d = cut_plane.normal.dot(from);
  643. while (from_poly != p_to_poly) {
  644. int back_nav_edge = from_poly->back_navigation_edge;
  645. Vector3 a = from_poly->poly->points[back_nav_edge].pos;
  646. Vector3 b = from_poly->poly->points[(back_nav_edge + 1) % from_poly->poly->points.size()].pos;
  647. ERR_FAIL_COND(from_poly->prev_navigation_poly_id == -1);
  648. from_poly = &p_navigation_polys[from_poly->prev_navigation_poly_id];
  649. if (a.distance_to(b) > CMP_EPSILON) {
  650. Vector3 inters;
  651. if (cut_plane.intersects_segment(a, b, &inters)) {
  652. if (inters.distance_to(p_to_point) > CMP_EPSILON && inters.distance_to(path[path.size() - 1]) > CMP_EPSILON) {
  653. path.push_back(inters);
  654. }
  655. }
  656. }
  657. }
  658. }