SoftBodyShapesTest.cpp 3.9 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/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/TaperedCylinderShape.h>
  14. #include <Jolt/Physics/Collision/Shape/ConvexHullShape.h>
  15. #include <Jolt/Physics/Collision/Shape/StaticCompoundShape.h>
  16. #include <Jolt/Physics/Collision/Shape/RotatedTranslatedShape.h>
  17. #include <Utils/SoftBodyCreator.h>
  18. #include <Renderer/DebugRendererImp.h>
  19. #include <Layers.h>
  20. JPH_IMPLEMENT_RTTI_VIRTUAL(SoftBodyShapesTest)
  21. {
  22. JPH_ADD_BASE_CLASS(SoftBodyShapesTest, Test)
  23. }
  24. void SoftBodyShapesTest::Initialize()
  25. {
  26. const Quat cCubeOrientation = Quat::sRotation(Vec3::sReplicate(sqrt(1.0f / 3.0f)), DegreesToRadians(45.0f));
  27. // Floor
  28. CreateMeshTerrain();
  29. // Create cloth that's fixated at the corners
  30. SoftBodyCreationSettings cloth(SoftBodyCreator::CreateClothWithFixatedCorners(), RVec3(0, 10.0f, 0), Quat::sRotation(Vec3::sAxisY(), 0.25f * JPH_PI), 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, Layers::MOVING);
  36. cube.mRestitution = 0.0f;
  37. mBodyInterface->CreateAndAddSoftBody(cube, EActivation::Activate);
  38. // Create pressurized sphere
  39. SoftBodyCreationSettings sphere(SoftBodyCreator::CreateSphere(), RVec3(15.0f, 10.0f, 15.0f), Quat::sIdentity(), Layers::MOVING);
  40. sphere.mPressure = 2000.0f;
  41. mBodyInterface->CreateAndAddSoftBody(sphere, EActivation::Activate);
  42. // Sphere below pressurized sphere
  43. RefConst<Shape> sphere_shape = new SphereShape(1.0f);
  44. BodyCreationSettings bcs(sphere_shape, RVec3(15.5f, 7.0f, 15.0f), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  45. bcs.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  46. bcs.mMassPropertiesOverride.mMass = 100.0f;
  47. mBodyInterface->CreateAndAddBody(bcs, EActivation::Activate);
  48. // Various shapes above cloth
  49. ConvexHullShapeSettings tetrahedron({ Vec3(-2, -2, -2), Vec3(0, -2, 2), Vec3(2, -2, -2), Vec3(0, 2, 0) });
  50. tetrahedron.SetEmbedded();
  51. StaticCompoundShapeSettings compound_shape;
  52. compound_shape.SetEmbedded();
  53. Quat rotate_x = Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI);
  54. compound_shape.AddShape(Vec3::sZero(), rotate_x, new CapsuleShape(2, 0.5f));
  55. compound_shape.AddShape(Vec3(0, 0, -2), Quat::sIdentity(), new SphereShape(1));
  56. compound_shape.AddShape(Vec3(0, 0, 2), Quat::sIdentity(), new SphereShape(1));
  57. RefConst<Shape> shapes[] = {
  58. sphere_shape,
  59. new BoxShape(Vec3(0.75f, 1.0f, 1.25f)),
  60. new RotatedTranslatedShape(Vec3::sZero(), rotate_x, new CapsuleShape(1, 0.5f)),
  61. new RotatedTranslatedShape(Vec3::sZero(), rotate_x, TaperedCapsuleShapeSettings(1.0f, 1.0f, 0.5f).Create().Get()),
  62. new RotatedTranslatedShape(Vec3::sZero(), rotate_x, new CylinderShape(1, 0.5f)),
  63. new RotatedTranslatedShape(Vec3::sZero(), rotate_x, TaperedCylinderShapeSettings(1, 0.5f, 1.0f).Create().Get()),
  64. tetrahedron.Create().Get(),
  65. compound_shape.Create().Get(),
  66. };
  67. int num_shapes = (int)std::size(shapes);
  68. for (int i = 0; i < num_shapes; ++i)
  69. {
  70. bcs.SetShape(shapes[i % num_shapes]);
  71. bcs.mPosition = RVec3(-float(num_shapes) + 2.0f * i, 15.0f, 0);
  72. mBodyInterface->CreateAndAddBody(bcs, EActivation::Activate);
  73. }
  74. }