summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYaohua Xiong <xiongyaohua@gmail.com>2022-12-14 09:46:07 +0800
committerYaohua Xiong <xiongyaohua@gmail.com>2022-12-14 09:46:07 +0800
commitf806c679de35cd681ba58a1e47c828620a276445 (patch)
tree1a11422d0b0748d6cb0d1e401d409edfd68c5b26
parentdb6976eed4c59b3a320cb26938898c732ef6b7ca (diff)
Fix Curve2D::get_closest_offset
-rw-r--r--scene/resources/curve.cpp14
1 files changed, 8 insertions, 6 deletions
diff --git a/scene/resources/curve.cpp b/scene/resources/curve.cpp
index bc2149a8c6..4ab5fae58e 100644
--- a/scene/resources/curve.cpp
+++ b/scene/resources/curve.cpp
@@ -1031,10 +1031,11 @@ Vector2 Curve2D::get_closest_point(const Vector2 &p_to_point) const {
real_t nearest_dist = -1.0f;
for (int i = 0; i < pc - 1; i++) {
+ const real_t interval = baked_dist_cache[i + 1] - baked_dist_cache[i];
Vector2 origin = r[i];
- Vector2 direction = (r[i + 1] - origin) / bake_interval;
+ Vector2 direction = (r[i + 1] - origin) / interval;
- real_t d = CLAMP((p_to_point - origin).dot(direction), 0.0f, bake_interval);
+ real_t d = CLAMP((p_to_point - origin).dot(direction), 0.0f, interval);
Vector2 proj = origin + direction * d;
real_t dist = proj.distance_squared_to(p_to_point);
@@ -1070,10 +1071,13 @@ real_t Curve2D::get_closest_offset(const Vector2 &p_to_point) const {
real_t offset = 0.0f;
for (int i = 0; i < pc - 1; i++) {
+ offset = baked_dist_cache[i];
+
+ const real_t interval = baked_dist_cache[i + 1] - baked_dist_cache[i];
Vector2 origin = r[i];
- Vector2 direction = (r[i + 1] - origin) / bake_interval;
+ Vector2 direction = (r[i + 1] - origin) / interval;
- real_t d = CLAMP((p_to_point - origin).dot(direction), 0.0f, bake_interval);
+ real_t d = CLAMP((p_to_point - origin).dot(direction), 0.0f, interval);
Vector2 proj = origin + direction * d;
real_t dist = proj.distance_squared_to(p_to_point);
@@ -1082,8 +1086,6 @@ real_t Curve2D::get_closest_offset(const Vector2 &p_to_point) const {
nearest = offset + d;
nearest_dist = dist;
}
-
- offset += bake_interval;
}
return nearest;