Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better heuristic for the shortest path algorithm for navigation2D #22047

Merged
merged 1 commit into from
Sep 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions scene/2d/navigation2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,34 @@ Vector<Vector2> Navigation2D::get_simple_path(const Vector2 &p_start, const Vect
Polygon *p = E->get();

float cost = p->distance;
cost += p->center.distance_to(end_point);

if (cost < least_cost) {
#ifdef USE_ENTRY_POINT
int es = p->edges.size();

float shortest_distance = 1e30;

for (int i = 0; i < es; i++) {
Polygon::Edge &e = p->edges.write[i];

if (!e.C)
continue;

Vector2 edge[2] = {
_get_vertex(p->edges[i].point),
_get_vertex(p->edges[(i + 1) % es].point)
};

Vector2 edge_point = Geometry::get_closest_point_to_segment_2d(p->entry, edge);
float dist = p->entry.distance_to(edge_point);
if (dist < shortest_distance)
shortest_distance = dist;
}

cost += shortest_distance;
#else
cost += p->center.distance_to(end_point);
#endif
if (cost < least_cost) {
least_cost_poly = E;
least_cost = cost;
}
Expand Down
40 changes: 38 additions & 2 deletions scene/3d/navigation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

#include "navigation.h"

#define USE_ENTRY_POINT

void Navigation::_navmesh_link(int p_id) {

ERR_FAIL_COND(!navmesh_map.has(p_id));
Expand Down Expand Up @@ -331,7 +333,18 @@ Vector<Vector3> Navigation::get_simple_path(const Vector3 &p_start, const Vector
if (begin_poly->edges[i].C) {

begin_poly->edges[i].C->prev_edge = begin_poly->edges[i].C_edge;
#ifdef USE_ENTRY_POINT
Vector3 edge[2] = {
_get_vertex(begin_poly->edges[i].point),
_get_vertex(begin_poly->edges[(i + 1) % begin_poly->edges.size()].point)
};

Vector3 entry = Geometry::get_closest_point_to_segment(begin_poly->entry, edge);
begin_poly->edges[i].C->distance = begin_poly->entry.distance_to(entry);
begin_poly->edges[i].C->entry = entry;
#else
begin_poly->edges[i].C->distance = begin_poly->center.distance_to(begin_poly->edges[i].C->center);
#endif
open_list.push_back(begin_poly->edges[i].C);

if (begin_poly->edges[i].C == end_poly) {
Expand All @@ -356,10 +369,33 @@ Vector<Vector3> Navigation::get_simple_path(const Vector3 &p_start, const Vector
Polygon *p = E->get();

float cost = p->distance;
cost += p->center.distance_to(end_point);
#ifdef USE_ENTRY_POINT
int es = p->edges.size();

if (cost < least_cost) {
float shortest_distance = 1e30;

for (int i = 0; i < es; i++) {
Polygon::Edge &e = p->edges.write[i];

if (!e.C)
continue;

Vector3 edge[2] = {
_get_vertex(p->edges[i].point),
_get_vertex(p->edges[(i + 1) % es].point)
};

Vector3 edge_point = Geometry::get_closest_point_to_segment(p->entry, edge);
float dist = p->entry.distance_to(edge_point);
if (dist < shortest_distance)
shortest_distance = dist;
}

cost += shortest_distance;
#else
cost += p->center.distance_to(end_point);
#endif
if (cost < least_cost) {
least_cost_poly = E;
least_cost = cost;
}
Expand Down
1 change: 1 addition & 0 deletions scene/3d/navigation.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class Navigation : public Spatial {
Vector<Edge> edges;

Vector3 center;
Vector3 entry;

float distance;
int prev_edge;
Expand Down