navigation2d.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. /*************************************************************************/
  2. /* navigation2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. DVector<Vector2> vertices = nm.navpoly->get_vertices();
  37. int len = vertices.size();
  38. if (len == 0)
  39. return;
  40. DVector<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[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[j].P = C->get().pending.push_back(pending);
  97. continue;
  98. //print_line(String()+_get_vertex(ek.a)+" -> "+_get_vertex(ek.b));
  99. }
  100. C->get().B = &p;
  101. C->get().B_edge = j;
  102. C->get().A->edges[C->get().A_edge].C = &p;
  103. C->get().A->edges[C->get().A_edge].C_edge = j;
  104. p.edges[j].C = C->get().A;
  105. p.edges[j].C_edge = C->get().A_edge;
  106. //connection successful.
  107. }
  108. }
  109. }
  110. nm.linked = true;
  111. }
  112. void Navigation2D::_navpoly_unlink(int p_id) {
  113. ERR_FAIL_COND(!navpoly_map.has(p_id));
  114. NavMesh &nm = navpoly_map[p_id];
  115. ERR_FAIL_COND(!nm.linked);
  116. //print_line("UNLINK");
  117. for (List<Polygon>::Element *E = nm.polygons.front(); E; E = E->next()) {
  118. Polygon &p = E->get();
  119. int ec = p.edges.size();
  120. Polygon::Edge *edges = p.edges.ptr();
  121. for (int i = 0; i < ec; i++) {
  122. int next = (i + 1) % ec;
  123. EdgeKey ek(edges[i].point, edges[next].point);
  124. Map<EdgeKey, Connection>::Element *C = connections.find(ek);
  125. ERR_CONTINUE(!C);
  126. if (edges[i].P) {
  127. C->get().pending.erase(edges[i].P);
  128. edges[i].P = NULL;
  129. } else if (C->get().B) {
  130. //disconnect
  131. C->get().B->edges[C->get().B_edge].C = NULL;
  132. C->get().B->edges[C->get().B_edge].C_edge = -1;
  133. C->get().A->edges[C->get().A_edge].C = NULL;
  134. C->get().A->edges[C->get().A_edge].C_edge = -1;
  135. if (C->get().A == &E->get()) {
  136. C->get().A = C->get().B;
  137. C->get().A_edge = C->get().B_edge;
  138. }
  139. C->get().B = NULL;
  140. C->get().B_edge = -1;
  141. if (C->get().pending.size()) {
  142. //reconnect if something is pending
  143. ConnectionPending cp = C->get().pending.front()->get();
  144. C->get().pending.pop_front();
  145. C->get().B = cp.polygon;
  146. C->get().B_edge = cp.edge;
  147. C->get().A->edges[C->get().A_edge].C = cp.polygon;
  148. C->get().A->edges[C->get().A_edge].C_edge = cp.edge;
  149. cp.polygon->edges[cp.edge].C = C->get().A;
  150. cp.polygon->edges[cp.edge].C_edge = C->get().A_edge;
  151. cp.polygon->edges[cp.edge].P = NULL;
  152. }
  153. } else {
  154. connections.erase(C);
  155. //erase
  156. }
  157. }
  158. }
  159. nm.polygons.clear();
  160. nm.linked = false;
  161. }
  162. int Navigation2D::navpoly_create(const Ref<NavigationPolygon> &p_mesh, const Matrix32 &p_xform, Object *p_owner) {
  163. int id = last_id++;
  164. NavMesh nm;
  165. nm.linked = false;
  166. nm.navpoly = p_mesh;
  167. nm.xform = p_xform;
  168. nm.owner = p_owner;
  169. navpoly_map[id] = nm;
  170. _navpoly_link(id);
  171. return id;
  172. }
  173. void Navigation2D::navpoly_set_transform(int p_id, const Matrix32 &p_xform) {
  174. ERR_FAIL_COND(!navpoly_map.has(p_id));
  175. NavMesh &nm = navpoly_map[p_id];
  176. if (nm.xform == p_xform)
  177. return; //bleh
  178. _navpoly_unlink(p_id);
  179. nm.xform = p_xform;
  180. _navpoly_link(p_id);
  181. }
  182. void Navigation2D::navpoly_remove(int p_id) {
  183. ERR_FAIL_COND(!navpoly_map.has(p_id));
  184. _navpoly_unlink(p_id);
  185. navpoly_map.erase(p_id);
  186. }
  187. #if 0
  188. void Navigation2D::_clip_path(Vector<Vector2>& path, Polygon *from_poly, const Vector2& p_to_point, Polygon* p_to_poly) {
  189. Vector2 from = path[path.size()-1];
  190. if (from.distance_to(p_to_point)<CMP_EPSILON)
  191. return;
  192. Plane cut_plane;
  193. cut_plane.normal = (from-p_to_point).cross(up);
  194. if (cut_plane.normal==Vector2())
  195. return;
  196. cut_plane.normal.normalize();
  197. cut_plane.d = cut_plane.normal.dot(from);
  198. while(from_poly!=p_to_poly) {
  199. int pe = from_poly->prev_edge;
  200. Vector2 a = _get_vertex(from_poly->edges[pe].point);
  201. Vector2 b = _get_vertex(from_poly->edges[(pe+1)%from_poly->edges.size()].point);
  202. from_poly=from_poly->edges[pe].C;
  203. ERR_FAIL_COND(!from_poly);
  204. if (a.distance_to(b)>CMP_EPSILON) {
  205. Vector2 inters;
  206. if (cut_plane.intersects_segment(a,b,&inters)) {
  207. if (inters.distance_to(p_to_point)>CMP_EPSILON && inters.distance_to(path[path.size()-1])>CMP_EPSILON) {
  208. path.push_back(inters);
  209. }
  210. }
  211. }
  212. }
  213. }
  214. #endif
  215. Vector<Vector2> Navigation2D::get_simple_path(const Vector2 &p_start, const Vector2 &p_end, bool p_optimize) {
  216. Polygon *begin_poly = NULL;
  217. Polygon *end_poly = NULL;
  218. Vector2 begin_point;
  219. Vector2 end_point;
  220. float begin_d = 1e20;
  221. float end_d = 1e20;
  222. //look for point inside triangle
  223. for (Map<int, NavMesh>::Element *E = navpoly_map.front(); E; E = E->next()) {
  224. if (!E->get().linked)
  225. continue;
  226. for (List<Polygon>::Element *F = E->get().polygons.front(); F; F = F->next()) {
  227. Polygon &p = F->get();
  228. if (begin_d || end_d) {
  229. for (int i = 2; i < p.edges.size(); i++) {
  230. if (begin_d > 0) {
  231. 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))) {
  232. begin_poly = &p;
  233. begin_point = p_start;
  234. begin_d = 0;
  235. if (end_d == 0)
  236. break;
  237. }
  238. }
  239. if (end_d > 0) {
  240. 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))) {
  241. end_poly = &p;
  242. end_point = p_end;
  243. end_d = 0;
  244. if (begin_d == 0)
  245. break;
  246. }
  247. }
  248. }
  249. }
  250. p.prev_edge = -1;
  251. }
  252. }
  253. //start or end not inside triangle.. look for closest segment :|
  254. if (begin_d || end_d) {
  255. for (Map<int, NavMesh>::Element *E = navpoly_map.front(); E; E = E->next()) {
  256. if (!E->get().linked)
  257. continue;
  258. for (List<Polygon>::Element *F = E->get().polygons.front(); F; F = F->next()) {
  259. Polygon &p = F->get();
  260. int es = p.edges.size();
  261. for (int i = 0; i < es; i++) {
  262. Vector2 edge[2] = {
  263. _get_vertex(p.edges[i].point),
  264. _get_vertex(p.edges[(i + 1) % es].point)
  265. };
  266. if (begin_d > 0) {
  267. Vector2 spoint = Geometry::get_closest_point_to_segment_2d(p_start, edge);
  268. float d = spoint.distance_to(p_start);
  269. if (d < begin_d) {
  270. begin_poly = &p;
  271. begin_point = spoint;
  272. begin_d = d;
  273. }
  274. }
  275. if (end_d > 0) {
  276. Vector2 spoint = Geometry::get_closest_point_to_segment_2d(p_end, edge);
  277. float d = spoint.distance_to(p_end);
  278. if (d < end_d) {
  279. end_poly = &p;
  280. end_point = spoint;
  281. end_d = d;
  282. }
  283. }
  284. }
  285. }
  286. }
  287. }
  288. if (!begin_poly || !end_poly) {
  289. return Vector<Vector2>(); //no path
  290. }
  291. if (begin_poly == end_poly) {
  292. Vector<Vector2> path;
  293. path.resize(2);
  294. path[0] = begin_point;
  295. path[1] = end_point;
  296. //print_line("Direct Path");
  297. return path;
  298. }
  299. bool found_route = false;
  300. List<Polygon *> open_list;
  301. begin_poly->entry = p_start;
  302. for (int i = 0; i < begin_poly->edges.size(); i++) {
  303. if (begin_poly->edges[i].C) {
  304. begin_poly->edges[i].C->prev_edge = begin_poly->edges[i].C_edge;
  305. #ifdef USE_ENTRY_POINT
  306. Vector2 edge[2] = {
  307. _get_vertex(begin_poly->edges[i].point),
  308. _get_vertex(begin_poly->edges[(i + 1) % begin_poly->edges.size()].point)
  309. };
  310. Vector2 entry = Geometry::get_closest_point_to_segment_2d(begin_poly->entry, edge);
  311. begin_poly->edges[i].C->distance = begin_poly->entry.distance_to(entry);
  312. begin_poly->edges[i].C->entry = entry;
  313. #else
  314. begin_poly->edges[i].C->distance = begin_poly->center.distance_to(begin_poly->edges[i].C->center);
  315. #endif
  316. open_list.push_back(begin_poly->edges[i].C);
  317. if (begin_poly->edges[i].C == end_poly) {
  318. found_route = true;
  319. }
  320. }
  321. }
  322. while (!found_route) {
  323. if (open_list.size() == 0) {
  324. // print_line("NOU OPEN LIST");
  325. break;
  326. }
  327. //check open list
  328. List<Polygon *>::Element *least_cost_poly = NULL;
  329. float least_cost = 1e30;
  330. //this could be faster (cache previous results)
  331. for (List<Polygon *>::Element *E = open_list.front(); E; E = E->next()) {
  332. Polygon *p = E->get();
  333. float cost = p->distance;
  334. cost += p->center.distance_to(end_point);
  335. if (cost < least_cost) {
  336. least_cost_poly = E;
  337. least_cost = cost;
  338. }
  339. }
  340. Polygon *p = least_cost_poly->get();
  341. //open the neighbours for search
  342. int es = p->edges.size();
  343. for (int i = 0; i < es; i++) {
  344. Polygon::Edge &e = p->edges[i];
  345. if (!e.C)
  346. continue;
  347. #ifdef USE_ENTRY_POINT
  348. Vector2 edge[2] = {
  349. _get_vertex(p->edges[i].point),
  350. _get_vertex(p->edges[(i + 1) % es].point)
  351. };
  352. Vector2 edge_entry = Geometry::get_closest_point_to_segment_2d(p->entry, edge);
  353. float distance = p->entry.distance_to(edge_entry) + p->distance;
  354. #else
  355. float distance = p->center.distance_to(e.C->center) + p->distance;
  356. #endif
  357. if (e.C->prev_edge != -1) {
  358. //oh this was visited already, can we win the cost?
  359. if (e.C->distance > distance) {
  360. e.C->prev_edge = e.C_edge;
  361. e.C->distance = distance;
  362. #ifdef USE_ENTRY_POINT
  363. e.C->entry = edge_entry;
  364. #endif
  365. }
  366. } else {
  367. //add to open neighbours
  368. e.C->prev_edge = e.C_edge;
  369. e.C->distance = distance;
  370. #ifdef USE_ENTRY_POINT
  371. e.C->entry = edge_entry;
  372. #endif
  373. open_list.push_back(e.C);
  374. if (e.C == end_poly) {
  375. //oh my reached end! stop algorithm
  376. found_route = true;
  377. break;
  378. }
  379. }
  380. }
  381. if (found_route)
  382. break;
  383. open_list.erase(least_cost_poly);
  384. }
  385. #if 0
  386. debug path
  387. {
  388. Polygon *p=end_poly;
  389. int idx=0;
  390. while(true) {
  391. int prev = p->prev_edge;
  392. int prev_n = (p->prev_edge+1)%p->edges.size();
  393. Vector2 point = (_get_vertex(p->edges[prev].point) + _get_vertex(p->edges[prev_n].point))*0.5;
  394. String points;
  395. for(int i=0;i<p->edges.size();i++) {
  396. if (i>0)
  397. points+=", ";
  398. points+=_get_vertex(p->edges[i].point);
  399. }
  400. //print_line("poly "+itos(idx++)+" - "+points);
  401. p = p->edges[prev].C;
  402. if (p==begin_poly)
  403. break;
  404. }
  405. }
  406. #endif
  407. if (found_route) {
  408. Vector<Vector2> path;
  409. if (p_optimize) {
  410. //string pulling
  411. Vector2 apex_point = end_point;
  412. Vector2 portal_left = apex_point;
  413. Vector2 portal_right = apex_point;
  414. Polygon *left_poly = end_poly;
  415. Polygon *right_poly = end_poly;
  416. Polygon *p = end_poly;
  417. path.push_back(end_point);
  418. while (p) {
  419. Vector2 left;
  420. Vector2 right;
  421. //#define CLOCK_TANGENT(m_a,m_b,m_c) ( ((m_a)-(m_c)).cross((m_a)-(m_b)) )
  422. #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)))
  423. if (p == begin_poly) {
  424. left = begin_point;
  425. right = begin_point;
  426. } else {
  427. int prev = p->prev_edge;
  428. int prev_n = (p->prev_edge + 1) % p->edges.size();
  429. left = _get_vertex(p->edges[prev].point);
  430. right = _get_vertex(p->edges[prev_n].point);
  431. if (p->clockwise) {
  432. SWAP(left, right);
  433. }
  434. /*if (CLOCK_TANGENT(apex_point,left,(left+right)*0.5) < 0){
  435. SWAP(left,right);
  436. }*/
  437. }
  438. bool skip = false;
  439. /*
  440. print_line("-----\nAPEX: "+(apex_point-end_point));
  441. print_line("LEFT:");
  442. print_line("\tPortal: "+(portal_left-end_point));
  443. print_line("\tPoint: "+(left-end_point));
  444. print_line("\tLeft Tangent: "+rtos(CLOCK_TANGENT(apex_point,portal_left,left)));
  445. print_line("\tLeft Distance: "+rtos(portal_left.distance_squared_to(apex_point)));
  446. print_line("\tLeft Test: "+rtos(CLOCK_TANGENT(apex_point,left,portal_right)));
  447. print_line("RIGHT:");
  448. print_line("\tPortal: "+(portal_right-end_point));
  449. print_line("\tPoint: "+(right-end_point));
  450. print_line("\tRight Tangent: "+rtos(CLOCK_TANGENT(apex_point,portal_right,right)));
  451. print_line("\tRight Distance: "+rtos(portal_right.distance_squared_to(apex_point)));
  452. print_line("\tRight Test: "+rtos(CLOCK_TANGENT(apex_point,right,portal_left)));
  453. */
  454. if (CLOCK_TANGENT(apex_point, portal_left, left) >= 0) {
  455. //process
  456. if (portal_left.distance_squared_to(apex_point) < CMP_EPSILON || CLOCK_TANGENT(apex_point, left, portal_right) > 0) {
  457. left_poly = p;
  458. portal_left = left;
  459. //print_line("***ADVANCE LEFT");
  460. } else {
  461. apex_point = portal_right;
  462. p = right_poly;
  463. left_poly = p;
  464. portal_left = apex_point;
  465. portal_right = apex_point;
  466. if (path[path.size() - 1].distance_to(apex_point) > CMP_EPSILON)
  467. path.push_back(apex_point);
  468. skip = true;
  469. //print_line("addpoint left");
  470. //print_line("***CLIP LEFT");
  471. }
  472. }
  473. if (!skip && CLOCK_TANGENT(apex_point, portal_right, right) <= 0) {
  474. //process
  475. if (portal_right.distance_squared_to(apex_point) < CMP_EPSILON || CLOCK_TANGENT(apex_point, right, portal_left) < 0) {
  476. right_poly = p;
  477. portal_right = right;
  478. //print_line("***ADVANCE RIGHT");
  479. } else {
  480. apex_point = portal_left;
  481. p = left_poly;
  482. right_poly = p;
  483. portal_right = apex_point;
  484. portal_left = apex_point;
  485. if (path[path.size() - 1].distance_to(apex_point) > CMP_EPSILON)
  486. path.push_back(apex_point);
  487. //print_line("addpoint right");
  488. //print_line("***CLIP RIGHT");
  489. }
  490. }
  491. if (p != begin_poly)
  492. p = p->edges[p->prev_edge].C;
  493. else
  494. p = NULL;
  495. }
  496. if (path[path.size() - 1].distance_to(begin_point) > CMP_EPSILON)
  497. path.push_back(begin_point);
  498. path.invert();
  499. } else {
  500. //midpoints
  501. Polygon *p = end_poly;
  502. path.push_back(end_point);
  503. while (true) {
  504. int prev = p->prev_edge;
  505. int prev_n = (p->prev_edge + 1) % p->edges.size();
  506. Vector2 point = (_get_vertex(p->edges[prev].point) + _get_vertex(p->edges[prev_n].point)) * 0.5;
  507. path.push_back(point);
  508. p = p->edges[prev].C;
  509. if (p == begin_poly)
  510. break;
  511. }
  512. if (path[path.size() - 1].distance_to(begin_point) > CMP_EPSILON)
  513. path.push_back(begin_point);
  514. path.invert();
  515. }
  516. return path;
  517. }
  518. return Vector<Vector2>();
  519. }
  520. Vector2 Navigation2D::get_closest_point(const Vector2 &p_point) {
  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 p_point; //inside triangle, nothing else to discuss
  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. }
  552. }
  553. }
  554. }
  555. return closest_point;
  556. }
  557. Object *Navigation2D::get_closest_point_owner(const Vector2 &p_point) {
  558. Object *owner = NULL;
  559. Vector2 closest_point = Vector2();
  560. float closest_point_d = 1e20;
  561. for (Map<int, NavMesh>::Element *E = navpoly_map.front(); E; E = E->next()) {
  562. if (!E->get().linked)
  563. continue;
  564. for (List<Polygon>::Element *F = E->get().polygons.front(); F; F = F->next()) {
  565. Polygon &p = F->get();
  566. for (int i = 2; i < p.edges.size(); i++) {
  567. 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))) {
  568. E->get().owner;
  569. }
  570. }
  571. }
  572. }
  573. for (Map<int, NavMesh>::Element *E = navpoly_map.front(); E; E = E->next()) {
  574. if (!E->get().linked)
  575. continue;
  576. for (List<Polygon>::Element *F = E->get().polygons.front(); F; F = F->next()) {
  577. Polygon &p = F->get();
  578. int es = p.edges.size();
  579. for (int i = 0; i < es; i++) {
  580. Vector2 edge[2] = {
  581. _get_vertex(p.edges[i].point),
  582. _get_vertex(p.edges[(i + 1) % es].point)
  583. };
  584. Vector2 spoint = Geometry::get_closest_point_to_segment_2d(p_point, edge);
  585. float d = spoint.distance_squared_to(p_point);
  586. if (d < closest_point_d) {
  587. closest_point = spoint;
  588. closest_point_d = d;
  589. owner = E->get().owner;
  590. }
  591. }
  592. }
  593. }
  594. return owner;
  595. }
  596. void Navigation2D::_bind_methods() {
  597. ObjectTypeDB::bind_method(_MD("navpoly_create", "mesh:NavigationPolygon", "xform", "owner"), &Navigation2D::navpoly_create, DEFVAL(Variant()));
  598. ObjectTypeDB::bind_method(_MD("navpoly_set_transform", "id", "xform"), &Navigation2D::navpoly_set_transform);
  599. ObjectTypeDB::bind_method(_MD("navpoly_remove", "id"), &Navigation2D::navpoly_remove);
  600. ObjectTypeDB::bind_method(_MD("get_simple_path", "start", "end", "optimize"), &Navigation2D::get_simple_path, DEFVAL(true));
  601. ObjectTypeDB::bind_method(_MD("get_closest_point", "to_point"), &Navigation2D::get_closest_point);
  602. ObjectTypeDB::bind_method(_MD("get_closest_point_owner", "to_point"), &Navigation2D::get_closest_point_owner);
  603. }
  604. Navigation2D::Navigation2D() {
  605. ERR_FAIL_COND(sizeof(Point) != 8);
  606. cell_size = 1; // one pixel
  607. last_id = 1;
  608. }