navigation2d.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /*************************************************************************/
  2. /* navigation2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "navigation2d.h"
  31. #define USE_ENTRY_POINT
  32. void Navigation2D::_navpoly_link(int p_id) {
  33. ERR_FAIL_COND(!navpoly_map.has(p_id));
  34. NavMesh &nm = navpoly_map[p_id];
  35. ERR_FAIL_COND(nm.linked);
  36. PoolVector<Vector2> vertices = nm.navpoly->get_vertices();
  37. int len = vertices.size();
  38. if (len == 0)
  39. return;
  40. PoolVector<Vector2>::Read r = vertices.read();
  41. for (int i = 0; i < nm.navpoly->get_polygon_count(); i++) {
  42. //build
  43. List<Polygon>::Element *P = nm.polygons.push_back(Polygon());
  44. Polygon &p = P->get();
  45. p.owner = &nm;
  46. Vector<int> poly = nm.navpoly->get_polygon(i);
  47. int plen = poly.size();
  48. const int *indices = poly.ptr();
  49. bool valid = true;
  50. p.edges.resize(plen);
  51. Vector2 center;
  52. float sum = 0;
  53. for (int j = 0; j < plen; j++) {
  54. int idx = indices[j];
  55. if (idx < 0 || idx >= len) {
  56. valid = false;
  57. break;
  58. }
  59. Polygon::Edge e;
  60. Vector2 ep = nm.xform.xform(r[idx]);
  61. center += ep;
  62. e.point = _get_point(ep);
  63. p.edges.write[j] = e;
  64. int idxn = indices[(j + 1) % plen];
  65. if (idxn < 0 || idxn >= len) {
  66. valid = false;
  67. break;
  68. }
  69. Vector2 epn = nm.xform.xform(r[idxn]);
  70. sum += (epn.x - ep.x) * (epn.y + ep.y);
  71. }
  72. p.clockwise = sum > 0;
  73. if (!valid) {
  74. nm.polygons.pop_back();
  75. ERR_CONTINUE(!valid);
  76. continue;
  77. }
  78. p.center = center / plen;
  79. //connect
  80. for (int j = 0; j < plen; j++) {
  81. int next = (j + 1) % plen;
  82. EdgeKey ek(p.edges[j].point, p.edges[next].point);
  83. Map<EdgeKey, Connection>::Element *C = connections.find(ek);
  84. if (!C) {
  85. Connection c;
  86. c.A = &p;
  87. c.A_edge = j;
  88. c.B = NULL;
  89. c.B_edge = -1;
  90. connections[ek] = c;
  91. } else {
  92. if (C->get().B != NULL) {
  93. ConnectionPending pending;
  94. pending.polygon = &p;
  95. pending.edge = j;
  96. p.edges.write[j].P = C->get().pending.push_back(pending);
  97. continue;
  98. }
  99. C->get().B = &p;
  100. C->get().B_edge = j;
  101. C->get().A->edges.write[C->get().A_edge].C = &p;
  102. C->get().A->edges.write[C->get().A_edge].C_edge = j;
  103. p.edges.write[j].C = C->get().A;
  104. p.edges.write[j].C_edge = C->get().A_edge;
  105. //connection successful.
  106. }
  107. }
  108. }
  109. nm.linked = true;
  110. }
  111. void Navigation2D::_navpoly_unlink(int p_id) {
  112. ERR_FAIL_COND(!navpoly_map.has(p_id));
  113. NavMesh &nm = navpoly_map[p_id];
  114. ERR_FAIL_COND(!nm.linked);
  115. for (List<Polygon>::Element *E = nm.polygons.front(); E; E = E->next()) {
  116. Polygon &p = E->get();
  117. int ec = p.edges.size();
  118. Polygon::Edge *edges = p.edges.ptrw();
  119. for (int i = 0; i < ec; i++) {
  120. int next = (i + 1) % ec;
  121. EdgeKey ek(edges[i].point, edges[next].point);
  122. Map<EdgeKey, Connection>::Element *C = connections.find(ek);
  123. ERR_CONTINUE(!C);
  124. if (edges[i].P) {
  125. C->get().pending.erase(edges[i].P);
  126. edges[i].P = NULL;
  127. } else if (C->get().B) {
  128. //disconnect
  129. C->get().B->edges.write[C->get().B_edge].C = NULL;
  130. C->get().B->edges.write[C->get().B_edge].C_edge = -1;
  131. C->get().A->edges.write[C->get().A_edge].C = NULL;
  132. C->get().A->edges.write[C->get().A_edge].C_edge = -1;
  133. if (C->get().A == &E->get()) {
  134. C->get().A = C->get().B;
  135. C->get().A_edge = C->get().B_edge;
  136. }
  137. C->get().B = NULL;
  138. C->get().B_edge = -1;
  139. if (C->get().pending.size()) {
  140. //reconnect if something is pending
  141. ConnectionPending cp = C->get().pending.front()->get();
  142. C->get().pending.pop_front();
  143. C->get().B = cp.polygon;
  144. C->get().B_edge = cp.edge;
  145. C->get().A->edges.write[C->get().A_edge].C = cp.polygon;
  146. C->get().A->edges.write[C->get().A_edge].C_edge = cp.edge;
  147. cp.polygon->edges.write[cp.edge].C = C->get().A;
  148. cp.polygon->edges.write[cp.edge].C_edge = C->get().A_edge;
  149. cp.polygon->edges.write[cp.edge].P = NULL;
  150. }
  151. } else {
  152. connections.erase(C);
  153. //erase
  154. }
  155. }
  156. }
  157. nm.polygons.clear();
  158. nm.linked = false;
  159. }
  160. int Navigation2D::navpoly_add(const Ref<NavigationPolygon> &p_mesh, const Transform2D &p_xform, Object *p_owner) {
  161. int id = last_id++;
  162. NavMesh nm;
  163. nm.linked = false;
  164. nm.navpoly = p_mesh;
  165. nm.xform = p_xform;
  166. nm.owner = p_owner;
  167. navpoly_map[id] = nm;
  168. _navpoly_link(id);
  169. return id;
  170. }
  171. void Navigation2D::navpoly_set_transform(int p_id, const Transform2D &p_xform) {
  172. ERR_FAIL_COND(!navpoly_map.has(p_id));
  173. NavMesh &nm = navpoly_map[p_id];
  174. if (nm.xform == p_xform)
  175. return; //bleh
  176. _navpoly_unlink(p_id);
  177. nm.xform = p_xform;
  178. _navpoly_link(p_id);
  179. }
  180. void Navigation2D::navpoly_remove(int p_id) {
  181. ERR_FAIL_COND(!navpoly_map.has(p_id));
  182. _navpoly_unlink(p_id);
  183. navpoly_map.erase(p_id);
  184. }
  185. Vector<Vector2> Navigation2D::get_simple_path(const Vector2 &p_start, const Vector2 &p_end, bool p_optimize) {
  186. Polygon *begin_poly = NULL;
  187. Polygon *end_poly = NULL;
  188. Vector2 begin_point;
  189. Vector2 end_point;
  190. float begin_d = 1e20;
  191. float end_d = 1e20;
  192. //look for point inside triangle
  193. for (Map<int, NavMesh>::Element *E = navpoly_map.front(); E; E = E->next()) {
  194. if (!E->get().linked)
  195. continue;
  196. for (List<Polygon>::Element *F = E->get().polygons.front(); F; F = F->next()) {
  197. Polygon &p = F->get();
  198. if (begin_d || end_d) {
  199. for (int i = 2; i < p.edges.size(); i++) {
  200. if (begin_d > 0) {
  201. if (Geometry::is_point_in_triangle(p_start, _get_vertex(p.edges[0].point), _get_vertex(p.edges[i - 1].point), _get_vertex(p.edges[i].point))) {
  202. begin_poly = &p;
  203. begin_point = p_start;
  204. begin_d = 0;
  205. if (end_d == 0)
  206. break;
  207. }
  208. }
  209. if (end_d > 0) {
  210. if (Geometry::is_point_in_triangle(p_end, _get_vertex(p.edges[0].point), _get_vertex(p.edges[i - 1].point), _get_vertex(p.edges[i].point))) {
  211. end_poly = &p;
  212. end_point = p_end;
  213. end_d = 0;
  214. if (begin_d == 0)
  215. break;
  216. }
  217. }
  218. }
  219. }
  220. p.prev_edge = -1;
  221. }
  222. }
  223. //start or end not inside triangle.. look for closest segment :|
  224. if (begin_d || end_d) {
  225. for (Map<int, NavMesh>::Element *E = navpoly_map.front(); E; E = E->next()) {
  226. if (!E->get().linked)
  227. continue;
  228. for (List<Polygon>::Element *F = E->get().polygons.front(); F; F = F->next()) {
  229. Polygon &p = F->get();
  230. int es = p.edges.size();
  231. for (int i = 0; i < es; i++) {
  232. Vector2 edge[2] = {
  233. _get_vertex(p.edges[i].point),
  234. _get_vertex(p.edges[(i + 1) % es].point)
  235. };
  236. if (begin_d > 0) {
  237. Vector2 spoint = Geometry::get_closest_point_to_segment_2d(p_start, edge);
  238. float d = spoint.distance_to(p_start);
  239. if (d < begin_d) {
  240. begin_poly = &p;
  241. begin_point = spoint;
  242. begin_d = d;
  243. }
  244. }
  245. if (end_d > 0) {
  246. Vector2 spoint = Geometry::get_closest_point_to_segment_2d(p_end, edge);
  247. float d = spoint.distance_to(p_end);
  248. if (d < end_d) {
  249. end_poly = &p;
  250. end_point = spoint;
  251. end_d = d;
  252. }
  253. }
  254. }
  255. }
  256. }
  257. }
  258. if (!begin_poly || !end_poly) {
  259. return Vector<Vector2>(); //no path
  260. }
  261. if (begin_poly == end_poly) {
  262. Vector<Vector2> path;
  263. path.resize(2);
  264. path.write[0] = begin_point;
  265. path.write[1] = end_point;
  266. return path;
  267. }
  268. bool found_route = false;
  269. List<Polygon *> open_list;
  270. begin_poly->entry = p_start;
  271. for (int i = 0; i < begin_poly->edges.size(); i++) {
  272. if (begin_poly->edges[i].C) {
  273. begin_poly->edges[i].C->prev_edge = begin_poly->edges[i].C_edge;
  274. #ifdef USE_ENTRY_POINT
  275. Vector2 edge[2] = {
  276. _get_vertex(begin_poly->edges[i].point),
  277. _get_vertex(begin_poly->edges[(i + 1) % begin_poly->edges.size()].point)
  278. };
  279. Vector2 entry = Geometry::get_closest_point_to_segment_2d(begin_poly->entry, edge);
  280. begin_poly->edges[i].C->distance = begin_poly->entry.distance_to(entry);
  281. begin_poly->edges[i].C->entry = entry;
  282. #else
  283. begin_poly->edges[i].C->distance = begin_poly->center.distance_to(begin_poly->edges[i].C->center);
  284. #endif
  285. open_list.push_back(begin_poly->edges[i].C);
  286. if (begin_poly->edges[i].C == end_poly) {
  287. found_route = true;
  288. }
  289. }
  290. }
  291. while (!found_route) {
  292. if (open_list.size() == 0) {
  293. break;
  294. }
  295. //check open list
  296. List<Polygon *>::Element *least_cost_poly = NULL;
  297. float least_cost = 1e30;
  298. //this could be faster (cache previous results)
  299. for (List<Polygon *>::Element *E = open_list.front(); E; E = E->next()) {
  300. Polygon *p = E->get();
  301. float cost = p->distance;
  302. #ifdef USE_ENTRY_POINT
  303. int es = p->edges.size();
  304. float shortest_distance = 1e30;
  305. for (int i = 0; i < es; i++) {
  306. Polygon::Edge &e = p->edges.write[i];
  307. if (!e.C)
  308. continue;
  309. Vector2 edge[2] = {
  310. _get_vertex(p->edges[i].point),
  311. _get_vertex(p->edges[(i + 1) % es].point)
  312. };
  313. Vector2 edge_point = Geometry::get_closest_point_to_segment_2d(p->entry, edge);
  314. float dist = p->entry.distance_to(edge_point);
  315. if (dist < shortest_distance)
  316. shortest_distance = dist;
  317. }
  318. cost += shortest_distance;
  319. #else
  320. cost += p->center.distance_to(end_point);
  321. #endif
  322. if (cost < least_cost) {
  323. least_cost_poly = E;
  324. least_cost = cost;
  325. }
  326. }
  327. Polygon *p = least_cost_poly->get();
  328. //open the neighbours for search
  329. int es = p->edges.size();
  330. for (int i = 0; i < es; i++) {
  331. Polygon::Edge &e = p->edges.write[i];
  332. if (!e.C)
  333. continue;
  334. #ifdef USE_ENTRY_POINT
  335. Vector2 edge[2] = {
  336. _get_vertex(p->edges[i].point),
  337. _get_vertex(p->edges[(i + 1) % es].point)
  338. };
  339. Vector2 edge_entry = Geometry::get_closest_point_to_segment_2d(p->entry, edge);
  340. float distance = p->entry.distance_to(edge_entry) + p->distance;
  341. #else
  342. float distance = p->center.distance_to(e.C->center) + p->distance;
  343. #endif
  344. if (e.C->prev_edge != -1) {
  345. //oh this was visited already, can we win the cost?
  346. if (e.C->distance > distance) {
  347. e.C->prev_edge = e.C_edge;
  348. e.C->distance = distance;
  349. #ifdef USE_ENTRY_POINT
  350. e.C->entry = edge_entry;
  351. #endif
  352. }
  353. } else {
  354. //add to open neighbours
  355. e.C->prev_edge = e.C_edge;
  356. e.C->distance = distance;
  357. #ifdef USE_ENTRY_POINT
  358. e.C->entry = edge_entry;
  359. #endif
  360. open_list.push_back(e.C);
  361. if (e.C == end_poly) {
  362. //oh my reached end! stop algorithm
  363. found_route = true;
  364. break;
  365. }
  366. }
  367. }
  368. if (found_route)
  369. break;
  370. open_list.erase(least_cost_poly);
  371. }
  372. if (found_route) {
  373. Vector<Vector2> path;
  374. if (p_optimize) {
  375. //string pulling
  376. Vector2 apex_point = end_point;
  377. Vector2 portal_left = apex_point;
  378. Vector2 portal_right = apex_point;
  379. Polygon *left_poly = end_poly;
  380. Polygon *right_poly = end_poly;
  381. Polygon *p = end_poly;
  382. while (p) {
  383. Vector2 left;
  384. Vector2 right;
  385. //#define CLOCK_TANGENT(m_a,m_b,m_c) ( ((m_a)-(m_c)).cross((m_a)-(m_b)) )
  386. #define CLOCK_TANGENT(m_a, m_b, m_c) ((((m_a).x - (m_c).x) * ((m_b).y - (m_c).y) - ((m_b).x - (m_c).x) * ((m_a).y - (m_c).y)))
  387. if (p == begin_poly) {
  388. left = begin_point;
  389. right = begin_point;
  390. } else {
  391. int prev = p->prev_edge;
  392. int prev_n = (p->prev_edge + 1) % p->edges.size();
  393. left = _get_vertex(p->edges[prev].point);
  394. right = _get_vertex(p->edges[prev_n].point);
  395. if (p->clockwise) {
  396. SWAP(left, right);
  397. }
  398. /*if (CLOCK_TANGENT(apex_point,left,(left+right)*0.5) < 0){
  399. SWAP(left,right);
  400. }*/
  401. }
  402. bool skip = false;
  403. /*
  404. print_line("-----\nAPEX: "+(apex_point-end_point));
  405. print_line("LEFT:");
  406. print_line("\tPortal: "+(portal_left-end_point));
  407. print_line("\tPoint: "+(left-end_point));
  408. print_line("\tLeft Tangent: "+rtos(CLOCK_TANGENT(apex_point,portal_left,left)));
  409. print_line("\tLeft Distance: "+rtos(portal_left.distance_squared_to(apex_point)));
  410. print_line("\tLeft Test: "+rtos(CLOCK_TANGENT(apex_point,left,portal_right)));
  411. print_line("RIGHT:");
  412. print_line("\tPortal: "+(portal_right-end_point));
  413. print_line("\tPoint: "+(right-end_point));
  414. print_line("\tRight Tangent: "+rtos(CLOCK_TANGENT(apex_point,portal_right,right)));
  415. print_line("\tRight Distance: "+rtos(portal_right.distance_squared_to(apex_point)));
  416. print_line("\tRight Test: "+rtos(CLOCK_TANGENT(apex_point,right,portal_left)));
  417. */
  418. if (CLOCK_TANGENT(apex_point, portal_left, left) >= 0) {
  419. //process
  420. if (portal_left.distance_squared_to(apex_point) < CMP_EPSILON || CLOCK_TANGENT(apex_point, left, portal_right) > 0) {
  421. left_poly = p;
  422. portal_left = left;
  423. } else {
  424. apex_point = portal_right;
  425. p = right_poly;
  426. left_poly = p;
  427. portal_left = apex_point;
  428. portal_right = apex_point;
  429. if (!path.size() || path[path.size() - 1].distance_to(apex_point) > CMP_EPSILON)
  430. path.push_back(apex_point);
  431. skip = true;
  432. }
  433. }
  434. if (!skip && CLOCK_TANGENT(apex_point, portal_right, right) <= 0) {
  435. //process
  436. if (portal_right.distance_squared_to(apex_point) < CMP_EPSILON || CLOCK_TANGENT(apex_point, right, portal_left) < 0) {
  437. right_poly = p;
  438. portal_right = right;
  439. } else {
  440. apex_point = portal_left;
  441. p = left_poly;
  442. right_poly = p;
  443. portal_right = apex_point;
  444. portal_left = apex_point;
  445. if (!path.size() || path[path.size() - 1].distance_to(apex_point) > CMP_EPSILON)
  446. path.push_back(apex_point);
  447. }
  448. }
  449. if (p != begin_poly)
  450. p = p->edges[p->prev_edge].C;
  451. else
  452. p = NULL;
  453. }
  454. } else {
  455. //midpoints
  456. Polygon *p = end_poly;
  457. while (true) {
  458. int prev = p->prev_edge;
  459. int prev_n = (p->prev_edge + 1) % p->edges.size();
  460. Vector2 point = (_get_vertex(p->edges[prev].point) + _get_vertex(p->edges[prev_n].point)) * 0.5;
  461. path.push_back(point);
  462. p = p->edges[prev].C;
  463. if (p == begin_poly)
  464. break;
  465. }
  466. }
  467. if (!path.size() || path[path.size() - 1].distance_squared_to(begin_point) > CMP_EPSILON) {
  468. path.push_back(begin_point); // Add the begin point
  469. } else {
  470. path.write[path.size() - 1] = begin_point; // Replace first midpoint by the exact begin point
  471. }
  472. path.invert();
  473. if (path.size() <= 1 || path[path.size() - 1].distance_squared_to(end_point) > CMP_EPSILON) {
  474. path.push_back(end_point); // Add the end point
  475. } else {
  476. path.write[path.size() - 1] = end_point; // Replace last midpoint by the exact end point
  477. }
  478. return path;
  479. }
  480. return Vector<Vector2>();
  481. }
  482. Vector2 Navigation2D::get_closest_point(const Vector2 &p_point) {
  483. Vector2 closest_point = Vector2();
  484. float closest_point_d = 1e20;
  485. for (Map<int, NavMesh>::Element *E = navpoly_map.front(); E; E = E->next()) {
  486. if (!E->get().linked)
  487. continue;
  488. for (List<Polygon>::Element *F = E->get().polygons.front(); F; F = F->next()) {
  489. Polygon &p = F->get();
  490. for (int i = 2; i < p.edges.size(); i++) {
  491. if (Geometry::is_point_in_triangle(p_point, _get_vertex(p.edges[0].point), _get_vertex(p.edges[i - 1].point), _get_vertex(p.edges[i].point))) {
  492. return p_point; //inside triangle, nothing else to discuss
  493. }
  494. }
  495. }
  496. }
  497. for (Map<int, NavMesh>::Element *E = navpoly_map.front(); E; E = E->next()) {
  498. if (!E->get().linked)
  499. continue;
  500. for (List<Polygon>::Element *F = E->get().polygons.front(); F; F = F->next()) {
  501. Polygon &p = F->get();
  502. int es = p.edges.size();
  503. for (int i = 0; i < es; i++) {
  504. Vector2 edge[2] = {
  505. _get_vertex(p.edges[i].point),
  506. _get_vertex(p.edges[(i + 1) % es].point)
  507. };
  508. Vector2 spoint = Geometry::get_closest_point_to_segment_2d(p_point, edge);
  509. float d = spoint.distance_squared_to(p_point);
  510. if (d < closest_point_d) {
  511. closest_point = spoint;
  512. closest_point_d = d;
  513. }
  514. }
  515. }
  516. }
  517. return closest_point;
  518. }
  519. Object *Navigation2D::get_closest_point_owner(const Vector2 &p_point) {
  520. Object *owner = NULL;
  521. Vector2 closest_point = Vector2();
  522. float closest_point_d = 1e20;
  523. for (Map<int, NavMesh>::Element *E = navpoly_map.front(); E; E = E->next()) {
  524. if (!E->get().linked)
  525. continue;
  526. for (List<Polygon>::Element *F = E->get().polygons.front(); F; F = F->next()) {
  527. Polygon &p = F->get();
  528. for (int i = 2; i < p.edges.size(); i++) {
  529. if (Geometry::is_point_in_triangle(p_point, _get_vertex(p.edges[0].point), _get_vertex(p.edges[i - 1].point), _get_vertex(p.edges[i].point))) {
  530. return E->get().owner;
  531. }
  532. }
  533. }
  534. }
  535. for (Map<int, NavMesh>::Element *E = navpoly_map.front(); E; E = E->next()) {
  536. if (!E->get().linked)
  537. continue;
  538. for (List<Polygon>::Element *F = E->get().polygons.front(); F; F = F->next()) {
  539. Polygon &p = F->get();
  540. int es = p.edges.size();
  541. for (int i = 0; i < es; i++) {
  542. Vector2 edge[2] = {
  543. _get_vertex(p.edges[i].point),
  544. _get_vertex(p.edges[(i + 1) % es].point)
  545. };
  546. Vector2 spoint = Geometry::get_closest_point_to_segment_2d(p_point, edge);
  547. float d = spoint.distance_squared_to(p_point);
  548. if (d < closest_point_d) {
  549. closest_point = spoint;
  550. closest_point_d = d;
  551. owner = E->get().owner;
  552. }
  553. }
  554. }
  555. }
  556. return owner;
  557. }
  558. void Navigation2D::_bind_methods() {
  559. ClassDB::bind_method(D_METHOD("navpoly_add", "mesh", "xform", "owner"), &Navigation2D::navpoly_add, DEFVAL(Variant()));
  560. ClassDB::bind_method(D_METHOD("navpoly_set_transform", "id", "xform"), &Navigation2D::navpoly_set_transform);
  561. ClassDB::bind_method(D_METHOD("navpoly_remove", "id"), &Navigation2D::navpoly_remove);
  562. ClassDB::bind_method(D_METHOD("get_simple_path", "start", "end", "optimize"), &Navigation2D::get_simple_path, DEFVAL(true));
  563. ClassDB::bind_method(D_METHOD("get_closest_point", "to_point"), &Navigation2D::get_closest_point);
  564. ClassDB::bind_method(D_METHOD("get_closest_point_owner", "to_point"), &Navigation2D::get_closest_point_owner);
  565. }
  566. Navigation2D::Navigation2D() {
  567. ERR_FAIL_COND(sizeof(Point) != 8);
  568. cell_size = 1; // one pixel
  569. last_id = 1;
  570. }