Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

GodotPhysics2D: Pass SAT cast flags at runtime instead of as template parameters

Rémi Verschelde89989ef32f5c
diff --git a/modules/godot_physics_2d/godot_collision_solver_2d_sat.cpp b/modules/godot_physics_2d/godot_collision_solver_2d_sat.cpp
index cf049042db2..823ef26dde0 100644
--- a/modules/godot_physics_2d/godot_collision_solver_2d_sat.cpp
+++ b/modules/godot_physics_2d/godot_collision_solver_2d_sat.cpp
@@ -171,7 +171,7 @@ static void _generate_contacts_from_supports(const Vector2 *p_points_A, int p_po
contacts_func(points_A, pointcount_A, points_B, pointcount_B, p_collector);
}
-template <typename ShapeA, typename ShapeB, bool castA = false, bool castB = false, bool withMargin = false>
+template <typename ShapeA, typename ShapeB, bool withMargin = false>
class SeparatorAxisTest2D {
const ShapeA *shape_A = nullptr;
const ShapeB *shape_B = nullptr;
@@ -185,6 +185,8 @@ class SeparatorAxisTest2D {
#endif
Vector2 motion_A;
Vector2 motion_B;
+ bool cast_A = false;
+ bool cast_B = false;
real_t margin_A = 0.0;
real_t margin_B = 0.0;
_CollectorCallback2D *callback;
@@ -202,7 +204,7 @@ public:
}
_FORCE_INLINE_ bool test_cast() {
- if (castA) {
+ if (cast_A) {
Vector2 na = motion_A.normalized();
if (!test_axis(na)) {
return false;
@@ -212,7 +214,7 @@ public:
}
}
- if (castB) {
+ if (cast_B) {
Vector2 nb = motion_B.normalized();
if (!test_axis(nb)) {
return false;
@@ -236,13 +238,13 @@ public:
real_t min_A = 0.0, max_A = 0.0, min_B = 0.0, max_B = 0.0;
- if (castA) {
+ if (cast_A) {
shape_A->project_range_cast(motion_A, axis, *transform_A, min_A, max_A);
} else {
shape_A->project_range(axis, *transform_A, min_A, max_A);
}
- if (castB) {
+ if (cast_B) {
shape_B->project_range_cast(motion_B, axis, *transform_B, min_B, max_B);
} else {
shape_B->project_range(axis, *transform_B, min_B, max_B);
@@ -318,7 +320,7 @@ public:
Vector2 supports_A[max_supports];
int support_count_A;
- if (castA) {
+ if (cast_A) {
shape_A->get_supports_transformed_cast(motion_A, -best_axis, *transform_A, supports_A, support_count_A);
} else {
shape_A->get_supports(transform_A->basis_xform_inv(-best_axis).normalized(), supports_A, support_count_A);
@@ -335,7 +337,7 @@ public:
Vector2 supports_B[max_supports];
int support_count_B;
- if (castB) {
+ if (cast_B) {
shape_B->get_supports_transformed_cast(motion_B, best_axis, *transform_B, supports_B, support_count_B);
} else {
shape_B->get_supports(transform_B->basis_xform_inv(best_axis).normalized(), supports_B, support_count_B);
@@ -359,7 +361,7 @@ public:
}
}
- _FORCE_INLINE_ SeparatorAxisTest2D(const ShapeA *p_shape_A, const Transform2D &p_transform_a, const ShapeB *p_shape_B, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_A = Vector2(), const Vector2 &p_motion_B = Vector2(), real_t p_margin_A = 0, real_t p_margin_B = 0) {
+ _FORCE_INLINE_ SeparatorAxisTest2D(const ShapeA *p_shape_A, const Transform2D &p_transform_a, const ShapeB *p_shape_B, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_A, const Vector2 &p_motion_B, bool p_cast_A, bool p_cast_B, real_t p_margin_A = 0, real_t p_margin_B = 0) {
margin_A = p_margin_A;
margin_B = p_margin_B;
shape_A = p_shape_A;
@@ -368,6 +370,8 @@ public:
transform_B = &p_transform_b;
motion_A = p_motion_A;
motion_B = p_motion_B;
+ cast_A = p_cast_A;
+ cast_B = p_cast_B;
callback = p_collector;
}
};
@@ -376,18 +380,18 @@ public:
#define TEST_POINT(m_a, m_b) \
((!separator.test_axis(((m_a) - (m_b)).normalized())) || \
- (castA && !separator.test_axis(((m_a) + p_motion_a - (m_b)).normalized())) || \
- (castB && !separator.test_axis(((m_a) - ((m_b) + p_motion_b)).normalized())) || \
- (castA && castB && !separator.test_axis(((m_a) + p_motion_a - ((m_b) + p_motion_b)).normalized())))
+ (p_cast_A && !separator.test_axis(((m_a) + p_motion_a - (m_b)).normalized())) || \
+ (p_cast_B && !separator.test_axis(((m_a) - ((m_b) + p_motion_b)).normalized())) || \
+ (p_cast_A && p_cast_B && !separator.test_axis(((m_a) + p_motion_a - ((m_b) + p_motion_b)).normalized())))
-typedef void (*CollisionFunc)(const GodotShape2D *, const Transform2D &, const GodotShape2D *, const Transform2D &, _CollectorCallback2D *p_collector, const Vector2 &, const Vector2 &, real_t, real_t);
+typedef void (*CollisionFunc)(const GodotShape2D *, const Transform2D &, const GodotShape2D *, const Transform2D &, _CollectorCallback2D *p_collector, const Vector2 &, const Vector2 &, bool, bool, real_t, real_t);
-template <bool castA, bool castB, bool withMargin>
-static void _collision_segment_segment(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
+template <bool withMargin>
+static void _collision_segment_segment(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, bool p_cast_A, bool p_cast_B, real_t p_margin_A, real_t p_margin_B) {
const GodotSegmentShape2D *segment_A = static_cast<const GodotSegmentShape2D *>(p_a);
const GodotSegmentShape2D *segment_B = static_cast<const GodotSegmentShape2D *>(p_b);
- SeparatorAxisTest2D<GodotSegmentShape2D, GodotSegmentShape2D, castA, castB, withMargin> separator(segment_A, p_transform_a, segment_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
+ SeparatorAxisTest2D<GodotSegmentShape2D, GodotSegmentShape2D, withMargin> separator(segment_A, p_transform_a, segment_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_cast_A, p_cast_B, p_margin_A, p_margin_B);
if (!separator.test_previous_axis()) {
return;
@@ -425,12 +429,12 @@ static void _collision_segment_segment(const GodotShape2D *p_a, const Transform2
separator.generate_contacts();
}
-template <bool castA, bool castB, bool withMargin>
-static void _collision_segment_circle(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
+template <bool withMargin>
+static void _collision_segment_circle(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, bool p_cast_A, bool p_cast_B, real_t p_margin_A, real_t p_margin_B) {
const GodotSegmentShape2D *segment_A = static_cast<const GodotSegmentShape2D *>(p_a);
const GodotCircleShape2D *circle_B = static_cast<const GodotCircleShape2D *>(p_b);
- SeparatorAxisTest2D<GodotSegmentShape2D, GodotCircleShape2D, castA, castB, withMargin> separator(segment_A, p_transform_a, circle_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
+ SeparatorAxisTest2D<GodotSegmentShape2D, GodotCircleShape2D, withMargin> separator(segment_A, p_transform_a, circle_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_cast_A, p_cast_B, p_margin_A, p_margin_B);
if (!separator.test_previous_axis()) {
return;
@@ -458,12 +462,12 @@ static void _collision_segment_circle(const GodotShape2D *p_a, const Transform2D
separator.generate_contacts();
}
-template <bool castA, bool castB, bool withMargin>
-static void _collision_segment_rectangle(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
+template <bool withMargin>
+static void _collision_segment_rectangle(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, bool p_cast_A, bool p_cast_B, real_t p_margin_A, real_t p_margin_B) {
const GodotSegmentShape2D *segment_A = static_cast<const GodotSegmentShape2D *>(p_a);
const GodotRectangleShape2D *rectangle_B = static_cast<const GodotRectangleShape2D *>(p_b);
- SeparatorAxisTest2D<GodotSegmentShape2D, GodotRectangleShape2D, castA, castB, withMargin> separator(segment_A, p_transform_a, rectangle_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
+ SeparatorAxisTest2D<GodotSegmentShape2D, GodotRectangleShape2D, withMargin> separator(segment_A, p_transform_a, rectangle_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_cast_A, p_cast_B, p_margin_A, p_margin_B);
if (!separator.test_previous_axis()) {
return;
@@ -498,7 +502,7 @@ static void _collision_segment_rectangle(const GodotShape2D *p_a, const Transfor
return;
}
- if constexpr (castA) {
+ if (p_cast_A) {
if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, inv, a + p_motion_a))) {
return;
}
@@ -507,7 +511,7 @@ static void _collision_segment_rectangle(const GodotShape2D *p_a, const Transfor
}
}
- if constexpr (castB) {
+ if (p_cast_B) {
if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, inv, a - p_motion_b))) {
return;
}
@@ -516,7 +520,7 @@ static void _collision_segment_rectangle(const GodotShape2D *p_a, const Transfor
}
}
- if constexpr (castA && castB) {
+ if (p_cast_A && p_cast_B) {
if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, inv, a - p_motion_b + p_motion_a))) {
return;
}
@@ -529,12 +533,12 @@ static void _collision_segment_rectangle(const GodotShape2D *p_a, const Transfor
separator.generate_contacts();
}
-template <bool castA, bool castB, bool withMargin>
-static void _collision_segment_capsule(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
+template <bool withMargin>
+static void _collision_segment_capsule(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, bool p_cast_A, bool p_cast_B, real_t p_margin_A, real_t p_margin_B) {
const GodotSegmentShape2D *segment_A = static_cast<const GodotSegmentShape2D *>(p_a);
const GodotCapsuleShape2D *capsule_B = static_cast<const GodotCapsuleShape2D *>(p_b);
- SeparatorAxisTest2D<GodotSegmentShape2D, GodotCapsuleShape2D, castA, castB, withMargin> separator(segment_A, p_transform_a, capsule_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
+ SeparatorAxisTest2D<GodotSegmentShape2D, GodotCapsuleShape2D, withMargin> separator(segment_A, p_transform_a, capsule_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_cast_A, p_cast_B, p_margin_A, p_margin_B);
if (!separator.test_previous_axis()) {
return;
@@ -570,12 +574,12 @@ static void _collision_segment_capsule(const GodotShape2D *p_a, const Transform2
separator.generate_contacts();
}
-template <bool castA, bool castB, bool withMargin>
-static void _collision_segment_convex_polygon(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
+template <bool withMargin>
+static void _collision_segment_convex_polygon(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, bool p_cast_A, bool p_cast_B, real_t p_margin_A, real_t p_margin_B) {
const GodotSegmentShape2D *segment_A = static_cast<const GodotSegmentShape2D *>(p_a);
const GodotConvexPolygonShape2D *convex_B = static_cast<const GodotConvexPolygonShape2D *>(p_b);
- SeparatorAxisTest2D<GodotSegmentShape2D, GodotConvexPolygonShape2D, castA, castB, withMargin> separator(segment_A, p_transform_a, convex_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
+ SeparatorAxisTest2D<GodotSegmentShape2D, GodotConvexPolygonShape2D, withMargin> separator(segment_A, p_transform_a, convex_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_cast_A, p_cast_B, p_margin_A, p_margin_B);
if (!separator.test_previous_axis()) {
return;
@@ -609,12 +613,12 @@ static void _collision_segment_convex_polygon(const GodotShape2D *p_a, const Tra
/////////
-template <bool castA, bool castB, bool withMargin>
-static void _collision_circle_circle(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
+template <bool withMargin>
+static void _collision_circle_circle(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, bool p_cast_A, bool p_cast_B, real_t p_margin_A, real_t p_margin_B) {
const GodotCircleShape2D *circle_A = static_cast<const GodotCircleShape2D *>(p_a);
const GodotCircleShape2D *circle_B = static_cast<const GodotCircleShape2D *>(p_b);
- SeparatorAxisTest2D<GodotCircleShape2D, GodotCircleShape2D, castA, castB, withMargin> separator(circle_A, p_transform_a, circle_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
+ SeparatorAxisTest2D<GodotCircleShape2D, GodotCircleShape2D, withMargin> separator(circle_A, p_transform_a, circle_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_cast_A, p_cast_B, p_margin_A, p_margin_B);
if (!separator.test_previous_axis()) {
return;
@@ -631,12 +635,12 @@ static void _collision_circle_circle(const GodotShape2D *p_a, const Transform2D
separator.generate_contacts();
}
-template <bool castA, bool castB, bool withMargin>
-static void _collision_circle_rectangle(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
+template <bool withMargin>
+static void _collision_circle_rectangle(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, bool p_cast_A, bool p_cast_B, real_t p_margin_A, real_t p_margin_B) {
const GodotCircleShape2D *circle_A = static_cast<const GodotCircleShape2D *>(p_a);
const GodotRectangleShape2D *rectangle_B = static_cast<const GodotRectangleShape2D *>(p_b);
- SeparatorAxisTest2D<GodotCircleShape2D, GodotRectangleShape2D, castA, castB, withMargin> separator(circle_A, p_transform_a, rectangle_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
+ SeparatorAxisTest2D<GodotCircleShape2D, GodotRectangleShape2D, withMargin> separator(circle_A, p_transform_a, rectangle_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_cast_A, p_cast_B, p_margin_A, p_margin_B);
if (!separator.test_previous_axis()) {
return;
@@ -665,21 +669,21 @@ static void _collision_circle_rectangle(const GodotShape2D *p_a, const Transform
}
}
- if constexpr (castA) {
+ if (p_cast_A) {
Vector2 sphereofs = sphere + p_motion_a;
if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, binv, sphereofs))) {
return;
}
}
- if constexpr (castB) {
+ if (p_cast_B) {
Vector2 sphereofs = sphere - p_motion_b;
if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, binv, sphereofs))) {
return;
}
}
- if constexpr (castA && castB) {
+ if (p_cast_A && p_cast_B) {
Vector2 sphereofs = sphere - p_motion_b + p_motion_a;
if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, binv, sphereofs))) {
return;
@@ -689,12 +693,12 @@ static void _collision_circle_rectangle(const GodotShape2D *p_a, const Transform
separator.generate_contacts();
}
-template <bool castA, bool castB, bool withMargin>
-static void _collision_circle_capsule(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
+template <bool withMargin>
+static void _collision_circle_capsule(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, bool p_cast_A, bool p_cast_B, real_t p_margin_A, real_t p_margin_B) {
const GodotCircleShape2D *circle_A = static_cast<const GodotCircleShape2D *>(p_a);
const GodotCapsuleShape2D *capsule_B = static_cast<const GodotCapsuleShape2D *>(p_b);
- SeparatorAxisTest2D<GodotCircleShape2D, GodotCapsuleShape2D, castA, castB, withMargin> separator(circle_A, p_transform_a, capsule_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
+ SeparatorAxisTest2D<GodotCircleShape2D, GodotCapsuleShape2D, withMargin> separator(circle_A, p_transform_a, capsule_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_cast_A, p_cast_B, p_margin_A, p_margin_B);
if (!separator.test_previous_axis()) {
return;
@@ -722,12 +726,12 @@ static void _collision_circle_capsule(const GodotShape2D *p_a, const Transform2D
separator.generate_contacts();
}
-template <bool castA, bool castB, bool withMargin>
-static void _collision_circle_convex_polygon(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
+template <bool withMargin>
+static void _collision_circle_convex_polygon(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, bool p_cast_A, bool p_cast_B, real_t p_margin_A, real_t p_margin_B) {
const GodotCircleShape2D *circle_A = static_cast<const GodotCircleShape2D *>(p_a);
const GodotConvexPolygonShape2D *convex_B = static_cast<const GodotConvexPolygonShape2D *>(p_b);
- SeparatorAxisTest2D<GodotCircleShape2D, GodotConvexPolygonShape2D, castA, castB, withMargin> separator(circle_A, p_transform_a, convex_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
+ SeparatorAxisTest2D<GodotCircleShape2D, GodotConvexPolygonShape2D, withMargin> separator(circle_A, p_transform_a, convex_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_cast_A, p_cast_B, p_margin_A, p_margin_B);
if (!separator.test_previous_axis()) {
return;
@@ -753,12 +757,12 @@ static void _collision_circle_convex_polygon(const GodotShape2D *p_a, const Tran
/////////
-template <bool castA, bool castB, bool withMargin>
-static void _collision_rectangle_rectangle(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
+template <bool withMargin>
+static void _collision_rectangle_rectangle(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, bool p_cast_A, bool p_cast_B, real_t p_margin_A, real_t p_margin_B) {
const GodotRectangleShape2D *rectangle_A = static_cast<const GodotRectangleShape2D *>(p_a);
const GodotRectangleShape2D *rectangle_B = static_cast<const GodotRectangleShape2D *>(p_b);
- SeparatorAxisTest2D<GodotRectangleShape2D, GodotRectangleShape2D, castA, castB, withMargin> separator(rectangle_A, p_transform_a, rectangle_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
+ SeparatorAxisTest2D<GodotRectangleShape2D, GodotRectangleShape2D, withMargin> separator(rectangle_A, p_transform_a, rectangle_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_cast_A, p_cast_B, p_margin_A, p_margin_B);
if (!separator.test_previous_axis()) {
return;
@@ -794,7 +798,7 @@ static void _collision_rectangle_rectangle(const GodotShape2D *p_a, const Transf
return;
}
- if constexpr (castA || castB) {
+ if (p_cast_A || p_cast_B) {
Transform2D aofs = p_transform_a;
aofs.columns[2] += p_motion_a;
@@ -804,19 +808,19 @@ static void _collision_rectangle_rectangle(const GodotShape2D *p_a, const Transf
[[maybe_unused]] Transform2D aofsinv = aofs.affine_inverse();
[[maybe_unused]] Transform2D bofsinv = bofs.affine_inverse();
- if constexpr (castA) {
+ if (p_cast_A) {
if (!separator.test_axis(rectangle_A->get_box_axis(aofs, aofsinv, rectangle_B, p_transform_b, invB))) {
return;
}
}
- if constexpr (castB) {
+ if (p_cast_B) {
if (!separator.test_axis(rectangle_A->get_box_axis(p_transform_a, invA, rectangle_B, bofs, bofsinv))) {
return;
}
}
- if constexpr (castA && castB) {
+ if (p_cast_A && p_cast_B) {
if (!separator.test_axis(rectangle_A->get_box_axis(aofs, aofsinv, rectangle_B, bofs, bofsinv))) {
return;
}
@@ -827,12 +831,12 @@ static void _collision_rectangle_rectangle(const GodotShape2D *p_a, const Transf
separator.generate_contacts();
}
-template <bool castA, bool castB, bool withMargin>
-static void _collision_rectangle_capsule(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
+template <bool withMargin>
+static void _collision_rectangle_capsule(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, bool p_cast_A, bool p_cast_B, real_t p_margin_A, real_t p_margin_B) {
const GodotRectangleShape2D *rectangle_A = static_cast<const GodotRectangleShape2D *>(p_a);
const GodotCapsuleShape2D *capsule_B = static_cast<const GodotCapsuleShape2D *>(p_b);
- SeparatorAxisTest2D<GodotRectangleShape2D, GodotCapsuleShape2D, castA, castB, withMargin> separator(rectangle_A, p_transform_a, capsule_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
+ SeparatorAxisTest2D<GodotRectangleShape2D, GodotCapsuleShape2D, withMargin> separator(rectangle_A, p_transform_a, capsule_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_cast_A, p_cast_B, p_margin_A, p_margin_B);
if (!separator.test_previous_axis()) {
return;
@@ -871,7 +875,7 @@ static void _collision_rectangle_capsule(const GodotShape2D *p_a, const Transfor
}
}
- if constexpr (castA) {
+ if (p_cast_A) {
Vector2 capsule_endpoint = p_transform_b.get_origin() + p_transform_b.columns[1] * capsule_dir;
capsule_endpoint -= p_motion_a;
@@ -880,7 +884,7 @@ static void _collision_rectangle_capsule(const GodotShape2D *p_a, const Transfor
}
}
- if constexpr (castB) {
+ if (p_cast_B) {
Vector2 capsule_endpoint = p_transform_b.get_origin() + p_transform_b.columns[1] * capsule_dir;
capsule_endpoint += p_motion_b;
@@ -889,7 +893,7 @@ static void _collision_rectangle_capsule(const GodotShape2D *p_a, const Transfor
}
}
- if constexpr (castA && castB) {
+ if (p_cast_A && p_cast_B) {
Vector2 capsule_endpoint = p_transform_b.get_origin() + p_transform_b.columns[1] * capsule_dir;
capsule_endpoint -= p_motion_a;
capsule_endpoint += p_motion_b;
@@ -905,12 +909,12 @@ static void _collision_rectangle_capsule(const GodotShape2D *p_a, const Transfor
separator.generate_contacts();
}
-template <bool castA, bool castB, bool withMargin>
-static void _collision_rectangle_convex_polygon(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
+template <bool withMargin>
+static void _collision_rectangle_convex_polygon(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, bool p_cast_A, bool p_cast_B, real_t p_margin_A, real_t p_margin_B) {
const GodotRectangleShape2D *rectangle_A = static_cast<const GodotRectangleShape2D *>(p_a);
const GodotConvexPolygonShape2D *convex_B = static_cast<const GodotConvexPolygonShape2D *>(p_b);
- SeparatorAxisTest2D<GodotRectangleShape2D, GodotConvexPolygonShape2D, castA, castB, withMargin> separator(rectangle_A, p_transform_a, convex_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
+ SeparatorAxisTest2D<GodotRectangleShape2D, GodotConvexPolygonShape2D, withMargin> separator(rectangle_A, p_transform_a, convex_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_cast_A, p_cast_B, p_margin_A, p_margin_B);
if (!separator.test_previous_axis()) {
return;
@@ -944,17 +948,17 @@ static void _collision_rectangle_convex_polygon(const GodotShape2D *p_a, const T
if (!separator.test_axis(rectangle_A->get_circle_axis(p_transform_a, boxinv, p_transform_b.xform(convex_B->get_point(i))))) {
return;
}
- if constexpr (castA) {
+ if (p_cast_A) {
if (!separator.test_axis(rectangle_A->get_circle_axis(p_transform_a, boxinv, p_transform_b.xform(convex_B->get_point(i)) - p_motion_a))) {
return;
}
}
- if constexpr (castB) {
+ if (p_cast_B) {
if (!separator.test_axis(rectangle_A->get_circle_axis(p_transform_a, boxinv, p_transform_b.xform(convex_B->get_point(i)) + p_motion_b))) {
return;
}
}
- if constexpr (castA && castB) {
+ if (p_cast_A && p_cast_B) {
if (!separator.test_axis(rectangle_A->get_circle_axis(p_transform_a, boxinv, p_transform_b.xform(convex_B->get_point(i)) + p_motion_b - p_motion_a))) {
return;
}
@@ -967,12 +971,12 @@ static void _collision_rectangle_convex_polygon(const GodotShape2D *p_a, const T
/////////
-template <bool castA, bool castB, bool withMargin>
-static void _collision_capsule_capsule(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
+template <bool withMargin>
+static void _collision_capsule_capsule(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, bool p_cast_A, bool p_cast_B, real_t p_margin_A, real_t p_margin_B) {
const GodotCapsuleShape2D *capsule_A = static_cast<const GodotCapsuleShape2D *>(p_a);
const GodotCapsuleShape2D *capsule_B = static_cast<const GodotCapsuleShape2D *>(p_b);
- SeparatorAxisTest2D<GodotCapsuleShape2D, GodotCapsuleShape2D, castA, castB, withMargin> separator(capsule_A, p_transform_a, capsule_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
+ SeparatorAxisTest2D<GodotCapsuleShape2D, GodotCapsuleShape2D, withMargin> separator(capsule_A, p_transform_a, capsule_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_cast_A, p_cast_B, p_margin_A, p_margin_B);
if (!separator.test_previous_axis()) {
return;
@@ -1015,12 +1019,12 @@ static void _collision_capsule_capsule(const GodotShape2D *p_a, const Transform2
separator.generate_contacts();
}
-template <bool castA, bool castB, bool withMargin>
-static void _collision_capsule_convex_polygon(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
+template <bool withMargin>
+static void _collision_capsule_convex_polygon(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, bool p_cast_A, bool p_cast_B, real_t p_margin_A, real_t p_margin_B) {
const GodotCapsuleShape2D *capsule_A = static_cast<const GodotCapsuleShape2D *>(p_a);
const GodotConvexPolygonShape2D *convex_B = static_cast<const GodotConvexPolygonShape2D *>(p_b);
- SeparatorAxisTest2D<GodotCapsuleShape2D, GodotConvexPolygonShape2D, castA, castB, withMargin> separator(capsule_A, p_transform_a, convex_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
+ SeparatorAxisTest2D<GodotCapsuleShape2D, GodotConvexPolygonShape2D, withMargin> separator(capsule_A, p_transform_a, convex_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_cast_A, p_cast_B, p_margin_A, p_margin_B);
if (!separator.test_previous_axis()) {
return;
@@ -1061,12 +1065,12 @@ static void _collision_capsule_convex_polygon(const GodotShape2D *p_a, const Tra
/////////
-template <bool castA, bool castB, bool withMargin>
-static void _collision_convex_polygon_convex_polygon(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
+template <bool withMargin>
+static void _collision_convex_polygon_convex_polygon(const GodotShape2D *p_a, const Transform2D &p_transform_a, const GodotShape2D *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, bool p_cast_A, bool p_cast_B, real_t p_margin_A, real_t p_margin_B) {
const GodotConvexPolygonShape2D *convex_A = static_cast<const GodotConvexPolygonShape2D *>(p_a);
const GodotConvexPolygonShape2D *convex_B = static_cast<const GodotConvexPolygonShape2D *>(p_b);
- SeparatorAxisTest2D<GodotConvexPolygonShape2D, GodotConvexPolygonShape2D, castA, castB, withMargin> separator(convex_A, p_transform_a, convex_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
+ SeparatorAxisTest2D<GodotConvexPolygonShape2D, GodotConvexPolygonShape2D, withMargin> separator(convex_A, p_transform_a, convex_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_cast_A, p_cast_B, p_margin_A, p_margin_B);
if (!separator.test_previous_axis()) {
return;
@@ -1117,234 +1121,60 @@ bool sat_2d_calculate_penetration(const GodotShape2D *p_shape_A, const Transform
ERR_FAIL_COND_V(p_shape_B->is_concave(), false);
static const CollisionFunc collision_table[5][5] = {
- { _collision_segment_segment<false, false, false>,
- _collision_segment_circle<false, false, false>,
- _collision_segment_rectangle<false, false, false>,
- _collision_segment_capsule<false, false, false>,
- _collision_segment_convex_polygon<false, false, false> },
+ { _collision_segment_segment<false>,
+ _collision_segment_circle<false>,
+ _collision_segment_rectangle<false>,
+ _collision_segment_capsule<false>,
+ _collision_segment_convex_polygon<false> },
{ nullptr,
- _collision_circle_circle<false, false, false>,
- _collision_circle_rectangle<false, false, false>,
- _collision_circle_capsule<false, false, false>,
- _collision_circle_convex_polygon<false, false, false> },
+ _collision_circle_circle<false>,
+ _collision_circle_rectangle<false>,
+ _collision_circle_capsule<false>,
+ _collision_circle_convex_polygon<false> },
{ nullptr,
nullptr,
- _collision_rectangle_rectangle<false, false, false>,
- _collision_rectangle_capsule<false, false, false>,
- _collision_rectangle_convex_polygon<false, false, false> },
+ _collision_rectangle_rectangle<false>,
+ _collision_rectangle_capsule<false>,
+ _collision_rectangle_convex_polygon<false> },
{ nullptr,
nullptr,
nullptr,
- _collision_capsule_capsule<false, false, false>,
- _collision_capsule_convex_polygon<false, false, false> },
+ _collision_capsule_capsule<false>,
+ _collision_capsule_convex_polygon<false> },
{ nullptr,
nullptr,
nullptr,
nullptr,
- _collision_convex_polygon_convex_polygon<false, false, false> }
-
- };
-
- static const CollisionFunc collision_table_castA[5][5] = {
- { _collision_segment_segment<true, false, false>,
- _collision_segment_circle<true, false, false>,
- _collision_segment_rectangle<true, false, false>,
- _collision_segment_capsule<true, false, false>,
- _collision_segment_convex_polygon<true, false, false> },
- { nullptr,
- _collision_circle_circle<true, false, false>,
- _collision_circle_rectangle<true, false, false>,
- _collision_circle_capsule<true, false, false>,
- _collision_circle_convex_polygon<true, false, false> },
- { nullptr,
- nullptr,
- _collision_rectangle_rectangle<true, false, false>,
- _collision_rectangle_capsule<true, false, false>,
- _collision_rectangle_convex_polygon<true, false, false> },
- { nullptr,
- nullptr,
- nullptr,
- _collision_capsule_capsule<true, false, false>,
- _collision_capsule_convex_polygon<true, false, false> },
- { nullptr,
- nullptr,
- nullptr,
- nullptr,
- _collision_convex_polygon_convex_polygon<true, false, false> }
-
- };
-
- static const CollisionFunc collision_table_castB[5][5] = {
- { _collision_segment_segment<false, true, false>,
- _collision_segment_circle<false, true, false>,
- _collision_segment_rectangle<false, true, false>,
- _collision_segment_capsule<false, true, false>,
- _collision_segment_convex_polygon<false, true, false> },
- { nullptr,
- _collision_circle_circle<false, true, false>,
- _collision_circle_rectangle<false, true, false>,
- _collision_circle_capsule<false, true, false>,
- _collision_circle_convex_polygon<false, true, false> },
- { nullptr,
- nullptr,
- _collision_rectangle_rectangle<false, true, false>,
- _collision_rectangle_capsule<false, true, false>,
- _collision_rectangle_convex_polygon<false, true, false> },
- { nullptr,
- nullptr,
- nullptr,
- _collision_capsule_capsule<false, true, false>,
- _collision_capsule_convex_polygon<false, true, false> },
- { nullptr,
- nullptr,
- nullptr,
- nullptr,
- _collision_convex_polygon_convex_polygon<false, true, false> }
-
- };
-
- static const CollisionFunc collision_table_castA_castB[5][5] = {
- { _collision_segment_segment<true, true, false>,
- _collision_segment_circle<true, true, false>,
- _collision_segment_rectangle<true, true, false>,
- _collision_segment_capsule<true, true, false>,
- _collision_segment_convex_polygon<true, true, false> },
- { nullptr,
- _collision_circle_circle<true, true, false>,
- _collision_circle_rectangle<true, true, false>,
- _collision_circle_capsule<true, true, false>,
- _collision_circle_convex_polygon<true, true, false> },
- { nullptr,
- nullptr,
- _collision_rectangle_rectangle<true, true, false>,
- _collision_rectangle_capsule<true, true, false>,
- _collision_rectangle_convex_polygon<true, true, false> },
- { nullptr,
- nullptr,
- nullptr,
- _collision_capsule_capsule<true, true, false>,
- _collision_capsule_convex_polygon<true, true, false> },
- { nullptr,
- nullptr,
- nullptr,
- nullptr,
- _collision_convex_polygon_convex_polygon<true, true, false> }
+ _collision_convex_polygon_convex_polygon<false> }
};
static const CollisionFunc collision_table_margin[5][5] = {
- { _collision_segment_segment<false, false, true>,
- _collision_segment_circle<false, false, true>,
- _collision_segment_rectangle<false, false, true>,
- _collision_segment_capsule<false, false, true>,
- _collision_segment_convex_polygon<false, false, true> },
- { nullptr,
- _collision_circle_circle<false, false, true>,
- _collision_circle_rectangle<false, false, true>,
- _collision_circle_capsule<false, false, true>,
- _collision_circle_convex_polygon<false, false, true> },
- { nullptr,
- nullptr,
- _collision_rectangle_rectangle<false, false, true>,
- _collision_rectangle_capsule<false, false, true>,
- _collision_rectangle_convex_polygon<false, false, true> },
- { nullptr,
- nullptr,
- nullptr,
- _collision_capsule_capsule<false, false, true>,
- _collision_capsule_convex_polygon<false, false, true> },
- { nullptr,
- nullptr,
- nullptr,
- nullptr,
- _collision_convex_polygon_convex_polygon<false, false, true> }
-
- };
-
- static const CollisionFunc collision_table_castA_margin[5][5] = {
- { _collision_segment_segment<true, false, true>,
- _collision_segment_circle<true, false, true>,
- _collision_segment_rectangle<true, false, true>,
- _collision_segment_capsule<true, false, true>,
- _collision_segment_convex_polygon<true, false, true> },
+ { _collision_segment_segment<true>,
+ _collision_segment_circle<true>,
+ _collision_segment_rectangle<true>,
+ _collision_segment_capsule<true>,
+ _collision_segment_convex_polygon<true> },
{ nullptr,
- _collision_circle_circle<true, false, true>,
- _collision_circle_rectangle<true, false, true>,
- _collision_circle_capsule<true, false, true>,
- _collision_circle_convex_polygon<true, false, true> },
+ _collision_circle_circle<true>,
+ _collision_circle_rectangle<true>,
+ _collision_circle_capsule<true>,
+ _collision_circle_convex_polygon<true> },
{ nullptr,
nullptr,
- _collision_rectangle_rectangle<true, false, true>,
- _collision_rectangle_capsule<true, false, true>,
- _collision_rectangle_convex_polygon<true, false, true> },
+ _collision_rectangle_rectangle<true>,
+ _collision_rectangle_capsule<true>,
+ _collision_rectangle_convex_polygon<true> },
{ nullptr,
nullptr,
nullptr,
- _collision_capsule_capsule<true, false, true>,
- _collision_capsule_convex_polygon<true, false, true> },
+ _collision_capsule_capsule<true>,
+ _collision_capsule_convex_polygon<true> },
{ nullptr,
nullptr,
nullptr,
nullptr,
- _collision_convex_polygon_convex_polygon<true, false, true> }
-
- };
-
- static const CollisionFunc collision_table_castB_margin[5][5] = {
- { _collision_segment_segment<false, true, true>,
- _collision_segment_circle<false, true, true>,
- _collision_segment_rectangle<false, true, true>,
- _collision_segment_capsule<false, true, true>,
- _collision_segment_convex_polygon<false, true, true> },
- { nullptr,
- _collision_circle_circle<false, true, true>,
- _collision_circle_rectangle<false, true, true>,
- _collision_circle_capsule<false, true, true>,
- _collision_circle_convex_polygon<false, true, true> },
- { nullptr,
- nullptr,
- _collision_rectangle_rectangle<false, true, true>,
- _collision_rectangle_capsule<false, true, true>,
- _collision_rectangle_convex_polygon<false, true, true> },
- { nullptr,
- nullptr,
- nullptr,
- _collision_capsule_capsule<false, true, true>,
- _collision_capsule_convex_polygon<false, true, true> },
- { nullptr,
- nullptr,
- nullptr,
- nullptr,
- _collision_convex_polygon_convex_polygon<false, true, true> }
-
- };
-
- static const CollisionFunc collision_table_castA_castB_margin[5][5] = {
- { _collision_segment_segment<true, true, true>,
- _collision_segment_circle<true, true, true>,
- _collision_segment_rectangle<true, true, true>,
- _collision_segment_capsule<true, true, true>,
- _collision_segment_convex_polygon<true, true, true> },
- { nullptr,
- _collision_circle_circle<true, true, true>,
- _collision_circle_rectangle<true, true, true>,
- _collision_circle_capsule<true, true, true>,
- _collision_circle_convex_polygon<true, true, true> },
- { nullptr,
- nullptr,
- _collision_rectangle_rectangle<true, true, true>,
- _collision_rectangle_capsule<true, true, true>,
- _collision_rectangle_convex_polygon<true, true, true> },
- { nullptr,
- nullptr,
- nullptr,
- _collision_capsule_capsule<true, true, true>,
- _collision_capsule_convex_polygon<true, true, true> },
- { nullptr,
- nullptr,
- nullptr,
- nullptr,
- _collision_convex_polygon_convex_polygon<true, true, true> }
+ _collision_convex_polygon_convex_polygon<true> }
};
@@ -1375,30 +1205,16 @@ bool sat_2d_calculate_penetration(const GodotShape2D *p_shape_A, const Transform
CollisionFunc collision_func;
if (p_margin_A || p_margin_B) {
- if (*motion_A == Vector2() && *motion_B == Vector2()) {
- collision_func = collision_table_margin[type_A - 2][type_B - 2];
- } else if (*motion_A != Vector2() && *motion_B == Vector2()) {
- collision_func = collision_table_castA_margin[type_A - 2][type_B - 2];
- } else if (*motion_A == Vector2() && *motion_B != Vector2()) {
- collision_func = collision_table_castB_margin[type_A - 2][type_B - 2];
- } else {
- collision_func = collision_table_castA_castB_margin[type_A - 2][type_B - 2];
- }
+ collision_func = collision_table_margin[type_A - 2][type_B - 2];
} else {
- if (*motion_A == Vector2() && *motion_B == Vector2()) {
- collision_func = collision_table[type_A - 2][type_B - 2];
- } else if (*motion_A != Vector2() && *motion_B == Vector2()) {
- collision_func = collision_table_castA[type_A - 2][type_B - 2];
- } else if (*motion_A == Vector2() && *motion_B != Vector2()) {
- collision_func = collision_table_castB[type_A - 2][type_B - 2];
- } else {
- collision_func = collision_table_castA_castB[type_A - 2][type_B - 2];
- }
+ collision_func = collision_table[type_A - 2][type_B - 2];
}
ERR_FAIL_NULL_V(collision_func, false);
- collision_func(A, *transform_A, B, *transform_B, &callback, *motion_A, *motion_B, margin_A, margin_B);
+ const bool cast_A = *motion_A != Vector2();
+ const bool cast_B = *motion_B != Vector2();
+ collision_func(A, *transform_A, B, *transform_B, &callback, *motion_A, *motion_B, cast_A, cast_B, margin_A, margin_B);
return callback.collided;
}