123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
- // SPDX-License-Identifier: MIT
- #include <TestFramework.h>
- #include <Tests/ConvexCollision/InteractivePairsTest.h>
- #include <Input/Keyboard.h>
- #include <Jolt/Geometry/Sphere.h>
- #include <Jolt/Geometry/AABox.h>
- #include <Jolt/Geometry/ConvexSupport.h>
- #include <Jolt/Geometry/EPAPenetrationDepth.h>
- #include <Renderer/DebugRendererImp.h>
- JPH_IMPLEMENT_RTTI_VIRTUAL(InteractivePairsTest)
- {
- JPH_ADD_BASE_CLASS(InteractivePairsTest, Test)
- }
- void InteractivePairsTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
- {
- // Keyboard controls
- if (inParams.mKeyboard->IsKeyPressed(DIK_Z))
- {
- mKeyboardMode = true;
- mDistance -= inParams.mDeltaTime;
- }
- else if (inParams.mKeyboard->IsKeyPressed(DIK_C))
- {
- mKeyboardMode = true;
- mDistance += inParams.mDeltaTime;
- }
- else if (inParams.mKeyboard->IsKeyPressed(DIK_X))
- {
- mKeyboardMode = false;
- }
- // Auto update
- if (!mKeyboardMode)
- mDistance -= inParams.mDeltaTime;
- // Clamp distance
- if (mDistance < -4.0f)
- mDistance = 4.0f;
- if (mDistance > 4.0f)
- mDistance = -4.0f;
- float z = 0.0f;
- const float r1 = 0.25f * JPH_PI;
- const float r2 = ATan(1.0f / sqrt(2.0f)); // When rotating cube by 45 degrees the one axis becomes sqrt(2) long while the other stays at length 1
- for (int i = 0; i < 2; ++i)
- {
- const float cvx_radius = i == 0? 0.0f : 0.1f; // First round without convex radius, second with
- const float edge_len = 1.0f - cvx_radius;
- AABox b(Vec3(-edge_len, -edge_len, -edge_len), Vec3(edge_len, edge_len, edge_len));
- // Face vs face
- TestBoxVsBox(Vec3(0, 0, z), Vec3(0, 0, 0), cvx_radius, b, Vec3(mDistance, 0, z), Vec3(0, 0, 0), cvx_radius, b);
- z += 4;
- TestBoxVsBox(Vec3(0, 0, z), Vec3(0, 0, 0), cvx_radius, b, Vec3(mDistance, 0, z), Vec3(r1, 0, 0), cvx_radius, b);
- z += 4;
- // Face vs edge
- TestBoxVsBox(Vec3(0, 0, z), Vec3(0, 0, 0), cvx_radius, b, Vec3(mDistance, 0, z), Vec3(0, r1, 0), cvx_radius, b);
- z += 4;
- TestBoxVsBox(Vec3(0, 0, z), Vec3(0, 0, 0), cvx_radius, b, Vec3(mDistance, 0, z), Vec3(0, 0, r1), cvx_radius, b);
- z += 4;
- // Face vs vertex
- TestBoxVsBox(Vec3(0, 0, z), Vec3(0, 0, 0), cvx_radius, b, Vec3(mDistance, 0, z), Vec3(0, r2, r1), cvx_radius, b);
- z += 4;
- // Edge vs edge
- TestBoxVsBox(Vec3(0, 0, z), Vec3(0, r1, 0), cvx_radius, b, Vec3(mDistance, 0, z), Vec3(0, r1, 0), cvx_radius, b);
- z += 4;
- TestBoxVsBox(Vec3(0, 0, z), Vec3(0, 0, r1), cvx_radius, b, Vec3(mDistance, 0, z), Vec3(0, r1, 0), cvx_radius, b);
- z += 4;
- // Edge vs vertex
- TestBoxVsBox(Vec3(0, 0, z), Vec3(0, r2, r1), cvx_radius, b, Vec3(mDistance, 0, z), Vec3(0, r2, r1), cvx_radius, b);
- z += 4;
- // Sphere vs face
- TestSphereVsBox(Vec3(0, 0, z), 1.0f, Vec3(mDistance, 0, z), Vec3(0, 0, 0), cvx_radius, b);
- z += 4;
- TestSphereVsBox(Vec3(0, 0, z), 1.0f, Vec3(mDistance, 0, z), Vec3(r1, 0, 0), cvx_radius, b);
- z += 4;
-
- // Sphere vs edge
- TestSphereVsBox(Vec3(0, 0, z), 1.0f, Vec3(mDistance, 0, z), Vec3(0, r1, 0), cvx_radius, b);
- z += 4;
- TestSphereVsBox(Vec3(0, 0, z), 1.0f, Vec3(mDistance, 0, z), Vec3(0, 0, r1), cvx_radius, b);
- z += 4;
-
- // Sphere vs vertex
- TestSphereVsBox(Vec3(0, 0, z), 1.0f, Vec3(mDistance, 0, z), Vec3(0, r2, r1), cvx_radius, b);
- z += 4;
- // Sphere vs sphere
- TestSphereVsSphere(Vec3(0, 0, z), 1.0f, Vec3(mDistance, 0, z), 1.0f, i == 1);
- z += 4;
- }
- }
- void InteractivePairsTest::TestBoxVsBox(Vec3Arg inTranslationA, Vec3Arg inRotationA, float inConvexRadiusA, const AABox &inA, Vec3Arg inTranslationB, Vec3Arg inRotationB, float inConvexRadiusB, const AABox &inB)
- {
- Mat44 mat_a = Mat44::sTranslation(inTranslationA) * Mat44::sRotationX(inRotationA.GetX()) * Mat44::sRotationY(inRotationA.GetY()) * Mat44::sRotationZ(inRotationA.GetZ());
- TransformedConvexObject<AABox> a(mat_a, inA);
- Mat44 mat_b = Mat44::sTranslation(inTranslationB) * Mat44::sRotationX(inRotationB.GetX()) * Mat44::sRotationY(inRotationB.GetY()) * Mat44::sRotationZ(inRotationB.GetZ());
- TransformedConvexObject<AABox> b(mat_b, inB);
- EPAPenetrationDepth pen_depth;
- Vec3 v = Vec3::sAxisX(), pa, pb;
- mDebugRenderer->DrawBox(mat_a, inA, Color::sWhite);
- AABox widened_a = inA;
- widened_a.ExpandBy(Vec3::sReplicate(inConvexRadiusA));
- AABox widened_b = inB;
- widened_b.ExpandBy(Vec3::sReplicate(inConvexRadiusB));
- mDebugRenderer->DrawBox(mat_a, inA, Color::sWhite);
- if (inConvexRadiusA > 0.0f)
- mDebugRenderer->DrawWireBox(mat_a, widened_a, Color::sWhite);
- AddConvexRadius<TransformedConvexObject<AABox>> a_inc(a, inConvexRadiusA);
- AddConvexRadius<TransformedConvexObject<AABox>> b_inc(b, inConvexRadiusB);
- if (pen_depth.GetPenetrationDepth(a, a_inc, inConvexRadiusA, b, b_inc, inConvexRadiusB, 1.0e-4f, FLT_EPSILON, v, pa, pb))
- {
- mDebugRenderer->DrawBox(mat_b, inB, Color::sRed);
- if (inConvexRadiusB > 0.0f)
- mDebugRenderer->DrawWireBox(mat_b, widened_b, Color::sRed);
- mDebugRenderer->DrawMarker(pa, Color::sYellow, 2.0f);
- mDebugRenderer->DrawMarker(pb, Color::sCyan, 2.0f);
- }
- else
- {
- mDebugRenderer->DrawBox(mat_b, inB, Color::sGreen);
- if (inConvexRadiusB > 0.0f)
- mDebugRenderer->DrawWireBox(mat_b, widened_b, Color::sGreen);
- }
- mDebugRenderer->DrawArrow(inTranslationB + Vec3(0, 2, 0), inTranslationB + v + Vec3(0, 2, 0), Color::sOrange, 0.05f);
- }
- void InteractivePairsTest::TestSphereVsBox(Vec3Arg inTranslationA, float inRadiusA, Vec3Arg inTranslationB, Vec3Arg inRotationB, float inConvexRadiusB, const AABox &inB)
- {
- Sphere s(inTranslationA, inRadiusA);
- Mat44 mat_b = Mat44::sTranslation(inTranslationB) * Mat44::sRotationX(inRotationB.GetX()) * Mat44::sRotationY(inRotationB.GetY()) * Mat44::sRotationZ(inRotationB.GetZ());
- TransformedConvexObject<AABox> b(mat_b, inB);
- AABox widened_b = inB;
- widened_b.ExpandBy(Vec3::sReplicate(inConvexRadiusB));
- EPAPenetrationDepth pen_depth;
- Vec3 v = Vec3::sAxisX(), pa, pb;
- mDebugRenderer->DrawSphere(inTranslationA, inRadiusA, Color::sWhite);
- AddConvexRadius<TransformedConvexObject<AABox>> b_inc(b, inConvexRadiusB);
- if (pen_depth.GetPenetrationDepth(s, s, 0.0f, b, b_inc, inConvexRadiusB, 1.0e-4f, FLT_EPSILON, v, pa, pb))
- {
- mDebugRenderer->DrawBox(mat_b, inB, Color::sRed);
- if (inConvexRadiusB > 0.0f)
- mDebugRenderer->DrawWireBox(mat_b, widened_b, Color::sRed);
- mDebugRenderer->DrawMarker(pa, Color::sYellow, 2.0f);
- mDebugRenderer->DrawMarker(pb, Color::sCyan, 2.0f);
- }
- else
- {
- mDebugRenderer->DrawBox(mat_b, inB, Color::sGreen);
- if (inConvexRadiusB > 0.0f)
- mDebugRenderer->DrawWireBox(mat_b, widened_b, Color::sGreen);
- }
- mDebugRenderer->DrawArrow(inTranslationB + Vec3(0, 2, 0), inTranslationB + v + Vec3(0, 2, 0), Color::sOrange, 0.05f);
- }
- void InteractivePairsTest::TestSphereVsSphere(Vec3Arg inTranslationA, float inRadiusA, Vec3Arg inTranslationB, float inRadiusB, bool inTreatSphereAsPointWithConvexRadius)
- {
- Sphere s1(inTranslationA, inRadiusA);
- Sphere s2(inTranslationB, inRadiusB);
- if (inTreatSphereAsPointWithConvexRadius)
- mDebugRenderer->DrawWireSphere(s1.GetCenter(), s1.GetRadius(), Color::sWhite);
- else
- mDebugRenderer->DrawSphere(s1.GetCenter(), s1.GetRadius(), Color::sWhite);
- bool intersects;
- EPAPenetrationDepth pen_depth;
- Vec3 v = Vec3::sAxisX(), pa, pb;
- if (inTreatSphereAsPointWithConvexRadius)
- intersects = pen_depth.GetPenetrationDepth(PointConvexSupport { inTranslationA }, s1, inRadiusA, PointConvexSupport { inTranslationB }, s2, inRadiusB, 1.0e-4f, FLT_EPSILON, v, pa, pb);
- else
- intersects = pen_depth.GetPenetrationDepth(s1, s1, 0.0f, s2, s2, 0.0f, 1.0e-4f, FLT_EPSILON, v, pa, pb);
- if (intersects)
- {
- if (inTreatSphereAsPointWithConvexRadius)
- mDebugRenderer->DrawWireSphere(s2.GetCenter(), s2.GetRadius(), Color::sRed);
- else
- mDebugRenderer->DrawSphere(s2.GetCenter(), s2.GetRadius(), Color::sRed);
- mDebugRenderer->DrawMarker(pa, Color::sYellow, 2.0f);
- mDebugRenderer->DrawMarker(pb, Color::sCyan, 2.0f);
- }
- else
- {
- if (inTreatSphereAsPointWithConvexRadius)
- mDebugRenderer->DrawWireSphere(s2.GetCenter(), s2.GetRadius(), Color::sGreen);
- else
- mDebugRenderer->DrawSphere(s2.GetCenter(), s2.GetRadius(), Color::sGreen);
- }
- mDebugRenderer->DrawArrow(inTranslationB + Vec3(0, 2, 0), inTranslationB + v + Vec3(0, 2, 0), Color::sOrange, 0.05f);
- }
|