summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNoshyaar <poommetee@protonmail.com>2018-01-01 18:37:46 +0700
committerGitHub <noreply@github.com>2018-01-01 18:37:46 +0700
commit8dcc28254c9695e9e13b3a1f4d7b2b3e97363dc3 (patch)
tree2324969d1c2e34c0900fa4691602390688d087bc
parentdefdb5761d20739a0a469507d55fa75e06aa26c3 (diff)
parent41c11894f1402082a2400987899ddf18ca755325 (diff)
Merge pull request #15091 from poke1024/fix-rect2-distance-to
Fix Rect2::distance_to() not returning 0
-rw-r--r--core/math/math_2d.h21
1 files changed, 15 insertions, 6 deletions
diff --git a/core/math/math_2d.h b/core/math/math_2d.h
index 4635a4da55..4939e7ae99 100644
--- a/core/math/math_2d.h
+++ b/core/math/math_2d.h
@@ -303,22 +303,31 @@ struct Rect2 {
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) {
- 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) {
- 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)) {
- 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)) {
- 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;
else
return dist;