SoftBodyBendConstraintTest.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2024 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Tests/SoftBody/SoftBodyBendConstraintTest.h>
  6. #include <Jolt/Physics/SoftBody/SoftBodyCreationSettings.h>
  7. #include <Utils/SoftBodyCreator.h>
  8. #include <Layers.h>
  9. #include <Renderer/DebugRendererImp.h>
  10. JPH_IMPLEMENT_RTTI_VIRTUAL(SoftBodyBendConstraintTest)
  11. {
  12. JPH_ADD_BASE_CLASS(SoftBodyBendConstraintTest, Test)
  13. }
  14. void SoftBodyBendConstraintTest::Initialize()
  15. {
  16. CreateFloor();
  17. default_random_engine random;
  18. uniform_real_distribution<float> random_float(-0.1f, 0.1f);
  19. auto inv_mass = [](uint, uint inZ) { return inZ < 2? 0.0f : 1.0f; };
  20. auto perturbation = [&random, &random_float](uint, uint inZ) { return Vec3(random_float(random), (inZ & 1)? 0.1f : -0.1f, random_float(random)); };
  21. {
  22. random.seed(1234);
  23. // Cloth without bend constraints
  24. Ref<SoftBodySharedSettings> cloth_settings = SoftBodyCreator::CreateCloth(cNumVerticesX, cNumVerticesZ, cVertexSpacing, inv_mass, perturbation, SoftBodySharedSettings::EBendType::None);
  25. SoftBodyCreationSettings cloth(cloth_settings, RVec3(-5.0f, 5.0f, 0), Quat::sIdentity(), Layers::MOVING);
  26. mBodyInterface->CreateAndAddSoftBody(cloth, EActivation::Activate);
  27. }
  28. {
  29. random.seed(1234);
  30. // Cloth with distance bend constraints
  31. Ref<SoftBodySharedSettings> cloth_settings = SoftBodyCreator::CreateCloth(cNumVerticesX, cNumVerticesZ, cVertexSpacing, inv_mass, perturbation, SoftBodySharedSettings::EBendType::Distance);
  32. SoftBodyCreationSettings cloth(cloth_settings, RVec3(0.0f, 5.0f, 0), Quat::sIdentity(), Layers::MOVING);
  33. mBodyInterface->CreateAndAddSoftBody(cloth, EActivation::Activate);
  34. }
  35. {
  36. random.seed(1234);
  37. // Cloth with dihedral bend constraints
  38. Ref<SoftBodySharedSettings> cloth_settings = SoftBodyCreator::CreateCloth(cNumVerticesX, cNumVerticesZ, cVertexSpacing, inv_mass, perturbation, SoftBodySharedSettings::EBendType::Dihedral);
  39. SoftBodyCreationSettings cloth(cloth_settings, RVec3(5.0f, 5.0f, 0), Quat::sIdentity(), Layers::MOVING);
  40. mBodyInterface->CreateAndAddSoftBody(cloth, EActivation::Activate);
  41. }
  42. {
  43. // Create sphere
  44. SoftBodyCreationSettings sphere(SoftBodyCreator::CreateSphere(1.0f, 10, 20, SoftBodySharedSettings::EBendType::None), RVec3(-5.0f, 5.0f, 10.0f), Quat::sIdentity(), Layers::MOVING);
  45. mBodyInterface->CreateAndAddSoftBody(sphere, EActivation::Activate);
  46. }
  47. {
  48. // Create sphere with distance bend constraints
  49. SoftBodyCreationSettings sphere(SoftBodyCreator::CreateSphere(1.0f, 10, 20, SoftBodySharedSettings::EBendType::Distance), RVec3(0.0f, 5.0f, 10.0f), Quat::sIdentity(), Layers::MOVING);
  50. mBodyInterface->CreateAndAddSoftBody(sphere, EActivation::Activate);
  51. }
  52. {
  53. // Create sphere with dihedral bend constraints
  54. SoftBodyCreationSettings sphere(SoftBodyCreator::CreateSphere(1.0f, 10, 20, SoftBodySharedSettings::EBendType::Dihedral), RVec3(5.0f, 5.0f, 10.0f), Quat::sIdentity(), Layers::MOVING);
  55. mBodyInterface->CreateAndAddSoftBody(sphere, EActivation::Activate);
  56. }
  57. }
  58. void SoftBodyBendConstraintTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  59. {
  60. mDebugRenderer->DrawText3D(RVec3(-5.0f, 7.5f, 0), "No bend constraints", Color::sWhite);
  61. mDebugRenderer->DrawText3D(RVec3(0.0f, 7.5f, 0), "Distance bend constraints", Color::sWhite);
  62. mDebugRenderer->DrawText3D(RVec3(5.0f, 7.5f, 0), "Dihedral angle bend constraints", Color::sWhite);
  63. }