2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* navigation.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:11:45 +00:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-07 22:45:00 +00:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2016-06-18 12:46:12 +00: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. */
|
|
|
|
/*************************************************************************/
|
2014-08-02 01:10:38 +00:00
|
|
|
#include "navigation.h"
|
|
|
|
|
|
|
|
void Navigation::_navmesh_link(int p_id) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!navmesh_map.has(p_id));
|
2017-03-18 23:36:26 +00:00
|
|
|
NavMesh &nm = navmesh_map[p_id];
|
2014-08-02 01:10:38 +00:00
|
|
|
ERR_FAIL_COND(nm.linked);
|
2017-10-03 17:51:26 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
DVector<Vector3> vertices = nm.navmesh->get_vertices();
|
2014-08-02 01:10:38 +00:00
|
|
|
int len = vertices.size();
|
2017-03-18 23:36:26 +00:00
|
|
|
if (len == 0)
|
2014-08-02 01:10:38 +00:00
|
|
|
return;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
DVector<Vector3>::Read r = vertices.read();
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < nm.navmesh->get_polygon_count(); i++) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
|
|
|
//build
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
List<Polygon>::Element *P = nm.polygons.push_back(Polygon());
|
|
|
|
Polygon &p = P->get();
|
|
|
|
p.owner = &nm;
|
2014-08-02 01:10:38 +00:00
|
|
|
|
|
|
|
Vector<int> poly = nm.navmesh->get_polygon(i);
|
2017-03-18 23:36:26 +00:00
|
|
|
int plen = poly.size();
|
|
|
|
const int *indices = poly.ptr();
|
|
|
|
bool valid = true;
|
2014-08-02 01:10:38 +00:00
|
|
|
p.edges.resize(plen);
|
|
|
|
|
|
|
|
Vector3 center;
|
2017-03-18 23:36:26 +00:00
|
|
|
float sum = 0;
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int j = 0; j < plen; j++) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
|
|
|
int idx = indices[j];
|
2017-03-18 23:36:26 +00:00
|
|
|
if (idx < 0 || idx >= len) {
|
|
|
|
valid = false;
|
2014-08-02 01:10:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Polygon::Edge e;
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector3 ep = nm.xform.xform(r[idx]);
|
|
|
|
center += ep;
|
|
|
|
e.point = _get_point(ep);
|
|
|
|
p.edges[j] = e;
|
2015-05-02 20:39:29 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (j >= 2) {
|
|
|
|
Vector3 epa = nm.xform.xform(r[indices[j - 2]]);
|
|
|
|
Vector3 epb = nm.xform.xform(r[indices[j - 1]]);
|
2015-05-02 20:39:29 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
sum += up.dot((epb - epa).cross(ep - epa));
|
2015-05-02 20:39:29 +00:00
|
|
|
}
|
2014-08-02 01:10:38 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
p.clockwise = sum > 0;
|
2015-05-02 20:39:29 +00:00
|
|
|
|
2014-08-02 01:10:38 +00:00
|
|
|
if (!valid) {
|
|
|
|
nm.polygons.pop_back();
|
|
|
|
ERR_CONTINUE(!valid);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-02-29 23:08:33 +00:00
|
|
|
p.center = center;
|
2017-03-18 23:36:26 +00:00
|
|
|
if (plen != 0) {
|
2016-02-29 23:08:33 +00:00
|
|
|
p.center /= plen;
|
|
|
|
}
|
|
|
|
|
2014-08-02 01:10:38 +00:00
|
|
|
//connect
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int j = 0; j < plen; j++) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int next = (j + 1) % plen;
|
|
|
|
EdgeKey ek(p.edges[j].point, p.edges[next].point);
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Map<EdgeKey, Connection>::Element *C = connections.find(ek);
|
2014-08-02 01:10:38 +00:00
|
|
|
if (!C) {
|
|
|
|
|
|
|
|
Connection c;
|
2017-03-18 23:36:26 +00:00
|
|
|
c.A = &p;
|
|
|
|
c.A_edge = j;
|
|
|
|
c.B = NULL;
|
|
|
|
c.B_edge = -1;
|
|
|
|
connections[ek] = c;
|
2014-08-02 01:10:38 +00:00
|
|
|
} else {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (C->get().B != NULL) {
|
2015-06-01 22:42:34 +00:00
|
|
|
ConnectionPending pending;
|
2017-03-18 23:36:26 +00:00
|
|
|
pending.polygon = &p;
|
|
|
|
pending.edge = j;
|
|
|
|
p.edges[j].P = C->get().pending.push_back(pending);
|
2015-06-01 22:42:34 +00:00
|
|
|
continue;
|
|
|
|
//print_line(String()+_get_vertex(ek.a)+" -> "+_get_vertex(ek.b));
|
2014-08-02 01:10:38 +00:00
|
|
|
}
|
2015-06-01 22:42:34 +00:00
|
|
|
//ERR_CONTINUE(C->get().B!=NULL); //wut
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00: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;
|
2014-08-02 01:10:38 +00:00
|
|
|
//connection successful.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
nm.linked = true;
|
2014-08-02 01:10:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Navigation::_navmesh_unlink(int p_id) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!navmesh_map.has(p_id));
|
2017-03-18 23:36:26 +00:00
|
|
|
NavMesh &nm = navmesh_map[p_id];
|
2014-08-02 01:10:38 +00:00
|
|
|
ERR_FAIL_COND(!nm.linked);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (List<Polygon>::Element *E = nm.polygons.front(); E; E = E->next()) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Polygon &p = E->get();
|
2014-08-02 01:10:38 +00:00
|
|
|
|
|
|
|
int ec = p.edges.size();
|
2017-03-18 23:36:26 +00:00
|
|
|
Polygon::Edge *edges = p.edges.ptr();
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < ec; i++) {
|
|
|
|
int next = (i + 1) % ec;
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
EdgeKey ek(edges[i].point, edges[next].point);
|
|
|
|
Map<EdgeKey, Connection>::Element *C = connections.find(ek);
|
2015-06-01 22:42:34 +00:00
|
|
|
|
2014-08-02 01:10:38 +00:00
|
|
|
ERR_CONTINUE(!C);
|
2015-06-01 22:42:34 +00:00
|
|
|
|
|
|
|
if (edges[i].P) {
|
|
|
|
C->get().pending.erase(edges[i].P);
|
2017-03-18 23:36:26 +00:00
|
|
|
edges[i].P = NULL;
|
2015-06-01 22:42:34 +00:00
|
|
|
} else if (C->get().B) {
|
2014-08-02 01:10:38 +00:00
|
|
|
//disconnect
|
|
|
|
|
2017-03-18 23:36:26 +00: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;
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (C->get().A == &E->get()) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
C->get().A = C->get().B;
|
|
|
|
C->get().A_edge = C->get().B_edge;
|
2014-08-02 01:10:38 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
C->get().B = NULL;
|
|
|
|
C->get().B_edge = -1;
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2015-06-01 22:42:34 +00: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-18 23:36:26 +00: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-01 22:42:34 +00:00
|
|
|
}
|
|
|
|
|
2014-08-02 01:10:38 +00:00
|
|
|
} else {
|
|
|
|
connections.erase(C);
|
|
|
|
//erase
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nm.polygons.clear();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
nm.linked = false;
|
2014-08-02 01:10:38 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int Navigation::navmesh_create(const Ref<NavigationMesh> &p_mesh, const Transform &p_xform, Object *p_owner) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
|
|
|
int id = last_id++;
|
|
|
|
NavMesh nm;
|
2017-03-18 23:36:26 +00:00
|
|
|
nm.linked = false;
|
|
|
|
nm.navmesh = p_mesh;
|
|
|
|
nm.xform = p_xform;
|
|
|
|
nm.owner = p_owner;
|
|
|
|
navmesh_map[id] = nm;
|
2014-08-02 01:10:38 +00:00
|
|
|
|
|
|
|
_navmesh_link(id);
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void Navigation::navmesh_set_transform(int p_id, const Transform &p_xform) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!navmesh_map.has(p_id));
|
2017-03-18 23:36:26 +00:00
|
|
|
NavMesh &nm = navmesh_map[p_id];
|
|
|
|
if (nm.xform == p_xform)
|
2014-08-02 01:10:38 +00:00
|
|
|
return; //bleh
|
|
|
|
_navmesh_unlink(p_id);
|
2017-03-18 23:36:26 +00:00
|
|
|
nm.xform = p_xform;
|
2014-08-02 01:10:38 +00:00
|
|
|
_navmesh_link(p_id);
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
void Navigation::navmesh_remove(int p_id) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!navmesh_map.has(p_id));
|
|
|
|
_navmesh_unlink(p_id);
|
|
|
|
navmesh_map.erase(p_id);
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void Navigation::_clip_path(Vector<Vector3> &path, Polygon *from_poly, const Vector3 &p_to_point, Polygon *p_to_poly) {
|
2014-11-17 10:46:11 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector3 from = path[path.size() - 1];
|
2014-11-17 10:46:11 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (from.distance_to(p_to_point) < CMP_EPSILON)
|
2014-11-17 10:46:11 +00:00
|
|
|
return;
|
|
|
|
Plane cut_plane;
|
2017-03-18 23:36:26 +00:00
|
|
|
cut_plane.normal = (from - p_to_point).cross(up);
|
|
|
|
if (cut_plane.normal == Vector3())
|
2014-11-17 10:46:11 +00:00
|
|
|
return;
|
|
|
|
cut_plane.normal.normalize();
|
|
|
|
cut_plane.d = cut_plane.normal.dot(from);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
while (from_poly != p_to_poly) {
|
2014-11-17 10:46:11 +00:00
|
|
|
|
|
|
|
int pe = from_poly->prev_edge;
|
|
|
|
Vector3 a = _get_vertex(from_poly->edges[pe].point);
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector3 b = _get_vertex(from_poly->edges[(pe + 1) % from_poly->edges.size()].point);
|
2014-11-17 10:46:11 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
from_poly = from_poly->edges[pe].C;
|
2014-11-17 10:46:11 +00:00
|
|
|
ERR_FAIL_COND(!from_poly);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (a.distance_to(b) > CMP_EPSILON) {
|
2014-11-17 10:46:11 +00:00
|
|
|
|
|
|
|
Vector3 inters;
|
2017-03-18 23:36:26 +00:00
|
|
|
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) {
|
2014-11-17 10:46:11 +00:00
|
|
|
path.push_back(inters);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector<Vector3> Navigation::get_simple_path(const Vector3 &p_start, const Vector3 &p_end, bool p_optimize) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Polygon *begin_poly = NULL;
|
|
|
|
Polygon *end_poly = NULL;
|
2014-08-02 01:10:38 +00:00
|
|
|
Vector3 begin_point;
|
|
|
|
Vector3 end_point;
|
2017-03-18 23:36:26 +00:00
|
|
|
float begin_d = 1e20;
|
|
|
|
float end_d = 1e20;
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (Map<int, NavMesh>::Element *E = navmesh_map.front(); E; E = E->next()) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
|
|
|
if (!E->get().linked)
|
|
|
|
continue;
|
2017-03-18 23:36:26 +00:00
|
|
|
for (List<Polygon>::Element *F = E->get().polygons.front(); F; F = F->next()) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Polygon &p = F->get();
|
|
|
|
for (int i = 2; i < p.edges.size(); i++) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Face3 f(_get_vertex(p.edges[0].point), _get_vertex(p.edges[i - 1].point), _get_vertex(p.edges[i].point));
|
2014-08-02 01:10:38 +00:00
|
|
|
Vector3 spoint = f.get_closest_point_to(p_start);
|
|
|
|
float dpoint = spoint.distance_to(p_start);
|
2017-03-18 23:36:26 +00:00
|
|
|
if (dpoint < begin_d) {
|
|
|
|
begin_d = dpoint;
|
|
|
|
begin_poly = &p;
|
|
|
|
begin_point = spoint;
|
2014-08-02 01:10:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
spoint = f.get_closest_point_to(p_end);
|
|
|
|
dpoint = spoint.distance_to(p_end);
|
2017-03-18 23:36:26 +00:00
|
|
|
if (dpoint < end_d) {
|
|
|
|
end_d = dpoint;
|
|
|
|
end_poly = &p;
|
|
|
|
end_point = spoint;
|
2014-08-02 01:10:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
p.prev_edge = -1;
|
2014-08-02 01:10:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!begin_poly || !end_poly) {
|
|
|
|
|
|
|
|
//print_line("No Path Path");
|
|
|
|
return Vector<Vector3>(); //no path
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (begin_poly == end_poly) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
|
|
|
Vector<Vector3> path;
|
|
|
|
path.resize(2);
|
2017-03-18 23:36:26 +00:00
|
|
|
path[0] = begin_point;
|
|
|
|
path[1] = end_point;
|
2014-08-02 01:10:38 +00:00
|
|
|
//print_line("Direct Path");
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool found_route = false;
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
List<Polygon *> open_list;
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < begin_poly->edges.size(); i++) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
|
|
|
if (begin_poly->edges[i].C) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
begin_poly->edges[i].C->prev_edge = begin_poly->edges[i].C_edge;
|
|
|
|
begin_poly->edges[i].C->distance = begin_poly->center.distance_to(begin_poly->edges[i].C->center);
|
2014-08-02 01:10:38 +00:00
|
|
|
open_list.push_back(begin_poly->edges[i].C);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (begin_poly->edges[i].C == end_poly) {
|
|
|
|
found_route = true;
|
2014-08-02 01:10:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
while (!found_route) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (open_list.size() == 0) {
|
|
|
|
// print_line("NOU OPEN LIST");
|
2014-08-02 01:10:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
//check open list
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
List<Polygon *>::Element *least_cost_poly = NULL;
|
|
|
|
float least_cost = 1e30;
|
2014-08-02 01:10:38 +00:00
|
|
|
|
|
|
|
//this could be faster (cache previous results)
|
2017-03-18 23:36:26 +00:00
|
|
|
for (List<Polygon *>::Element *E = open_list.front(); E; E = E->next()) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Polygon *p = E->get();
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
float cost = p->distance;
|
|
|
|
cost += p->center.distance_to(end_point);
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (cost < least_cost) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
least_cost_poly = E;
|
|
|
|
least_cost = cost;
|
2014-08-02 01:10:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Polygon *p = least_cost_poly->get();
|
2014-08-02 01:10:38 +00:00
|
|
|
//open the neighbours for search
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < p->edges.size(); i++) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Polygon::Edge &e = p->edges[i];
|
2014-08-02 01:10:38 +00:00
|
|
|
|
|
|
|
if (!e.C)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
float distance = p->center.distance_to(e.C->center) + p->distance;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (e.C->prev_edge != -1) {
|
2014-08-02 01:10:38 +00:00
|
|
|
//oh this was visited already, can we win the cost?
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (e.C->distance > distance) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
e.C->prev_edge = e.C_edge;
|
|
|
|
e.C->distance = distance;
|
2014-08-02 01:10:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//add to open neighbours
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
e.C->prev_edge = e.C_edge;
|
|
|
|
e.C->distance = distance;
|
2014-08-02 01:10:38 +00:00
|
|
|
open_list.push_back(e.C);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (e.C == end_poly) {
|
2014-08-02 01:10:38 +00:00
|
|
|
//oh my reached end! stop algorithm
|
2017-03-18 23:36:26 +00:00
|
|
|
found_route = true;
|
2014-08-02 01:10:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found_route)
|
|
|
|
break;
|
|
|
|
|
|
|
|
open_list.erase(least_cost_poly);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found_route) {
|
|
|
|
|
|
|
|
Vector<Vector3> path;
|
|
|
|
|
2014-08-14 13:31:38 +00:00
|
|
|
if (p_optimize) {
|
|
|
|
//string pulling
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Polygon *apex_poly = end_poly;
|
|
|
|
Vector3 apex_point = end_point;
|
|
|
|
Vector3 portal_left = apex_point;
|
|
|
|
Vector3 portal_right = apex_point;
|
|
|
|
Polygon *left_poly = end_poly;
|
|
|
|
Polygon *right_poly = end_poly;
|
|
|
|
Polygon *p = end_poly;
|
2014-08-14 13:31:38 +00:00
|
|
|
path.push_back(end_point);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
while (p) {
|
2014-08-14 13:31:38 +00:00
|
|
|
|
|
|
|
Vector3 left;
|
|
|
|
Vector3 right;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
#define CLOCK_TANGENT(m_a, m_b, m_c) (((m_a) - (m_c)).cross((m_a) - (m_b)))
|
2014-08-14 13:31:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p == begin_poly) {
|
|
|
|
left = begin_point;
|
|
|
|
right = begin_point;
|
2014-08-14 13:31:38 +00:00
|
|
|
} else {
|
|
|
|
int prev = p->prev_edge;
|
2017-03-18 23:36:26 +00:00
|
|
|
int prev_n = (p->prev_edge + 1) % p->edges.size();
|
2014-08-14 13:31:38 +00:00
|
|
|
left = _get_vertex(p->edges[prev].point);
|
|
|
|
right = _get_vertex(p->edges[prev_n].point);
|
|
|
|
|
2015-05-02 20:39:29 +00:00
|
|
|
//if (CLOCK_TANGENT(apex_point,left,(left+right)*0.5).dot(up) < 0){
|
|
|
|
if (p->clockwise) {
|
2017-03-18 23:36:26 +00:00
|
|
|
SWAP(left, right);
|
2014-08-14 13:31:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool skip = false;
|
2014-08-14 13:31:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (CLOCK_TANGENT(apex_point, portal_left, left).dot(up) >= 0) {
|
2014-08-14 13:31:38 +00:00
|
|
|
//process
|
2017-03-18 23:36:26 +00:00
|
|
|
if (portal_left == apex_point || CLOCK_TANGENT(apex_point, left, portal_right).dot(up) > 0) {
|
|
|
|
left_poly = p;
|
|
|
|
portal_left = left;
|
2014-08-14 13:31:38 +00:00
|
|
|
} else {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
_clip_path(path, apex_poly, portal_right, right_poly);
|
2014-11-17 10:46:11 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
apex_point = portal_right;
|
|
|
|
p = right_poly;
|
|
|
|
left_poly = p;
|
|
|
|
apex_poly = p;
|
|
|
|
portal_left = apex_point;
|
|
|
|
portal_right = apex_point;
|
2014-08-14 13:31:38 +00:00
|
|
|
path.push_back(apex_point);
|
2017-03-18 23:36:26 +00:00
|
|
|
skip = true;
|
2014-08-14 13:31:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (!skip && CLOCK_TANGENT(apex_point, portal_right, right).dot(up) <= 0) {
|
2014-08-14 13:31:38 +00:00
|
|
|
//process
|
2017-03-18 23:36:26 +00:00
|
|
|
if (portal_right == apex_point || CLOCK_TANGENT(apex_point, right, portal_left).dot(up) < 0) {
|
|
|
|
right_poly = p;
|
|
|
|
portal_right = right;
|
2014-08-14 13:31:38 +00:00
|
|
|
} else {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
_clip_path(path, apex_poly, portal_left, left_poly);
|
2014-11-17 10:46:11 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
apex_point = portal_left;
|
|
|
|
p = left_poly;
|
|
|
|
right_poly = p;
|
|
|
|
apex_poly = p;
|
|
|
|
portal_right = apex_point;
|
|
|
|
portal_left = apex_point;
|
2014-08-14 13:31:38 +00:00
|
|
|
path.push_back(apex_point);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p != begin_poly)
|
|
|
|
p = p->edges[p->prev_edge].C;
|
2014-08-14 13:31:38 +00:00
|
|
|
else
|
2017-03-18 23:36:26 +00:00
|
|
|
p = NULL;
|
2014-08-14 13:31:38 +00:00
|
|
|
}
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (path[path.size() - 1] != begin_point)
|
2014-08-14 13:31:38 +00:00
|
|
|
path.push_back(begin_point);
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2014-08-14 13:31:38 +00:00
|
|
|
path.invert();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
//midpoints
|
2017-03-18 23:36:26 +00:00
|
|
|
Polygon *p = end_poly;
|
2014-08-14 13:31:38 +00:00
|
|
|
|
|
|
|
path.push_back(end_point);
|
2017-03-18 23:36:26 +00:00
|
|
|
while (true) {
|
2014-08-14 13:31:38 +00:00
|
|
|
int prev = p->prev_edge;
|
2017-03-18 23:36:26 +00:00
|
|
|
int prev_n = (p->prev_edge + 1) % p->edges.size();
|
|
|
|
Vector3 point = (_get_vertex(p->edges[prev].point) + _get_vertex(p->edges[prev_n].point)) * 0.5;
|
2014-08-14 13:31:38 +00:00
|
|
|
path.push_back(point);
|
|
|
|
p = p->edges[prev].C;
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p == begin_poly)
|
2014-08-14 13:31:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
path.push_back(begin_point);
|
|
|
|
|
2017-01-14 17:03:38 +00:00
|
|
|
path.invert();
|
2014-08-14 13:31:38 +00:00
|
|
|
}
|
2014-08-02 01:10:38 +00:00
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Vector<Vector3>();
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector3 Navigation::get_closest_point_to_segment(const Vector3 &p_from, const Vector3 &p_to, const bool &p_use_collision) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool use_collision = p_use_collision;
|
2014-08-02 01:10:38 +00:00
|
|
|
Vector3 closest_point;
|
2017-03-18 23:36:26 +00:00
|
|
|
float closest_point_d = 1e20;
|
|
|
|
NavMesh *closest_navmesh = NULL;
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (Map<int, NavMesh>::Element *E = navmesh_map.front(); E; E = E->next()) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
|
|
|
if (!E->get().linked)
|
|
|
|
continue;
|
2017-03-18 23:36:26 +00:00
|
|
|
for (List<Polygon>::Element *F = E->get().polygons.front(); F; F = F->next()) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Polygon &p = F->get();
|
|
|
|
for (int i = 2; i < p.edges.size(); i++) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Face3 f(_get_vertex(p.edges[0].point), _get_vertex(p.edges[i - 1].point), _get_vertex(p.edges[i].point));
|
2014-08-02 01:10:38 +00:00
|
|
|
Vector3 inters;
|
2017-03-18 23:36:26 +00:00
|
|
|
if (f.intersects_segment(p_from, p_to, &inters)) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
|
|
|
if (!use_collision) {
|
2017-03-18 23:36:26 +00:00
|
|
|
closest_point = inters;
|
|
|
|
use_collision = true;
|
|
|
|
closest_point_d = p_from.distance_to(inters);
|
|
|
|
closest_navmesh = p.owner;
|
|
|
|
} else if (closest_point_d > inters.distance_to(p_from)) {
|
|
|
|
|
|
|
|
closest_point = inters;
|
|
|
|
closest_point_d = p_from.distance_to(inters);
|
|
|
|
closest_navmesh = p.owner;
|
2014-08-02 01:10:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!use_collision) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < p.edges.size(); i++) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector3 a, b;
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Geometry::get_closest_points_between_segments(p_from, p_to, _get_vertex(p.edges[i].point), _get_vertex(p.edges[(i + 1) % p.edges.size()].point), a, b);
|
2014-08-02 01:10:38 +00:00
|
|
|
|
|
|
|
float d = a.distance_to(b);
|
2017-03-18 23:36:26 +00:00
|
|
|
if (d < closest_point_d) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
closest_point_d = d;
|
|
|
|
closest_point = b;
|
|
|
|
closest_navmesh = p.owner;
|
2014-08-02 01:10:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-03 03:10:51 +00:00
|
|
|
if (closest_navmesh && closest_navmesh->owner) {
|
|
|
|
//print_line("navmesh is: "+closest_navmesh->owner->cast_to<Node>()->get_name());
|
|
|
|
}
|
|
|
|
|
2014-08-02 01:10:38 +00:00
|
|
|
return closest_point;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector3 Navigation::get_closest_point(const Vector3 &p_point) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
|
|
|
Vector3 closest_point;
|
2017-03-18 23:36:26 +00:00
|
|
|
float closest_point_d = 1e20;
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (Map<int, NavMesh>::Element *E = navmesh_map.front(); E; E = E->next()) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
|
|
|
if (!E->get().linked)
|
|
|
|
continue;
|
2017-03-18 23:36:26 +00:00
|
|
|
for (List<Polygon>::Element *F = E->get().polygons.front(); F; F = F->next()) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Polygon &p = F->get();
|
|
|
|
for (int i = 2; i < p.edges.size(); i++) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Face3 f(_get_vertex(p.edges[0].point), _get_vertex(p.edges[i - 1].point), _get_vertex(p.edges[i].point));
|
2014-08-02 01:10:38 +00:00
|
|
|
Vector3 inters = f.get_closest_point_to(p_point);
|
|
|
|
float d = inters.distance_to(p_point);
|
2017-03-18 23:36:26 +00:00
|
|
|
if (d < closest_point_d) {
|
|
|
|
closest_point = inters;
|
|
|
|
closest_point_d = d;
|
2014-08-02 01:10:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return closest_point;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector3 Navigation::get_closest_point_normal(const Vector3 &p_point) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
|
|
|
Vector3 closest_point;
|
|
|
|
Vector3 closest_normal;
|
2017-03-18 23:36:26 +00:00
|
|
|
float closest_point_d = 1e20;
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (Map<int, NavMesh>::Element *E = navmesh_map.front(); E; E = E->next()) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
|
|
|
if (!E->get().linked)
|
|
|
|
continue;
|
2017-03-18 23:36:26 +00:00
|
|
|
for (List<Polygon>::Element *F = E->get().polygons.front(); F; F = F->next()) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Polygon &p = F->get();
|
|
|
|
for (int i = 2; i < p.edges.size(); i++) {
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Face3 f(_get_vertex(p.edges[0].point), _get_vertex(p.edges[i - 1].point), _get_vertex(p.edges[i].point));
|
2014-08-02 01:10:38 +00:00
|
|
|
Vector3 inters = f.get_closest_point_to(p_point);
|
|
|
|
float d = inters.distance_to(p_point);
|
2017-03-18 23:36:26 +00:00
|
|
|
if (d < closest_point_d) {
|
|
|
|
closest_point = inters;
|
|
|
|
closest_point_d = d;
|
|
|
|
closest_normal = f.get_plane().normal;
|
2014-08-02 01:10:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return closest_normal;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Object *Navigation::get_closest_point_owner(const Vector3 &p_point) {
|
2015-04-21 19:47:49 +00:00
|
|
|
|
|
|
|
Vector3 closest_point;
|
2017-03-18 23:36:26 +00:00
|
|
|
Object *owner = NULL;
|
|
|
|
float closest_point_d = 1e20;
|
2015-04-21 19:47:49 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (Map<int, NavMesh>::Element *E = navmesh_map.front(); E; E = E->next()) {
|
2015-04-21 19:47:49 +00:00
|
|
|
|
|
|
|
if (!E->get().linked)
|
|
|
|
continue;
|
2017-03-18 23:36:26 +00:00
|
|
|
for (List<Polygon>::Element *F = E->get().polygons.front(); F; F = F->next()) {
|
2015-04-21 19:47:49 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Polygon &p = F->get();
|
|
|
|
for (int i = 2; i < p.edges.size(); i++) {
|
2015-04-21 19:47:49 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Face3 f(_get_vertex(p.edges[0].point), _get_vertex(p.edges[i - 1].point), _get_vertex(p.edges[i].point));
|
2015-04-21 19:47:49 +00:00
|
|
|
Vector3 inters = f.get_closest_point_to(p_point);
|
|
|
|
float d = inters.distance_to(p_point);
|
2017-03-18 23:36:26 +00:00
|
|
|
if (d < closest_point_d) {
|
|
|
|
closest_point = inters;
|
|
|
|
closest_point_d = d;
|
|
|
|
owner = E->get().owner;
|
2015-04-21 19:47:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return owner;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void Navigation::set_up_vector(const Vector3 &p_up) {
|
2014-08-14 13:31:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
up = p_up;
|
2014-08-14 13:31:38 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector3 Navigation::get_up_vector() const {
|
2014-08-14 13:31:38 +00:00
|
|
|
|
|
|
|
return up;
|
|
|
|
}
|
|
|
|
|
2014-08-02 01:10:38 +00:00
|
|
|
void Navigation::_bind_methods() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("navmesh_create", "mesh:NavigationMesh", "xform", "owner"), &Navigation::navmesh_create, DEFVAL(Variant()));
|
|
|
|
ObjectTypeDB::bind_method(_MD("navmesh_set_transform", "id", "xform"), &Navigation::navmesh_set_transform);
|
|
|
|
ObjectTypeDB::bind_method(_MD("navmesh_remove", "id"), &Navigation::navmesh_remove);
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("get_simple_path", "start", "end", "optimize"), &Navigation::get_simple_path, DEFVAL(true));
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_closest_point_to_segment", "start", "end", "use_collision"), &Navigation::get_closest_point_to_segment, DEFVAL(false));
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_closest_point", "to_point"), &Navigation::get_closest_point);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_closest_point_normal", "to_point"), &Navigation::get_closest_point_normal);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_closest_point_owner", "to_point"), &Navigation::get_closest_point_owner);
|
2014-08-02 01:10:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_up_vector", "up"), &Navigation::set_up_vector);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_up_vector"), &Navigation::get_up_vector);
|
2014-08-14 13:31:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "up_vector"), _SCS("set_up_vector"), _SCS("get_up_vector"));
|
2014-08-02 01:10:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Navigation::Navigation() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND(sizeof(Point) != 8);
|
|
|
|
cell_size = 0.01; //one centimeter
|
|
|
|
last_id = 1;
|
|
|
|
up = Vector3(0, 1, 0);
|
2014-08-02 01:10:38 +00:00
|
|
|
}
|