summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan Linietsky <reduzio@gmail.com>2015-02-11 07:12:15 -0300
committerJuan Linietsky <reduzio@gmail.com>2015-02-11 07:12:15 -0300
commitb4e1c1d717c41b48e12f8712fb5d4bbf84732229 (patch)
treec5f86d714b262dbcf4820e4f360150f9e9d95cd8
parent7f88df73bef93ce2627be2921919216a3fae5dc3 (diff)
parent8db3c0a4dbf32d498bd9dfd84eab7f4a18757fea (diff)
Merge pull request #1327 from romulox-x/polygonPathFinderClosestPoint
changed PolygonPathFinder::get_closest_point to return the closest posit...
-rw-r--r--scene/resources/polygon_path_finder.cpp26
1 files changed, 17 insertions, 9 deletions
diff --git a/scene/resources/polygon_path_finder.cpp b/scene/resources/polygon_path_finder.cpp
index 9f691d6ad3..e8cdec66df 100644
--- a/scene/resources/polygon_path_finder.cpp
+++ b/scene/resources/polygon_path_finder.cpp
@@ -525,24 +525,32 @@ bool PolygonPathFinder::is_point_inside(const Vector2& p_point) const {
Vector2 PolygonPathFinder::get_closest_point(const Vector2& p_point) const {
- int closest_idx=-1;
float closest_dist=1e20;
- for(int i=0;i<points.size()-2;i++) {
+ Vector2 closest_point;
+
+ for (Set<Edge>::Element *E=edges.front();E;E=E->next()) {
+
+ const Edge& e=E->get();
+ Vector2 seg[2]={
+ points[e.points[0]].pos,
+ points[e.points[1]].pos
+ };
+
+
+ Vector2 closest = Geometry::get_closest_point_to_segment_2d(p_point,seg);
+ float d = p_point.distance_squared_to(closest);
- float d = p_point.distance_squared_to(points[i].pos);
if (d<closest_dist) {
closest_dist=d;
- closest_idx=i;
+ closest_point=closest;
}
-
}
+
+ ERR_FAIL_COND_V(closest_dist==1e20,Vector2());
- ERR_FAIL_COND_V(closest_idx==-1,Vector2());
-
- return points[closest_idx].pos;
+ return closest_point;
}
-
Vector<Vector2> PolygonPathFinder::get_intersections(const Vector2& p_from, const Vector2& p_to) const {
Vector<Vector2> inters;