SoftBodyStressTest.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2023 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Tests/SoftBody/SoftBodyStressTest.h>
  6. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  7. #include <Jolt/Physics/SoftBody/SoftBodyCreationSettings.h>
  8. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  9. #include <Utils/SoftBodyCreator.h>
  10. #include <Application/DebugUI.h>
  11. #include <Layers.h>
  12. JPH_IMPLEMENT_RTTI_VIRTUAL(SoftBodyStressTest)
  13. {
  14. JPH_ADD_BASE_CLASS(SoftBodyStressTest, Test)
  15. }
  16. const char *SoftBodyStressTest::sScenes[] =
  17. {
  18. "SpheresVsBoxes",
  19. "LargeCloth"
  20. };
  21. const char *SoftBodyStressTest::sSceneName = "SpheresVsBoxes";
  22. void SoftBodyStressTest::Initialize()
  23. {
  24. if (strcmp(sSceneName, "SpheresVsBoxes") == 0)
  25. {
  26. // Floor
  27. CreateMeshTerrain();
  28. // Pressurized sphere settings
  29. SoftBodyCreationSettings sphere(SoftBodyCreator::CreateSphere(), RVec3::sZero(), Quat::sIdentity(), Layers::MOVING);
  30. sphere.mPressure = 2000.0f;
  31. // Box settings
  32. BodyCreationSettings box(new BoxShape(Vec3::sReplicate(1.0f)), RVec3::sZero(), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  33. box.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  34. box.mMassPropertiesOverride.mMass = 100.0f;
  35. for (int x = 0; x <= 10; ++x)
  36. for (int z = 0; z <= 10; ++z)
  37. {
  38. sphere.mPosition = RVec3(-20.0_r + 4.0_r * x, 5.0_r, -20.0_r + 4.0_r * z);
  39. mBodyInterface->CreateAndAddSoftBody(sphere, EActivation::Activate);
  40. box.mPosition = sphere.mPosition + RVec3(0, 4, 0);
  41. mBodyInterface->CreateAndAddBody(box, EActivation::Activate);
  42. }
  43. }
  44. else if (strcmp(sSceneName, "LargeCloth") == 0)
  45. {
  46. // Floor
  47. CreateFloor();
  48. // Create cloth that's fixated at the corners
  49. SoftBodyCreationSettings cloth(SoftBodyCreator::CreateCloth(100, 0.25f), RVec3(0, 15.0f, 0), Quat::sIdentity(), Layers::MOVING);
  50. cloth.mUpdatePosition = false; // Don't update the position of the cloth as it is fixed to the world
  51. mBodyInterface->CreateAndAddSoftBody(cloth, EActivation::Activate);
  52. // Box settings
  53. BodyCreationSettings box(new BoxShape(Vec3::sReplicate(0.5f)), RVec3::sZero(), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  54. box.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  55. box.mMassPropertiesOverride.mMass = 10.0f;
  56. // Create a number of boxes that fall on the cloth
  57. for (int x = 0; x <= 10; ++x)
  58. for (int z = 0; z <= 10; ++z)
  59. {
  60. box.mPosition = cloth.mPosition + RVec3(-10.0_r + 2.0_r * x, 2.0_r, -10.0_r + 2.0_r * z);
  61. mBodyInterface->CreateAndAddBody(box, EActivation::Activate);
  62. }
  63. }
  64. }
  65. void SoftBodyStressTest::CreateSettingsMenu(DebugUI *inUI, UIElement *inSubMenu)
  66. {
  67. inUI->CreateTextButton(inSubMenu, "Select Scene", [this, inUI]() {
  68. UIElement *scene_name = inUI->CreateMenu();
  69. for (uint i = 0; i < size(sScenes); ++i)
  70. inUI->CreateTextButton(scene_name, sScenes[i], [this, i]() { sSceneName = sScenes[i]; RestartTest(); });
  71. inUI->ShowMenu(scene_name);
  72. });
  73. }