From 88d4447b2041235ace36291b833cbd0af4a1624f Mon Sep 17 00:00:00 2001 From: Orange Date: Thu, 13 Nov 2025 17:22:54 +0300 Subject: [PATCH] add another test --- tests/general/unit_test_epa.cpp | 61 ++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/tests/general/unit_test_epa.cpp b/tests/general/unit_test_epa.cpp index f9d9f8d..adc468a 100644 --- a/tests/general/unit_test_epa.cpp +++ b/tests/general/unit_test_epa.cpp @@ -77,4 +77,63 @@ TEST(UnitTestEpa, TestCollisionTrue) Mesh b_wrong = b; b_wrong.set_origin(b_wrong.get_origin() - resolve); EXPECT_TRUE(GJK::is_collide(A, Collider(b_wrong))); -} \ No newline at end of file +} +TEST(UnitTestEpa, TestCollisionTrue2) +{ + std::vector> vbo = {{-1, -1, -1}, {-1, -1, 1}, {-1, 1, -1}, {-1, 1, 1}, + {1, 1, 1}, {1, 1, -1}, {1, -1, 1}, {1, -1, -1}}; + std::vector> vao; // not needed + + Mesh a(vbo, vao, {1, 1, 1}); + Mesh b(vbo, vao, {1, 1, 1}); + + // Overlap along +X by 0.5 + a.set_origin({0, 0, 0}); + b.set_origin({0.5f, 0, 0}); + + Collider A(a), B(b); + + // --- GJK must detect collision and provide simplex --- + auto gjk = GJK::is_collide_with_simplex_info(A, B); + ASSERT_TRUE(gjk.hit) << "GJK should report collision for overlapping cubes"; + + // --- EPA penetration --- + EPA::Params params; + params.max_iterations = 64; + params.tolerance = 1e-4f; + auto epa = EPA::solve(A, B, gjk.simplex, params); + ASSERT_TRUE(epa.success) << "EPA should converge"; + + // Normal is unit-length + EXPECT_NEAR(epa.normal.dot(epa.normal), 1.0f, 1e-5f); + + // For centers at 0 and +0.5 and half-extent 1 -> depth ≈ 1.5 + EXPECT_NEAR(epa.depth, 1.5f, 1e-3f); + + // Axis sanity: mostly X + EXPECT_NEAR(std::abs(epa.normal.x), 1.0f, 1e-3f); + EXPECT_NEAR(epa.normal.y, 0.0f, 1e-3f); + EXPECT_NEAR(epa.normal.z, 0.0f, 1e-3f); + + // Choose a deterministic sign: orient penetration from A toward B + const auto centers = b.get_origin() - a.get_origin(); // (0.5, 0, 0) + float sign = (epa.normal.dot(centers) >= 0.0f) ? +1.0f : -1.0f; + + constexpr float margin = 1.0f + 1e-3f; // tiny slack to avoid grazing + const auto pen = epa.normal * epa.depth * sign; + + // Apply once: B + pen must separate; the opposite must still collide + Mesh b_resolved = b; + b_resolved.set_origin(b_resolved.get_origin() + pen * margin); + EXPECT_FALSE(GJK::is_collide(A, Collider(b_resolved))) << "Applying penetration should separate"; + + Mesh b_wrong = b; + b_wrong.set_origin(b_wrong.get_origin() - pen * margin); + EXPECT_TRUE(GJK::is_collide(A, Collider(b_wrong))) << "Opposite direction should still intersect"; + + // Some book-keeping sanity + EXPECT_GT(epa.iterations, 0); + EXPECT_LT(epa.iterations, params.max_iterations); + EXPECT_GE(epa.num_faces, 4); + EXPECT_GT(epa.num_vertices, 4); +}