Skip to content

Commit c1a2821

Browse files
committed
Removed unnecessary operations for set_rotation and rectangle collision. And added set_rotation test.
1 parent 77eaec7 commit c1a2821

File tree

3 files changed

+60
-13
lines changed

3 files changed

+60
-13
lines changed

core/math/transform_2d.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ void Transform2D::set_rotation(real_t p_rot) {
9393
columns[0][1] = sr;
9494
columns[1][0] = -sr;
9595
columns[1][1] = cr;
96-
set_scale(scale);
96+
columns[0] *= scale.x;
97+
columns[1] *= scale.y;
9798
}
9899

99100
Transform2D::Transform2D(real_t p_rot, const Vector2 &p_pos) {

modules/godot_physics_2d/godot_collision_solver_2d_sat.cpp

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -479,11 +479,15 @@ static void _collision_segment_rectangle(const GodotShape2D *p_a, const Transfor
479479
return;
480480
}
481481

482-
if (!separator.test_axis(p_transform_b.columns[0].normalized())) {
482+
const Vector2 rect_fn = p_transform_b.columns[0].normalized();
483+
484+
if (!separator.test_axis(rect_fn)) {
483485
return;
484486
}
485487

486-
if (!separator.test_axis(p_transform_b.columns[1].normalized())) {
488+
const Vector2 nd_rect_fn = Vector2(-rect_fn[1], rect_fn[0]);
489+
490+
if (!separator.test_axis(nd_rect_fn)) {
487491
return;
488492
}
489493

@@ -652,11 +656,14 @@ static void _collision_circle_rectangle(const GodotShape2D *p_a, const Transform
652656
const Vector2 *axis = &p_transform_b.columns[0];
653657
//const Vector2& half_extents = rectangle_B->get_half_extents();
654658

655-
if (!separator.test_axis(axis[0].normalized())) {
659+
const Vector2 rect_fn = axis[0].normalized();
660+
if (!separator.test_axis(rect_fn)) {
656661
return;
657662
}
658663

659-
if (!separator.test_axis(axis[1].normalized())) {
664+
const Vector2 nd_rect_fn = Vector2(-rect_fn[1], rect_fn[0]);
665+
666+
if (!separator.test_axis(nd_rect_fn)) {
660667
return;
661668
}
662669

@@ -770,21 +777,31 @@ static void _collision_rectangle_rectangle(const GodotShape2D *p_a, const Transf
770777
return;
771778
}
772779

780+
//It's the same normal for both axis tests,
781+
//but the second one is just inverted and has the x as inverse.
782+
const Vector2 rect_fn = p_transform_a.columns[0].normalized();
783+
773784
//box faces A
774-
if (!separator.test_axis(p_transform_a.columns[0].normalized())) {
785+
if (!separator.test_axis(rect_fn)) {
775786
return;
776787
}
777788

778-
if (!separator.test_axis(p_transform_a.columns[1].normalized())) {
789+
const Vector2 nd_rect_fn = Vector2(-rect_fn[1], rect_fn[0]);
790+
791+
if (!separator.test_axis(nd_rect_fn)) {
779792
return;
780793
}
781794

795+
const Vector2 rect_fn2 = p_transform_b.columns[0].normalized();
796+
782797
//box faces B
783-
if (!separator.test_axis(p_transform_b.columns[0].normalized())) {
798+
if (!separator.test_axis(rect_fn2)) {
784799
return;
785800
}
786801

787-
if (!separator.test_axis(p_transform_b.columns[1].normalized())) {
802+
const Vector2 nd_rect_fn2 = Vector2(-rect_fn2[1], rect_fn2[0]);
803+
804+
if (!separator.test_axis(nd_rect_fn2)) {
788805
return;
789806
}
790807

@@ -844,12 +861,18 @@ static void _collision_rectangle_capsule(const GodotShape2D *p_a, const Transfor
844861
return;
845862
}
846863

864+
//It's the same normal for both axis tests,
865+
//but the second one is just inverted and has the x as inverse.
866+
const Vector2 rect_fn = p_transform_a.columns[0].normalized();
867+
847868
//box faces
848-
if (!separator.test_axis(p_transform_a.columns[0].normalized())) {
869+
if (!separator.test_axis(rect_fn)) {
849870
return;
850871
}
851872

852-
if (!separator.test_axis(p_transform_a.columns[1].normalized())) {
873+
const Vector2 nd_rect_fn = Vector2(-rect_fn[1], rect_fn[0]);
874+
875+
if (!separator.test_axis(nd_rect_fn)) {
853876
return;
854877
}
855878

@@ -922,12 +945,18 @@ static void _collision_rectangle_convex_polygon(const GodotShape2D *p_a, const T
922945
return;
923946
}
924947

948+
//It's the same normal for both axis tests,
949+
//but the second one is just inverted and has the x as inverse.
950+
const Vector2 rect_fn = p_transform_a.columns[0].normalized();
951+
925952
//box faces
926-
if (!separator.test_axis(p_transform_a.columns[0].normalized())) {
953+
if (!separator.test_axis(rect_fn)) {
927954
return;
928955
}
929956

930-
if (!separator.test_axis(p_transform_a.columns[1].normalized())) {
957+
const Vector2 nd_rect_fn = Vector2(-rect_fn[1], rect_fn[0]);
958+
959+
if (!separator.test_axis(nd_rect_fn)) {
931960
return;
932961
}
933962

tests/core/math/test_transform_2d.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,23 @@ TEST_CASE("[Transform2D] Affine inverse") {
113113
CHECK(affine_inverted_again == orig);
114114
}
115115

116+
TEST_CASE("[TRANSFORM2D Set rotation]") {
117+
const real_t rot = 1.8;
118+
const Vector2 scale = Vector2(2.5, 5);
119+
const Vector2 useless_pos = Vector2();
120+
Transform2D subject = Transform2D(0.2, scale, 0.0, useless_pos);
121+
122+
subject.set_rotation(rot);
123+
124+
const real_t res_rot = subject.get_rotation();
125+
const Vector2 res_scale = subject.get_scale();
126+
const real_t tol = 0.001;
127+
128+
CHECK(Math::is_equal_approx(rot, res_rot, tol));
129+
CHECK(Math::is_equal_approx(scale.x, res_scale.x, tol));
130+
CHECK(Math::is_equal_approx(scale.y, res_scale.y, tol));
131+
}
132+
116133
TEST_CASE("[Transform2D] Orthonormalized") {
117134
const Transform2D T = create_dummy_transform();
118135
const Transform2D orthonormalized_T = T.orthonormalized();

0 commit comments

Comments
 (0)