فهرست منبع

Added soft body stress test (#715)

Jorrit Rouwe 2 سال پیش
والد
کامیت
bc2e05eb1c
4فایلهای تغییر یافته به همراه119 افزوده شده و 0 حذف شده
  1. 2 0
      Samples/Samples.cmake
  2. 2 0
      Samples/SamplesApp.cpp
  3. 87 0
      Samples/Tests/SoftBody/SoftBodyStressTest.cpp
  4. 28 0
      Samples/Tests/SoftBody/SoftBodyStressTest.h

+ 2 - 0
Samples/Samples.cmake

@@ -175,6 +175,8 @@ set(SAMPLES_SRC_FILES
 	${SAMPLES_ROOT}/Tests/SoftBody/SoftBodyRestitutionTest.h
 	${SAMPLES_ROOT}/Tests/SoftBody/SoftBodyShapesTest.cpp
 	${SAMPLES_ROOT}/Tests/SoftBody/SoftBodyShapesTest.h
+	${SAMPLES_ROOT}/Tests/SoftBody/SoftBodyStressTest.cpp
+	${SAMPLES_ROOT}/Tests/SoftBody/SoftBodyStressTest.h
 	${SAMPLES_ROOT}/Tests/SoftBody/SoftBodyUpdatePositionTest.cpp
 	${SAMPLES_ROOT}/Tests/SoftBody/SoftBodyUpdatePositionTest.h
 	${SAMPLES_ROOT}/Tests/Test.cpp

+ 2 - 0
Samples/SamplesApp.cpp

@@ -308,6 +308,7 @@ JPH_DECLARE_RTTI_FOR_FACTORY(JPH_NO_EXPORT, SoftBodyPressureTest)
 JPH_DECLARE_RTTI_FOR_FACTORY(JPH_NO_EXPORT, SoftBodyGravityFactorTest)
 JPH_DECLARE_RTTI_FOR_FACTORY(JPH_NO_EXPORT, SoftBodyKinematicTest)
 JPH_DECLARE_RTTI_FOR_FACTORY(JPH_NO_EXPORT, SoftBodyUpdatePositionTest)
+JPH_DECLARE_RTTI_FOR_FACTORY(JPH_NO_EXPORT, SoftBodyStressTest)
 
 static TestNameAndRTTI sSoftBodyTests[] =
 {
@@ -318,6 +319,7 @@ static TestNameAndRTTI sSoftBodyTests[] =
 	{ "Soft Body Gravity Factor",		JPH_RTTI(SoftBodyGravityFactorTest) },
 	{ "Soft Body Kinematic",			JPH_RTTI(SoftBodyKinematicTest) },
 	{ "Soft Body Update Position",		JPH_RTTI(SoftBodyUpdatePositionTest) },
+	{ "Soft Body Stress Test",			JPH_RTTI(SoftBodyStressTest) },
 };
 
 JPH_DECLARE_RTTI_FOR_FACTORY(JPH_NO_EXPORT, BroadPhaseCastRayTest)

+ 87 - 0
Samples/Tests/SoftBody/SoftBodyStressTest.cpp

@@ -0,0 +1,87 @@
+// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
+// SPDX-FileCopyrightText: 2023 Jorrit Rouwe
+// SPDX-License-Identifier: MIT
+
+#include <TestFramework.h>
+
+#include <Tests/SoftBody/SoftBodyStressTest.h>
+#include <Jolt/Physics/Body/BodyCreationSettings.h>
+#include <Jolt/Physics/SoftBody/SoftBodyCreationSettings.h>
+#include <Jolt/Physics/Collision/Shape/BoxShape.h>
+#include <Utils/SoftBodyCreator.h>
+#include <Application/DebugUI.h>
+#include <Layers.h>
+
+JPH_IMPLEMENT_RTTI_VIRTUAL(SoftBodyStressTest)
+{
+	JPH_ADD_BASE_CLASS(SoftBodyStressTest, Test)
+}
+
+const char *SoftBodyStressTest::sScenes[] =
+{
+	"SpheresVsBoxes",
+	"LargeCloth"
+};
+
+const char *SoftBodyStressTest::sSceneName = "SpheresVsBoxes";
+
+void SoftBodyStressTest::Initialize()
+{
+	if (strcmp(sSceneName, "SpheresVsBoxes") == 0)
+	{
+		// Floor
+		CreateMeshTerrain();
+
+		// Pressurized sphere settings
+		SoftBodyCreationSettings sphere(SoftBodyCreator::CreateSphere(), RVec3::sZero(), Quat::sIdentity(), Layers::MOVING);
+		sphere.mPressure = 2000.0f;
+
+		// Box settings
+		BodyCreationSettings box(new BoxShape(Vec3::sReplicate(1.0f)), RVec3::sZero(), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
+		box.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
+		box.mMassPropertiesOverride.mMass = 100.0f;
+
+		for (int x = 0; x <= 10; ++x)
+			for (int z = 0; z <= 10; ++z)
+			{
+				sphere.mPosition = RVec3(-20.0_r + 4.0_r * x, 5.0_r, -20.0_r + 4.0_r * z);
+				mBodyInterface->CreateAndAddSoftBody(sphere, EActivation::Activate);
+
+				box.mPosition = sphere.mPosition + RVec3(0, 4, 0);
+				mBodyInterface->CreateAndAddBody(box, EActivation::Activate);
+			}
+	}
+	else if (strcmp(sSceneName, "LargeCloth") == 0)
+	{
+		// Floor
+		CreateFloor();
+
+		// Create cloth that's fixated at the corners
+		SoftBodyCreationSettings cloth(SoftBodyCreator::CreateCloth(100, 0.25f), RVec3(0, 15.0f, 0), Quat::sIdentity(), Layers::MOVING);
+		cloth.mUpdatePosition = false; // Don't update the position of the cloth as it is fixed to the world
+		mBodyInterface->CreateAndAddSoftBody(cloth, EActivation::Activate);
+
+		// Box settings
+		BodyCreationSettings box(new BoxShape(Vec3::sReplicate(0.5f)), RVec3::sZero(), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
+		box.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
+		box.mMassPropertiesOverride.mMass = 10.0f;
+
+		// Create a number of boxes that fall on the cloth
+		for (int x = 0; x <= 10; ++x)
+			for (int z = 0; z <= 10; ++z)
+			{
+				box.mPosition = cloth.mPosition + RVec3(-10.0_r + 2.0_r * x, 2.0_r, -10.0_r + 2.0_r * z);
+				mBodyInterface->CreateAndAddBody(box, EActivation::Activate);
+			}
+	}
+}
+
+void SoftBodyStressTest::CreateSettingsMenu(DebugUI *inUI, UIElement *inSubMenu)
+{
+	inUI->CreateTextButton(inSubMenu, "Select Scene", [this, inUI]() {
+		UIElement *scene_name = inUI->CreateMenu();
+		for (uint i = 0; i < size(sScenes); ++i)
+			inUI->CreateTextButton(scene_name, sScenes[i], [this, i]() { sSceneName = sScenes[i]; RestartTest(); });
+		inUI->ShowMenu(scene_name);
+	});
+}

+ 28 - 0
Samples/Tests/SoftBody/SoftBodyStressTest.h

@@ -0,0 +1,28 @@
+// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
+// SPDX-FileCopyrightText: 2023 Jorrit Rouwe
+// SPDX-License-Identifier: MIT
+
+#pragma once
+
+#include <Tests/Test.h>
+
+// This test stresses the soft body system by creating a large number of soft bodies / a soft body with many vertices
+class SoftBodyStressTest : public Test
+{
+public:
+	JPH_DECLARE_RTTI_VIRTUAL(JPH_NO_EXPORT, SoftBodyStressTest)
+
+	// See: Test
+	virtual void			Initialize() override;
+
+	// Optional settings menu
+	virtual bool			HasSettingsMenu() const override							{ return true; }
+	virtual void			CreateSettingsMenu(DebugUI *inUI, UIElement *inSubMenu) override;
+
+private:
+	// List of possible scene names
+	static const char *		sScenes[];
+
+	// Filename of animation to load for this test
+	static const char *		sSceneName;
+};