SoftBodyShapesTest.cpp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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), Layers::MOVING);
  30. cloth.mUpdatePosition = false; // Don't update the position of the cloth as it is fixed to the world
  31. cloth.mMakeRotationIdentity = false; // Test explicitly checks if soft bodies with a rotation collide with shapes properly
  32. mBodyInterface->CreateAndAddSoftBody(cloth, EActivation::Activate);
  33. // Create cube
  34. SoftBodyCreationSettings cube(SoftBodyCreator::CreateCube(), RVec3(20.0f, 10.0f, 0.0f), cCubeOrientation, Layers::MOVING);
  35. cube.mRestitution = 0.0f;
  36. mBodyInterface->CreateAndAddSoftBody(cube, EActivation::Activate);
  37. // Create pressurized sphere
  38. SoftBodyCreationSettings sphere(SoftBodyCreator::CreateSphere(), RVec3(15.0f, 10.0f, 15.0f), Quat::sIdentity(), Layers::MOVING);
  39. sphere.mPressure = 2000.0f;
  40. mBodyInterface->CreateAndAddSoftBody(sphere, EActivation::Activate);
  41. // Sphere below pressurized sphere
  42. RefConst<Shape> sphere_shape = new SphereShape(1.0f);
  43. BodyCreationSettings bcs(sphere_shape, RVec3(15.5f, 7.0f, 15.0f), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  44. bcs.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  45. bcs.mMassPropertiesOverride.mMass = 100.0f;
  46. mBodyInterface->CreateAndAddBody(bcs, EActivation::Activate);
  47. // Various shapes above cloth
  48. ConvexHullShapeSettings tetrahedron({ Vec3(-2, -2, -2), Vec3(0, -2, 2), Vec3(2, -2, -2), Vec3(0, 2, 0) });
  49. tetrahedron.SetEmbedded();
  50. StaticCompoundShapeSettings compound_shape;
  51. compound_shape.SetEmbedded();
  52. Quat rotate_x = Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI);
  53. compound_shape.AddShape(Vec3::sZero(), rotate_x, new CapsuleShape(2, 0.5f));
  54. compound_shape.AddShape(Vec3(0, 0, -2), Quat::sIdentity(), new SphereShape(1));
  55. compound_shape.AddShape(Vec3(0, 0, 2), Quat::sIdentity(), new SphereShape(1));
  56. RefConst<Shape> shapes[] = {
  57. sphere_shape,
  58. new BoxShape(Vec3(0.75f, 1.0f, 1.25f)),
  59. new RotatedTranslatedShape(Vec3::sZero(), rotate_x, new CapsuleShape(1, 0.5f)),
  60. new RotatedTranslatedShape(Vec3::sZero(), rotate_x, TaperedCapsuleShapeSettings(1.0f, 1.0f, 0.5f).Create().Get()),
  61. new RotatedTranslatedShape(Vec3::sZero(), rotate_x, new CylinderShape(1, 0.5f)),
  62. tetrahedron.Create().Get(),
  63. compound_shape.Create().Get(),
  64. };
  65. int num_shapes = (int)std::size(shapes);
  66. for (int i = 0; i < num_shapes; ++i)
  67. {
  68. bcs.SetShape(shapes[i % num_shapes]);
  69. bcs.mPosition = RVec3(-float(num_shapes) + 2.0f * i, 15.0f, 0);
  70. mBodyInterface->CreateAndAddBody(bcs, EActivation::Activate);
  71. }
  72. }