Skip to content

Commit 1a91570

Browse files
committed
[Curve3D] Fix middle point forward vector when control1=end and control2=begin; issue #88923
Co-authored-by: A Thousand Ships <[email protected]>
1 parent 80a3d20 commit 1a91570

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

scene/resources/curve.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,10 @@ Vector2 Curve2D::_calculate_tangent(const Vector2 &p_begin, const Vector2 &p_con
899899
}
900900
}
901901

902+
if (p_control_1.is_equal_approx(p_end) && p_control_2.is_equal_approx(p_begin)) {
903+
return (p_end - p_begin).normalized();
904+
}
905+
902906
return p_begin.bezier_derivative(p_control_1, p_control_2, p_end, p_t).normalized();
903907
}
904908

@@ -1648,6 +1652,10 @@ Vector3 Curve3D::_calculate_tangent(const Vector3 &p_begin, const Vector3 &p_con
16481652
}
16491653
}
16501654

1655+
if (p_control_1.is_equal_approx(p_end) && p_control_2.is_equal_approx(p_begin)) {
1656+
return (p_end - p_begin).normalized();
1657+
}
1658+
16511659
return p_begin.bezier_derivative(p_control_1, p_control_2, p_end, p_t).normalized();
16521660
}
16531661

tests/scene/test_curve_2d.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,19 @@ TEST_CASE("[Curve2D] Sampling") {
236236
CHECK(curve->get_closest_point(Vector2(50, 50)) == Vector2(0, 50));
237237
CHECK(curve->get_closest_point(Vector2(0, 100)) == Vector2(0, 50));
238238
}
239+
240+
SUBCASE("sample_baked_with_rotation, linear curve with control1 = end and control2 = begin") {
241+
// Regression test for issue #88923
242+
// The Vector2s that aren't relevant to the issue have x = 2 or x = -2.
243+
// They're just set to make collisions with corner cases less likely
244+
// that involve zero-vector control points.
245+
Ref<Curve2D> cross_linear_curve = memnew(Curve2D);
246+
cross_linear_curve->set_bake_interval(0.5);
247+
cross_linear_curve->add_point(Vector2(), Vector2(-2, 0), Vector2(1, 0));
248+
cross_linear_curve->add_point(Vector2(1, 0), Vector2(-1, 0), Vector2(2, 0));
249+
CHECK(cross_linear_curve->get_baked_points().size() >= 3);
250+
CHECK(cross_linear_curve->sample_baked_with_rotation(cross_linear_curve->get_closest_offset(Vector2(0.5, 0))).is_equal_approx(Transform2D(Vector2(1, 0), Vector2(0, 1), Vector2(0.5, 0))));
251+
}
239252
}
240253

241254
TEST_CASE("[Curve2D] Tessellation") {

tests/scene/test_curve_3d.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,18 @@ TEST_CASE("[Curve3D] Sampling") {
234234
c->add_point(Vector3(0, .1, 1));
235235
CHECK_LT((c->sample_baked_up_vector(c->get_closest_offset(Vector3(0, 0, .9))) - Vector3(0, 0.995037, -0.099504)).length(), 0.01);
236236
}
237+
238+
SUBCASE("sample_baked_with_rotation, linear curve with control1 = end and control2 = begin") {
239+
// Regression test for issue #88923
240+
// The Vector3s that aren't relevant to the issue have z = 2.
241+
// They're just set to make collisions with corner cases less likely
242+
// that involve zero-vector control points.
243+
Ref<Curve3D> cross_linear_curve = memnew(Curve3D);
244+
cross_linear_curve->add_point(Vector3(), Vector3(-1, 0, 2), Vector3(1, 0, 0));
245+
cross_linear_curve->add_point(Vector3(1, 0, 0), Vector3(-1, 0, 0), Vector3(1, 0, 2));
246+
CHECK(cross_linear_curve->get_baked_points().size() >= 3);
247+
CHECK(cross_linear_curve->sample_baked_with_rotation(cross_linear_curve->get_closest_offset(Vector3(0.5, 0, 0))).is_equal_approx(Transform3D(Basis(Vector3(0, 0, 1), Vector3(0, 1, 0), Vector3(-1, 0, 0)), Vector3(0.5, 0, 0))));
248+
}
237249
}
238250

239251
TEST_CASE("[Curve3D] Tessellation") {

0 commit comments

Comments
 (0)