2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* navigation2d.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2016-06-18 14:46:12 +02:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2015-02-14 16:09:52 +01:00
|
|
|
#include "navigation2d.h"
|
|
|
|
|
2015-02-19 04:27:02 +01:00
|
|
|
#define USE_ENTRY_POINT
|
|
|
|
|
2015-02-14 16:09:52 +01:00
|
|
|
void Navigation2D::_navpoly_link(int p_id) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!navpoly_map.has(p_id));
|
2017-03-19 00:36:26 +01:00
|
|
|
NavMesh &nm = navpoly_map[p_id];
|
2015-02-14 16:09:52 +01:00
|
|
|
ERR_FAIL_COND(nm.linked);
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
DVector<Vector2> vertices = nm.navpoly->get_vertices();
|
2015-02-14 16:09:52 +01:00
|
|
|
int len = vertices.size();
|
2017-03-19 00:36:26 +01:00
|
|
|
if (len == 0)
|
2015-02-14 16:09:52 +01:00
|
|
|
return;
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
DVector<Vector2>::Read r = vertices.read();
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < nm.navpoly->get_polygon_count(); i++) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
|
|
|
//build
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
List<Polygon>::Element *P = nm.polygons.push_back(Polygon());
|
|
|
|
Polygon &p = P->get();
|
|
|
|
p.owner = &nm;
|
2015-02-14 16:09:52 +01:00
|
|
|
|
|
|
|
Vector<int> poly = nm.navpoly->get_polygon(i);
|
2017-03-19 00:36:26 +01:00
|
|
|
int plen = poly.size();
|
|
|
|
const int *indices = poly.ptr();
|
|
|
|
bool valid = true;
|
2015-02-14 16:09:52 +01:00
|
|
|
p.edges.resize(plen);
|
|
|
|
|
|
|
|
Vector2 center;
|
2017-03-19 00:36:26 +01:00
|
|
|
float sum = 0;
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int j = 0; j < plen; j++) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
|
|
|
int idx = indices[j];
|
2017-03-19 00:36:26 +01:00
|
|
|
if (idx < 0 || idx >= len) {
|
|
|
|
valid = false;
|
2015-02-14 16:09:52 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Polygon::Edge e;
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector2 ep = nm.xform.xform(r[idx]);
|
|
|
|
center += ep;
|
|
|
|
e.point = _get_point(ep);
|
|
|
|
p.edges[j] = e;
|
|
|
|
|
|
|
|
int idxn = indices[(j + 1) % plen];
|
|
|
|
if (idxn < 0 || idxn >= len) {
|
|
|
|
valid = false;
|
2015-05-02 21:35:43 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2 epn = nm.xform.xform(r[idxn]);
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
sum += (epn.x - ep.x) * (epn.y + ep.y);
|
2015-02-14 16:09:52 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
p.clockwise = sum > 0;
|
2015-05-02 21:35:43 +02:00
|
|
|
|
2015-02-14 16:09:52 +01:00
|
|
|
if (!valid) {
|
|
|
|
nm.polygons.pop_back();
|
|
|
|
ERR_CONTINUE(!valid);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
p.center = center / plen;
|
2015-02-14 16:09:52 +01:00
|
|
|
|
|
|
|
//connect
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int j = 0; j < plen; j++) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int next = (j + 1) % plen;
|
|
|
|
EdgeKey ek(p.edges[j].point, p.edges[next].point);
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Map<EdgeKey, Connection>::Element *C = connections.find(ek);
|
2015-02-14 16:09:52 +01:00
|
|
|
if (!C) {
|
|
|
|
|
|
|
|
Connection c;
|
2017-03-19 00:36:26 +01:00
|
|
|
c.A = &p;
|
|
|
|
c.A_edge = j;
|
|
|
|
c.B = NULL;
|
|
|
|
c.B_edge = -1;
|
|
|
|
connections[ek] = c;
|
2015-02-14 16:09:52 +01:00
|
|
|
} else {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (C->get().B != NULL) {
|
2015-06-02 00:42:34 +02:00
|
|
|
ConnectionPending pending;
|
2017-03-19 00:36:26 +01:00
|
|
|
pending.polygon = &p;
|
|
|
|
pending.edge = j;
|
|
|
|
p.edges[j].P = C->get().pending.push_back(pending);
|
2015-06-02 00:42:34 +02:00
|
|
|
continue;
|
|
|
|
//print_line(String()+_get_vertex(ek.a)+" -> "+_get_vertex(ek.b));
|
2015-02-14 16:09:52 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
C->get().B = &p;
|
|
|
|
C->get().B_edge = j;
|
|
|
|
C->get().A->edges[C->get().A_edge].C = &p;
|
|
|
|
C->get().A->edges[C->get().A_edge].C_edge = j;
|
|
|
|
p.edges[j].C = C->get().A;
|
|
|
|
p.edges[j].C_edge = C->get().A_edge;
|
2015-02-14 16:09:52 +01:00
|
|
|
//connection successful.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
nm.linked = true;
|
2015-02-14 16:09:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Navigation2D::_navpoly_unlink(int p_id) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!navpoly_map.has(p_id));
|
2017-03-19 00:36:26 +01:00
|
|
|
NavMesh &nm = navpoly_map[p_id];
|
2015-02-14 16:09:52 +01:00
|
|
|
ERR_FAIL_COND(!nm.linked);
|
|
|
|
|
2015-09-26 19:50:42 +02:00
|
|
|
//print_line("UNLINK");
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (List<Polygon>::Element *E = nm.polygons.front(); E; E = E->next()) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Polygon &p = E->get();
|
2015-02-14 16:09:52 +01:00
|
|
|
|
|
|
|
int ec = p.edges.size();
|
2017-03-19 00:36:26 +01:00
|
|
|
Polygon::Edge *edges = p.edges.ptr();
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < ec; i++) {
|
|
|
|
int next = (i + 1) % ec;
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
EdgeKey ek(edges[i].point, edges[next].point);
|
|
|
|
Map<EdgeKey, Connection>::Element *C = connections.find(ek);
|
2015-02-14 16:09:52 +01:00
|
|
|
ERR_CONTINUE(!C);
|
2015-06-02 00:42:34 +02:00
|
|
|
|
|
|
|
if (edges[i].P) {
|
|
|
|
C->get().pending.erase(edges[i].P);
|
2017-03-19 00:36:26 +01:00
|
|
|
edges[i].P = NULL;
|
2015-06-02 00:42:34 +02:00
|
|
|
|
|
|
|
} else if (C->get().B) {
|
2015-02-14 16:09:52 +01:00
|
|
|
//disconnect
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
C->get().B->edges[C->get().B_edge].C = NULL;
|
|
|
|
C->get().B->edges[C->get().B_edge].C_edge = -1;
|
|
|
|
C->get().A->edges[C->get().A_edge].C = NULL;
|
|
|
|
C->get().A->edges[C->get().A_edge].C_edge = -1;
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (C->get().A == &E->get()) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
C->get().A = C->get().B;
|
|
|
|
C->get().A_edge = C->get().B_edge;
|
2015-02-14 16:09:52 +01:00
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
C->get().B = NULL;
|
|
|
|
C->get().B_edge = -1;
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2015-06-02 00:42:34 +02:00
|
|
|
if (C->get().pending.size()) {
|
|
|
|
//reconnect if something is pending
|
|
|
|
ConnectionPending cp = C->get().pending.front()->get();
|
|
|
|
C->get().pending.pop_front();
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
C->get().B = cp.polygon;
|
|
|
|
C->get().B_edge = cp.edge;
|
|
|
|
C->get().A->edges[C->get().A_edge].C = cp.polygon;
|
|
|
|
C->get().A->edges[C->get().A_edge].C_edge = cp.edge;
|
|
|
|
cp.polygon->edges[cp.edge].C = C->get().A;
|
|
|
|
cp.polygon->edges[cp.edge].C_edge = C->get().A_edge;
|
|
|
|
cp.polygon->edges[cp.edge].P = NULL;
|
2015-06-02 00:42:34 +02:00
|
|
|
}
|
|
|
|
|
2015-02-14 16:09:52 +01:00
|
|
|
} else {
|
|
|
|
connections.erase(C);
|
|
|
|
//erase
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nm.polygons.clear();
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
nm.linked = false;
|
2015-02-14 16:09:52 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int Navigation2D::navpoly_create(const Ref<NavigationPolygon> &p_mesh, const Matrix32 &p_xform, Object *p_owner) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
|
|
|
int id = last_id++;
|
|
|
|
NavMesh nm;
|
2017-03-19 00:36:26 +01:00
|
|
|
nm.linked = false;
|
|
|
|
nm.navpoly = p_mesh;
|
|
|
|
nm.xform = p_xform;
|
|
|
|
nm.owner = p_owner;
|
|
|
|
navpoly_map[id] = nm;
|
2015-02-14 16:09:52 +01:00
|
|
|
|
|
|
|
_navpoly_link(id);
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void Navigation2D::navpoly_set_transform(int p_id, const Matrix32 &p_xform) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!navpoly_map.has(p_id));
|
2017-03-19 00:36:26 +01:00
|
|
|
NavMesh &nm = navpoly_map[p_id];
|
|
|
|
if (nm.xform == p_xform)
|
2015-02-14 16:09:52 +01:00
|
|
|
return; //bleh
|
|
|
|
_navpoly_unlink(p_id);
|
2017-03-19 00:36:26 +01:00
|
|
|
nm.xform = p_xform;
|
2015-02-14 16:09:52 +01:00
|
|
|
_navpoly_link(p_id);
|
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
void Navigation2D::navpoly_remove(int p_id) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!navpoly_map.has(p_id));
|
|
|
|
_navpoly_unlink(p_id);
|
|
|
|
navpoly_map.erase(p_id);
|
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
void Navigation2D::_clip_path(Vector<Vector2>& path, Polygon *from_poly, const Vector2& p_to_point, Polygon* p_to_poly) {
|
|
|
|
|
|
|
|
Vector2 from = path[path.size()-1];
|
|
|
|
|
|
|
|
if (from.distance_to(p_to_point)<CMP_EPSILON)
|
|
|
|
return;
|
|
|
|
Plane cut_plane;
|
|
|
|
cut_plane.normal = (from-p_to_point).cross(up);
|
|
|
|
if (cut_plane.normal==Vector2())
|
|
|
|
return;
|
|
|
|
cut_plane.normal.normalize();
|
|
|
|
cut_plane.d = cut_plane.normal.dot(from);
|
|
|
|
|
|
|
|
|
|
|
|
while(from_poly!=p_to_poly) {
|
|
|
|
|
|
|
|
int pe = from_poly->prev_edge;
|
|
|
|
Vector2 a = _get_vertex(from_poly->edges[pe].point);
|
|
|
|
Vector2 b = _get_vertex(from_poly->edges[(pe+1)%from_poly->edges.size()].point);
|
|
|
|
|
|
|
|
from_poly=from_poly->edges[pe].C;
|
|
|
|
ERR_FAIL_COND(!from_poly);
|
|
|
|
|
|
|
|
if (a.distance_to(b)>CMP_EPSILON) {
|
|
|
|
|
|
|
|
Vector2 inters;
|
|
|
|
if (cut_plane.intersects_segment(a,b,&inters)) {
|
|
|
|
if (inters.distance_to(p_to_point)>CMP_EPSILON && inters.distance_to(path[path.size()-1])>CMP_EPSILON) {
|
|
|
|
path.push_back(inters);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector<Vector2> Navigation2D::get_simple_path(const Vector2 &p_start, const Vector2 &p_end, bool p_optimize) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Polygon *begin_poly = NULL;
|
|
|
|
Polygon *end_poly = NULL;
|
2015-02-14 16:09:52 +01:00
|
|
|
Vector2 begin_point;
|
|
|
|
Vector2 end_point;
|
2017-03-19 00:36:26 +01:00
|
|
|
float begin_d = 1e20;
|
|
|
|
float end_d = 1e20;
|
2015-02-14 16:09:52 +01:00
|
|
|
|
|
|
|
//look for point inside triangle
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (Map<int, NavMesh>::Element *E = navpoly_map.front(); E; E = E->next()) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
|
|
|
if (!E->get().linked)
|
|
|
|
continue;
|
2017-03-19 00:36:26 +01:00
|
|
|
for (List<Polygon>::Element *F = E->get().polygons.front(); F; F = F->next()) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Polygon &p = F->get();
|
2015-02-14 16:09:52 +01:00
|
|
|
if (begin_d || end_d) {
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 2; i < p.edges.size(); i++) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (begin_d > 0) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
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))) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
begin_poly = &p;
|
|
|
|
begin_point = p_start;
|
|
|
|
begin_d = 0;
|
|
|
|
if (end_d == 0)
|
2015-02-14 16:09:52 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (end_d > 0) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
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))) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
end_poly = &p;
|
|
|
|
end_point = p_end;
|
|
|
|
end_d = 0;
|
|
|
|
if (begin_d == 0)
|
2015-02-14 16:09:52 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
p.prev_edge = -1;
|
2015-02-14 16:09:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//start or end not inside triangle.. look for closest segment :|
|
|
|
|
if (begin_d || end_d) {
|
2017-03-19 00:36:26 +01:00
|
|
|
for (Map<int, NavMesh>::Element *E = navpoly_map.front(); E; E = E->next()) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
|
|
|
if (!E->get().linked)
|
|
|
|
continue;
|
2017-03-19 00:36:26 +01:00
|
|
|
for (List<Polygon>::Element *F = E->get().polygons.front(); F; F = F->next()) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Polygon &p = F->get();
|
2015-02-14 16:09:52 +01:00
|
|
|
int es = p.edges.size();
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < es; i++) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector2 edge[2] = {
|
2015-02-14 16:09:52 +01:00
|
|
|
_get_vertex(p.edges[i].point),
|
2017-03-19 00:36:26 +01:00
|
|
|
_get_vertex(p.edges[(i + 1) % es].point)
|
2015-02-14 16:09:52 +01:00
|
|
|
};
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (begin_d > 0) {
|
|
|
|
Vector2 spoint = Geometry::get_closest_point_to_segment_2d(p_start, edge);
|
2015-02-14 16:09:52 +01:00
|
|
|
float d = spoint.distance_to(p_start);
|
2017-03-19 00:36:26 +01:00
|
|
|
if (d < begin_d) {
|
|
|
|
begin_poly = &p;
|
|
|
|
begin_point = spoint;
|
|
|
|
begin_d = d;
|
2015-02-14 16:09:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (end_d > 0) {
|
|
|
|
Vector2 spoint = Geometry::get_closest_point_to_segment_2d(p_end, edge);
|
2015-02-14 16:09:52 +01:00
|
|
|
float d = spoint.distance_to(p_end);
|
2017-03-19 00:36:26 +01:00
|
|
|
if (d < end_d) {
|
|
|
|
end_poly = &p;
|
|
|
|
end_point = spoint;
|
|
|
|
end_d = d;
|
2015-02-14 16:09:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!begin_poly || !end_poly) {
|
|
|
|
|
|
|
|
return Vector<Vector2>(); //no path
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (begin_poly == end_poly) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
|
|
|
Vector<Vector2> path;
|
|
|
|
path.resize(2);
|
2017-03-19 00:36:26 +01:00
|
|
|
path[0] = begin_point;
|
|
|
|
path[1] = end_point;
|
2015-02-14 16:09:52 +01:00
|
|
|
//print_line("Direct Path");
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
bool found_route = false;
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
List<Polygon *> open_list;
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
begin_poly->entry = p_start;
|
2015-02-19 04:27:02 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < begin_poly->edges.size(); i++) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
|
|
|
if (begin_poly->edges[i].C) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
begin_poly->edges[i].C->prev_edge = begin_poly->edges[i].C_edge;
|
2015-02-19 04:27:02 +01:00
|
|
|
#ifdef USE_ENTRY_POINT
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector2 edge[2] = {
|
2015-02-19 04:27:02 +01:00
|
|
|
_get_vertex(begin_poly->edges[i].point),
|
2017-03-19 00:36:26 +01:00
|
|
|
_get_vertex(begin_poly->edges[(i + 1) % begin_poly->edges.size()].point)
|
2015-02-19 04:27:02 +01:00
|
|
|
};
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector2 entry = Geometry::get_closest_point_to_segment_2d(begin_poly->entry, edge);
|
2015-02-19 04:27:02 +01:00
|
|
|
begin_poly->edges[i].C->distance = begin_poly->entry.distance_to(entry);
|
2017-03-19 00:36:26 +01:00
|
|
|
begin_poly->edges[i].C->entry = entry;
|
2015-02-19 04:27:02 +01:00
|
|
|
#else
|
2017-03-19 00:36:26 +01:00
|
|
|
begin_poly->edges[i].C->distance = begin_poly->center.distance_to(begin_poly->edges[i].C->center);
|
2015-02-19 04:27:02 +01:00
|
|
|
#endif
|
2015-02-14 16:09:52 +01:00
|
|
|
open_list.push_back(begin_poly->edges[i].C);
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (begin_poly->edges[i].C == end_poly) {
|
|
|
|
found_route = true;
|
2015-02-14 16:09:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
while (!found_route) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (open_list.size() == 0) {
|
|
|
|
// print_line("NOU OPEN LIST");
|
2015-02-14 16:09:52 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
//check open list
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
List<Polygon *>::Element *least_cost_poly = NULL;
|
|
|
|
float least_cost = 1e30;
|
2015-02-14 16:09:52 +01:00
|
|
|
|
|
|
|
//this could be faster (cache previous results)
|
2017-03-19 00:36:26 +01:00
|
|
|
for (List<Polygon *>::Element *E = open_list.front(); E; E = E->next()) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Polygon *p = E->get();
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
float cost = p->distance;
|
|
|
|
cost += p->center.distance_to(end_point);
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (cost < least_cost) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
least_cost_poly = E;
|
|
|
|
least_cost = cost;
|
2015-02-14 16:09:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Polygon *p = least_cost_poly->get();
|
2015-02-14 16:09:52 +01:00
|
|
|
//open the neighbours for search
|
2015-02-19 04:27:02 +01:00
|
|
|
int es = p->edges.size();
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < es; i++) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Polygon::Edge &e = p->edges[i];
|
2015-02-14 16:09:52 +01:00
|
|
|
|
|
|
|
if (!e.C)
|
|
|
|
continue;
|
|
|
|
|
2015-02-19 04:27:02 +01:00
|
|
|
#ifdef USE_ENTRY_POINT
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector2 edge[2] = {
|
2015-02-19 04:27:02 +01:00
|
|
|
_get_vertex(p->edges[i].point),
|
2017-03-19 00:36:26 +01:00
|
|
|
_get_vertex(p->edges[(i + 1) % es].point)
|
2015-02-19 04:27:02 +01:00
|
|
|
};
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector2 edge_entry = Geometry::get_closest_point_to_segment_2d(p->entry, edge);
|
2015-02-19 04:27:02 +01:00
|
|
|
float distance = p->entry.distance_to(edge_entry) + p->distance;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2015-02-14 16:09:52 +01:00
|
|
|
float distance = p->center.distance_to(e.C->center) + p->distance;
|
|
|
|
|
2015-02-19 04:27:02 +01:00
|
|
|
#endif
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (e.C->prev_edge != -1) {
|
2015-02-14 16:09:52 +01:00
|
|
|
//oh this was visited already, can we win the cost?
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (e.C->distance > distance) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
e.C->prev_edge = e.C_edge;
|
|
|
|
e.C->distance = distance;
|
2015-02-19 04:27:02 +01:00
|
|
|
#ifdef USE_ENTRY_POINT
|
2017-03-19 00:36:26 +01:00
|
|
|
e.C->entry = edge_entry;
|
2015-02-19 04:27:02 +01:00
|
|
|
#endif
|
2015-02-14 16:09:52 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//add to open neighbours
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
e.C->prev_edge = e.C_edge;
|
|
|
|
e.C->distance = distance;
|
2015-02-19 04:27:02 +01:00
|
|
|
#ifdef USE_ENTRY_POINT
|
2017-03-19 00:36:26 +01:00
|
|
|
e.C->entry = edge_entry;
|
2015-02-19 04:27:02 +01:00
|
|
|
#endif
|
|
|
|
|
2015-02-14 16:09:52 +01:00
|
|
|
open_list.push_back(e.C);
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (e.C == end_poly) {
|
2015-02-14 16:09:52 +01:00
|
|
|
//oh my reached end! stop algorithm
|
2017-03-19 00:36:26 +01:00
|
|
|
found_route = true;
|
2015-02-14 16:09:52 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found_route)
|
|
|
|
break;
|
|
|
|
|
|
|
|
open_list.erase(least_cost_poly);
|
|
|
|
}
|
2016-01-22 23:49:05 +01:00
|
|
|
#if 0
|
|
|
|
debug path
|
2015-12-31 04:31:00 +01:00
|
|
|
{
|
|
|
|
Polygon *p=end_poly;
|
|
|
|
int idx=0;
|
|
|
|
|
|
|
|
while(true) {
|
|
|
|
int prev = p->prev_edge;
|
|
|
|
int prev_n = (p->prev_edge+1)%p->edges.size();
|
|
|
|
Vector2 point = (_get_vertex(p->edges[prev].point) + _get_vertex(p->edges[prev_n].point))*0.5;
|
|
|
|
String points;
|
|
|
|
for(int i=0;i<p->edges.size();i++) {
|
|
|
|
if (i>0)
|
|
|
|
points+=", ";
|
|
|
|
points+=_get_vertex(p->edges[i].point);
|
|
|
|
}
|
2016-01-09 23:50:37 +01:00
|
|
|
//print_line("poly "+itos(idx++)+" - "+points);
|
2015-12-31 04:31:00 +01:00
|
|
|
p = p->edges[prev].C;
|
|
|
|
if (p==begin_poly)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-01-22 23:49:05 +01:00
|
|
|
#endif
|
2015-02-14 16:09:52 +01:00
|
|
|
if (found_route) {
|
|
|
|
|
|
|
|
Vector<Vector2> path;
|
|
|
|
|
|
|
|
if (p_optimize) {
|
|
|
|
//string pulling
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector2 apex_point = end_point;
|
|
|
|
Vector2 portal_left = apex_point;
|
|
|
|
Vector2 portal_right = apex_point;
|
|
|
|
Polygon *left_poly = end_poly;
|
|
|
|
Polygon *right_poly = end_poly;
|
|
|
|
Polygon *p = end_poly;
|
2015-02-14 16:09:52 +01:00
|
|
|
path.push_back(end_point);
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
while (p) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
|
|
|
Vector2 left;
|
|
|
|
Vector2 right;
|
|
|
|
|
|
|
|
//#define CLOCK_TANGENT(m_a,m_b,m_c) ( ((m_a)-(m_c)).cross((m_a)-(m_b)) )
|
2017-03-19 00:36:26 +01:00
|
|
|
#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)))
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p == begin_poly) {
|
|
|
|
left = begin_point;
|
|
|
|
right = begin_point;
|
2015-02-14 16:09:52 +01:00
|
|
|
} else {
|
|
|
|
int prev = p->prev_edge;
|
2017-03-19 00:36:26 +01:00
|
|
|
int prev_n = (p->prev_edge + 1) % p->edges.size();
|
2015-02-14 16:09:52 +01:00
|
|
|
left = _get_vertex(p->edges[prev].point);
|
|
|
|
right = _get_vertex(p->edges[prev_n].point);
|
|
|
|
|
2015-05-02 21:35:43 +02:00
|
|
|
if (p->clockwise) {
|
2017-03-19 00:36:26 +01:00
|
|
|
SWAP(left, right);
|
2015-02-14 16:09:52 +01:00
|
|
|
}
|
2015-05-02 21:35:43 +02:00
|
|
|
/*if (CLOCK_TANGENT(apex_point,left,(left+right)*0.5) < 0){
|
|
|
|
SWAP(left,right);
|
|
|
|
}*/
|
2015-02-14 16:09:52 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
bool skip = false;
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2016-01-09 23:50:37 +01:00
|
|
|
/*
|
2015-12-31 04:31:00 +01:00
|
|
|
print_line("-----\nAPEX: "+(apex_point-end_point));
|
2015-05-02 21:35:43 +02:00
|
|
|
print_line("LEFT:");
|
|
|
|
print_line("\tPortal: "+(portal_left-end_point));
|
|
|
|
print_line("\tPoint: "+(left-end_point));
|
2015-12-31 04:31:00 +01:00
|
|
|
print_line("\tLeft Tangent: "+rtos(CLOCK_TANGENT(apex_point,portal_left,left)));
|
|
|
|
print_line("\tLeft Distance: "+rtos(portal_left.distance_squared_to(apex_point)));
|
|
|
|
print_line("\tLeft Test: "+rtos(CLOCK_TANGENT(apex_point,left,portal_right)));
|
2015-05-02 21:35:43 +02:00
|
|
|
print_line("RIGHT:");
|
|
|
|
print_line("\tPortal: "+(portal_right-end_point));
|
|
|
|
print_line("\tPoint: "+(right-end_point));
|
2015-12-31 04:31:00 +01:00
|
|
|
print_line("\tRight Tangent: "+rtos(CLOCK_TANGENT(apex_point,portal_right,right)));
|
|
|
|
print_line("\tRight Distance: "+rtos(portal_right.distance_squared_to(apex_point)));
|
|
|
|
print_line("\tRight Test: "+rtos(CLOCK_TANGENT(apex_point,right,portal_left)));
|
2016-01-09 23:50:37 +01:00
|
|
|
*/
|
2015-12-31 04:31:00 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (CLOCK_TANGENT(apex_point, portal_left, left) >= 0) {
|
2015-02-14 16:09:52 +01:00
|
|
|
//process
|
2017-03-19 00:36:26 +01:00
|
|
|
if (portal_left.distance_squared_to(apex_point) < CMP_EPSILON || CLOCK_TANGENT(apex_point, left, portal_right) > 0) {
|
|
|
|
left_poly = p;
|
|
|
|
portal_left = left;
|
2016-01-09 23:50:37 +01:00
|
|
|
//print_line("***ADVANCE LEFT");
|
2015-02-14 16:09:52 +01:00
|
|
|
} else {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
apex_point = portal_right;
|
|
|
|
p = right_poly;
|
|
|
|
left_poly = p;
|
|
|
|
portal_left = apex_point;
|
|
|
|
portal_right = apex_point;
|
|
|
|
if (path[path.size() - 1].distance_to(apex_point) > CMP_EPSILON)
|
2015-04-30 15:06:18 +02:00
|
|
|
path.push_back(apex_point);
|
2017-03-19 00:36:26 +01:00
|
|
|
skip = true;
|
2015-05-02 21:35:43 +02:00
|
|
|
//print_line("addpoint left");
|
2016-01-09 23:50:37 +01:00
|
|
|
//print_line("***CLIP LEFT");
|
2015-02-14 16:09:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (!skip && CLOCK_TANGENT(apex_point, portal_right, right) <= 0) {
|
2015-02-14 16:09:52 +01:00
|
|
|
//process
|
2017-03-19 00:36:26 +01:00
|
|
|
if (portal_right.distance_squared_to(apex_point) < CMP_EPSILON || CLOCK_TANGENT(apex_point, right, portal_left) < 0) {
|
|
|
|
right_poly = p;
|
|
|
|
portal_right = right;
|
2016-01-09 23:50:37 +01:00
|
|
|
//print_line("***ADVANCE RIGHT");
|
2015-02-14 16:09:52 +01:00
|
|
|
} else {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
apex_point = portal_left;
|
|
|
|
p = left_poly;
|
|
|
|
right_poly = p;
|
|
|
|
portal_right = apex_point;
|
|
|
|
portal_left = apex_point;
|
|
|
|
if (path[path.size() - 1].distance_to(apex_point) > CMP_EPSILON)
|
2015-04-30 15:06:18 +02:00
|
|
|
path.push_back(apex_point);
|
2015-05-02 21:35:43 +02:00
|
|
|
//print_line("addpoint right");
|
2016-01-09 23:50:37 +01:00
|
|
|
//print_line("***CLIP RIGHT");
|
2015-02-14 16:09:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p != begin_poly)
|
|
|
|
p = p->edges[p->prev_edge].C;
|
2015-02-14 16:09:52 +01:00
|
|
|
else
|
2017-03-19 00:36:26 +01:00
|
|
|
p = NULL;
|
2015-02-14 16:09:52 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (path[path.size() - 1].distance_to(begin_point) > CMP_EPSILON)
|
2015-02-14 16:09:52 +01:00
|
|
|
path.push_back(begin_point);
|
|
|
|
|
|
|
|
path.invert();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
//midpoints
|
2017-03-19 00:36:26 +01:00
|
|
|
Polygon *p = end_poly;
|
2015-02-14 16:09:52 +01:00
|
|
|
|
|
|
|
path.push_back(end_point);
|
2017-03-19 00:36:26 +01:00
|
|
|
while (true) {
|
2015-02-14 16:09:52 +01:00
|
|
|
int prev = p->prev_edge;
|
2017-03-19 00:36:26 +01:00
|
|
|
int prev_n = (p->prev_edge + 1) % p->edges.size();
|
|
|
|
Vector2 point = (_get_vertex(p->edges[prev].point) + _get_vertex(p->edges[prev_n].point)) * 0.5;
|
2015-02-14 16:09:52 +01:00
|
|
|
path.push_back(point);
|
|
|
|
p = p->edges[prev].C;
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p == begin_poly)
|
2015-02-14 16:09:52 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
path.push_back(begin_point);
|
|
|
|
|
2017-01-14 18:03:38 +01:00
|
|
|
path.invert();
|
2015-02-14 16:09:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Vector<Vector2>();
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector2 Navigation2D::get_closest_point(const Vector2 &p_point) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector2 closest_point = Vector2();
|
|
|
|
float closest_point_d = 1e20;
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (Map<int, NavMesh>::Element *E = navpoly_map.front(); E; E = E->next()) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
|
|
|
if (!E->get().linked)
|
|
|
|
continue;
|
2017-03-19 00:36:26 +01:00
|
|
|
for (List<Polygon>::Element *F = E->get().polygons.front(); F; F = F->next()) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Polygon &p = F->get();
|
|
|
|
for (int i = 2; i < p.edges.size(); i++) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
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))) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
|
|
|
return p_point; //inside triangle, nothing else to discuss
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (Map<int, NavMesh>::Element *E = navpoly_map.front(); E; E = E->next()) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
|
|
|
if (!E->get().linked)
|
|
|
|
continue;
|
2017-03-19 00:36:26 +01:00
|
|
|
for (List<Polygon>::Element *F = E->get().polygons.front(); F; F = F->next()) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Polygon &p = F->get();
|
2015-02-14 16:09:52 +01:00
|
|
|
int es = p.edges.size();
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < es; i++) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector2 edge[2] = {
|
2015-02-14 16:09:52 +01:00
|
|
|
_get_vertex(p.edges[i].point),
|
2017-03-19 00:36:26 +01:00
|
|
|
_get_vertex(p.edges[(i + 1) % es].point)
|
2015-02-14 16:09:52 +01:00
|
|
|
};
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector2 spoint = Geometry::get_closest_point_to_segment_2d(p_point, edge);
|
2015-02-14 16:09:52 +01:00
|
|
|
float d = spoint.distance_squared_to(p_point);
|
2017-03-19 00:36:26 +01:00
|
|
|
if (d < closest_point_d) {
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
closest_point = spoint;
|
|
|
|
closest_point_d = d;
|
2015-02-14 16:09:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return closest_point;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Object *Navigation2D::get_closest_point_owner(const Vector2 &p_point) {
|
2015-04-21 21:47:49 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Object *owner = NULL;
|
|
|
|
Vector2 closest_point = Vector2();
|
|
|
|
float closest_point_d = 1e20;
|
2015-04-21 21:47:49 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (Map<int, NavMesh>::Element *E = navpoly_map.front(); E; E = E->next()) {
|
2015-04-21 21:47:49 +02:00
|
|
|
|
|
|
|
if (!E->get().linked)
|
|
|
|
continue;
|
2017-03-19 00:36:26 +01:00
|
|
|
for (List<Polygon>::Element *F = E->get().polygons.front(); F; F = F->next()) {
|
2015-04-21 21:47:49 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Polygon &p = F->get();
|
|
|
|
for (int i = 2; i < p.edges.size(); i++) {
|
2015-04-21 21:47:49 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
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))) {
|
2015-04-21 21:47:49 +02:00
|
|
|
|
|
|
|
E->get().owner;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (Map<int, NavMesh>::Element *E = navpoly_map.front(); E; E = E->next()) {
|
2015-04-21 21:47:49 +02:00
|
|
|
|
|
|
|
if (!E->get().linked)
|
|
|
|
continue;
|
2017-03-19 00:36:26 +01:00
|
|
|
for (List<Polygon>::Element *F = E->get().polygons.front(); F; F = F->next()) {
|
2015-04-21 21:47:49 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Polygon &p = F->get();
|
2015-04-21 21:47:49 +02:00
|
|
|
int es = p.edges.size();
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < es; i++) {
|
2015-04-21 21:47:49 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector2 edge[2] = {
|
2015-04-21 21:47:49 +02:00
|
|
|
_get_vertex(p.edges[i].point),
|
2017-03-19 00:36:26 +01:00
|
|
|
_get_vertex(p.edges[(i + 1) % es].point)
|
2015-04-21 21:47:49 +02:00
|
|
|
};
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector2 spoint = Geometry::get_closest_point_to_segment_2d(p_point, edge);
|
2015-04-21 21:47:49 +02:00
|
|
|
float d = spoint.distance_squared_to(p_point);
|
2017-03-19 00:36:26 +01:00
|
|
|
if (d < closest_point_d) {
|
2015-04-21 21:47:49 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
closest_point = spoint;
|
|
|
|
closest_point_d = d;
|
|
|
|
owner = E->get().owner;
|
2015-04-21 21:47:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return owner;
|
|
|
|
}
|
|
|
|
|
2015-02-14 16:09:52 +01:00
|
|
|
void Navigation2D::_bind_methods() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("navpoly_create", "mesh:NavigationPolygon", "xform", "owner"), &Navigation2D::navpoly_create, DEFVAL(Variant()));
|
|
|
|
ObjectTypeDB::bind_method(_MD("navpoly_set_transform", "id", "xform"), &Navigation2D::navpoly_set_transform);
|
|
|
|
ObjectTypeDB::bind_method(_MD("navpoly_remove", "id"), &Navigation2D::navpoly_remove);
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("get_simple_path", "start", "end", "optimize"), &Navigation2D::get_simple_path, DEFVAL(true));
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_closest_point", "to_point"), &Navigation2D::get_closest_point);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_closest_point_owner", "to_point"), &Navigation2D::get_closest_point_owner);
|
2015-02-14 16:09:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Navigation2D::Navigation2D() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_COND(sizeof(Point) != 8);
|
|
|
|
cell_size = 1; // one pixel
|
|
|
|
last_id = 1;
|
2015-02-14 16:09:52 +01:00
|
|
|
}
|