SoftBodyShapesTest.cpp 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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/SoftBodyShapesTest.h>
  6. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  7. #include <Jolt/Physics/SoftBody/SoftBodyCreationSettings.h>
  8. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  9. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  10. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  11. #include <Jolt/Physics/Collision/Shape/TaperedCapsuleShape.h>
  12. #include <Jolt/Physics/Collision/Shape/CylinderShape.h>
  13. #include <Jolt/Physics/Collision/Shape/ConvexHullShape.h>
  14. #include <Jolt/Physics/Collision/Shape/StaticCompoundShape.h>
  15. #include <Jolt/Physics/Collision/Shape/RotatedTranslatedShape.h>
  16. #include <Utils/SoftBodyCreator.h>
  17. #include <Renderer/DebugRendererImp.h>
  18. #include <Layers.h>
  19. JPH_IMPLEMENT_RTTI_VIRTUAL(SoftBodyShapesTest)
  20. {
  21. JPH_ADD_BASE_CLASS(SoftBodyShapesTest, Test)
  22. }
  23. void SoftBodyShapesTest::Initialize()
  24. {
  25. const Quat cCubeOrientation = Quat::sRotation(Vec3::sReplicate(sqrt(1.0f / 3.0f)), DegreesToRadians(45.0f));
  26. // Floor
  27. CreateMeshTerrain();
  28. // Create cloth that's fixated at the corners
  29. SoftBodyCreationSettings cloth(SoftBodyCreator::CreateCloth(), RVec3(0, 10.0f, 0), Quat::sRotation(Vec3::sAxisY(), 0.25f * JPH_PI));
  30. cloth.mObjectLayer = Layers::MOVING;
  31. cloth.mUpdatePosition = false; // Don't update the position of the cloth as it is fixed to the world
  32. cloth.mMakeRotationIdentity = false; // Test explicitly checks if soft bodies with a rotation collide with shapes properly
  33. mBodyInterface->CreateAndAddSoftBody(cloth, EActivation::Activate);
  34. // Create cube
  35. SoftBodyCreationSettings cube(SoftBodyCreator::CreateCube(), RVec3(20.0f, 10.0f, 0.0f), cCubeOrientation);
  36. cube.mObjectLayer = Layers::MOVING;
  37. cube.mRestitution = 0.0f;
  38. mBodyInterface->CreateAndAddSoftBody(cube, EActivation::Activate);
  39. // Create pressurized sphere
  40. SoftBodyCreationSettings sphere(SoftBodyCreator::CreateSphere(), RVec3(15.0f, 10.0f, 15.0f));
  41. sphere.mObjectLayer = Layers::MOVING;
  42. sphere.mPressure = 2000.0f;
  43. mBodyInterface->CreateAndAddSoftBody(sphere, EActivation::Activate);
  44. // Sphere below pressurized sphere
  45. RefConst<Shape> sphere_shape = new SphereShape(1.0f);
  46. BodyCreationSettings bcs(sphere_shape, RVec3(15.5f, 7.0f, 15.0f), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  47. bcs.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  48. bcs.mMassPropertiesOverride.mMass = 100.0f;
  49. mBodyInterface->CreateAndAddBody(bcs, EActivation::Activate);
  50. // Various shapes above cloth
  51. ConvexHullShapeSettings tetrahedron({ Vec3(-2, -2, -2), Vec3(0, -2, 2), Vec3(2, -2, -2), Vec3(0, 2, 0) });
  52. tetrahedron.SetEmbedded();
  53. StaticCompoundShapeSettings compound_shape;
  54. compound_shape.SetEmbedded();
  55. Quat rotate_x = Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI);
  56. compound_shape.AddShape(Vec3::sZero(), rotate_x, new CapsuleShape(2, 0.5f));
  57. compound_shape.AddShape(Vec3(0, 0, -2), Quat::sIdentity(), new SphereShape(1));
  58. compound_shape.AddShape(Vec3(0, 0, 2), Quat::sIdentity(), new SphereShape(1));
  59. RefConst<Shape> shapes[] = {
  60. sphere_shape,
  61. new BoxShape(Vec3(0.75f, 1.0f, 1.25f)),
  62. new RotatedTranslatedShape(Vec3::sZero(), rotate_x, new CapsuleShape(1, 0.5f)),
  63. new RotatedTranslatedShape(Vec3::sZero(), rotate_x, TaperedCapsuleShapeSettings(1.0f, 1.0f, 0.5f).Create().Get()),
  64. new RotatedTranslatedShape(Vec3::sZero(), rotate_x, new CylinderShape(1, 0.5f)),
  65. tetrahedron.Create().Get(),
  66. compound_shape.Create().Get(),
  67. };
  68. int num_shapes = (int)std::size(shapes);
  69. for (int i = 0; i < num_shapes; ++i)
  70. {
  71. bcs.SetShape(shapes[i % num_shapes]);
  72. bcs.mPosition = RVec3(-float(num_shapes) + 2.0f * i, 15.0f, 0);
  73. mBodyInterface->CreateAndAddBody(bcs, EActivation::Activate);
  74. }
  75. }