فهرست منبع

Fix Rect2::distance_to() not returning 0

Bernhard Liebl 7 سال پیش
والد
کامیت
41c11894f1
1فایلهای تغییر یافته به همراه15 افزوده شده و 6 حذف شده
  1. 15 6
      core/math/math_2d.h

+ 15 - 6
core/math/math_2d.h

@@ -239,22 +239,31 @@ struct Rect2 {
 
 
 	inline real_t distance_to(const Vector2 &p_point) const {
 	inline real_t distance_to(const Vector2 &p_point) const {
 
 
-		real_t dist = 1e20;
+		real_t dist;
+		bool inside = true;
 
 
 		if (p_point.x < position.x) {
 		if (p_point.x < position.x) {
-			dist = MIN(dist, position.x - p_point.x);
+			real_t d = position.x - p_point.x;
+			dist = inside ? d : MIN(dist, d);
+			inside = false;
 		}
 		}
 		if (p_point.y < position.y) {
 		if (p_point.y < position.y) {
-			dist = MIN(dist, position.y - p_point.y);
+			real_t d = position.y - p_point.y;
+			dist = inside ? d : MIN(dist, d);
+			inside = false;
 		}
 		}
 		if (p_point.x >= (position.x + size.x)) {
 		if (p_point.x >= (position.x + size.x)) {
-			dist = MIN(p_point.x - (position.x + size.x), dist);
+			real_t d = p_point.x - (position.x + size.x);
+			dist = inside ? d : MIN(dist, d);
+			inside = false;
 		}
 		}
 		if (p_point.y >= (position.y + size.y)) {
 		if (p_point.y >= (position.y + size.y)) {
-			dist = MIN(p_point.y - (position.y + size.y), dist);
+			real_t d = p_point.y - (position.y + size.y);
+			dist = inside ? d : MIN(dist, d);
+			inside = false;
 		}
 		}
 
 
-		if (dist == 1e20)
+		if (inside)
 			return 0;
 			return 0;
 		else
 		else
 			return dist;
 			return dist;