WaterShapeTest.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Tests/Water/WaterShapeTest.h>
  6. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  7. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  8. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  9. #include <Jolt/Physics/Collision/Shape/StaticCompoundShape.h>
  10. #include <Jolt/Physics/Collision/Shape/MutableCompoundShape.h>
  11. #include <Jolt/Physics/Collision/Shape/ConvexHullShape.h>
  12. #include <Jolt/Physics/Collision/Shape/ScaledShape.h>
  13. #include <Jolt/Physics/Collision/Shape/OffsetCenterOfMassShape.h>
  14. #include <Layers.h>
  15. #include <Renderer/DebugRendererImp.h>
  16. JPH_IMPLEMENT_RTTI_VIRTUAL(WaterShapeTest)
  17. {
  18. JPH_ADD_BASE_CLASS(WaterShapeTest, Test)
  19. }
  20. void WaterShapeTest::Initialize()
  21. {
  22. CreateFloor();
  23. // Create scaled box
  24. BodyID body_id = mBodyInterface->CreateBody(BodyCreationSettings(new ScaledShape(new BoxShape(Vec3(1.0f, 2.0f, 2.5f)), Vec3(0.5f, 0.6f, -0.7f)), RVec3(-10, 20, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING))->GetID();
  25. mBodyInterface->AddBody(body_id, EActivation::Activate);
  26. // Create box
  27. body_id = mBodyInterface->CreateBody(BodyCreationSettings(new BoxShape(Vec3(1.0f, 2.0f, 2.5f)), RVec3(-7, 20, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING))->GetID();
  28. mBodyInterface->AddBody(body_id, EActivation::Activate);
  29. // Create sphere
  30. body_id = mBodyInterface->CreateBody(BodyCreationSettings(new SphereShape(2.0f), RVec3(-3, 20, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING))->GetID();
  31. mBodyInterface->AddBody(body_id, EActivation::Activate);
  32. // Create static compound
  33. Ref<StaticCompoundShapeSettings> static_compound = new StaticCompoundShapeSettings;
  34. static_compound->AddShape(Vec3(2.0f, 0, 0), Quat::sIdentity(), new SphereShape(2.0f));
  35. static_compound->AddShape(Vec3(-1.0f, 0, 0), Quat::sIdentity(), new SphereShape(1.0f));
  36. body_id = mBodyInterface->CreateBody(BodyCreationSettings(static_compound, RVec3(3, 20, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING))->GetID();
  37. mBodyInterface->AddBody(body_id, EActivation::Activate);
  38. // Create tetrahedron
  39. Array<Vec3> tetrahedron;
  40. tetrahedron.push_back(Vec3(-2, 0, -2));
  41. tetrahedron.push_back(Vec3(0, 0, 2));
  42. tetrahedron.push_back(Vec3(2, 0, -2));
  43. tetrahedron.push_back(Vec3(0, -2, 0));
  44. Ref<ConvexHullShapeSettings> tetrahedron_shape = new ConvexHullShapeSettings(tetrahedron);
  45. body_id = mBodyInterface->CreateBody(BodyCreationSettings(tetrahedron_shape, RVec3(10, 20, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING))->GetID();
  46. mBodyInterface->AddBody(body_id, EActivation::Activate);
  47. // Non-uniform scaled tetrahedron
  48. body_id = mBodyInterface->CreateBody(BodyCreationSettings(new ScaledShapeSettings(tetrahedron_shape, Vec3(1, -1.5f, 2.0f)), RVec3(15, 20, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING))->GetID();
  49. mBodyInterface->AddBody(body_id, EActivation::Activate);
  50. // Create convex hull box
  51. Array<Vec3> box;
  52. box.push_back(Vec3(1.5f, 1.0f, 0.5f));
  53. box.push_back(Vec3(-1.5f, 1.0f, 0.5f));
  54. box.push_back(Vec3(1.5f, -1.0f, 0.5f));
  55. box.push_back(Vec3(-1.5f, -1.0f, 0.5f));
  56. box.push_back(Vec3(1.5f, 1.0f, -0.5f));
  57. box.push_back(Vec3(-1.5f, 1.0f, -0.5f));
  58. box.push_back(Vec3(1.5f, -1.0f, -0.5f));
  59. box.push_back(Vec3(-1.5f, -1.0f, -0.5f));
  60. body_id = mBodyInterface->CreateBody(BodyCreationSettings(new ConvexHullShapeSettings(box), RVec3(18, 20, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING))->GetID();
  61. mBodyInterface->AddBody(body_id, EActivation::Activate);
  62. // Create random convex shape
  63. default_random_engine random;
  64. uniform_real_distribution<float> hull_size(0.1f, 1.9f);
  65. Array<Vec3> points;
  66. for (int j = 0; j < 20; ++j)
  67. points.push_back(hull_size(random) * Vec3::sRandom(random));
  68. body_id = mBodyInterface->CreateBody(BodyCreationSettings(new ConvexHullShapeSettings(points), RVec3(21, 20, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING))->GetID();
  69. mBodyInterface->AddBody(body_id, EActivation::Activate);
  70. // Create mutable compound
  71. Ref<MutableCompoundShapeSettings> mutable_compound = new MutableCompoundShapeSettings;
  72. mutable_compound->AddShape(Vec3(1.0f, 0, 0), Quat::sIdentity(), new BoxShape(Vec3(0.5f, 0.75f, 1.0f)));
  73. mutable_compound->AddShape(Vec3(-1.0f, 0, 0), Quat::sIdentity(), new SphereShape(1.0f));
  74. body_id = mBodyInterface->CreateBody(BodyCreationSettings(mutable_compound, RVec3(25, 20, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING))->GetID();
  75. mBodyInterface->AddBody(body_id, EActivation::Activate);
  76. // Create box with center of mass offset
  77. body_id = mBodyInterface->CreateBody(BodyCreationSettings(new OffsetCenterOfMassShapeSettings(Vec3(-1.0f, 0.0f, 0.0f), new BoxShape(Vec3(2.0f, 0.25f, 0.25f))), RVec3(30, 20, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING))->GetID();
  78. mBodyInterface->AddBody(body_id, EActivation::Activate);
  79. }
  80. void WaterShapeTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  81. {
  82. // Draw the water surface 5mm below actual surface to avoid z fighting with intersection shapes
  83. RVec3 surface_point = RVec3(0, 10, 0);
  84. for (int i = -20; i <= 20; ++i)
  85. {
  86. mDebugRenderer->DrawLine(surface_point + Vec3(5.0f * i, 0, -100), surface_point + Vec3(5.0f * i, 0, 100), Color::sBlue);
  87. mDebugRenderer->DrawLine(surface_point + Vec3(-100, 0, 5.0f * i), surface_point + Vec3(100, 0, 5.0f * i), Color::sBlue);
  88. }
  89. // Broadphase results, will apply buoyancy to any body that intersects with the water volume
  90. class MyCollector : public CollideShapeBodyCollector
  91. {
  92. public:
  93. MyCollector(PhysicsSystem *inSystem, RVec3Arg inSurfacePosition, Vec3Arg inSurfaceNormal, float inDeltaTime) : mSystem(inSystem), mSurfacePosition(inSurfacePosition), mSurfaceNormal(inSurfaceNormal), mDeltaTime(inDeltaTime) { }
  94. virtual void AddHit(const BodyID &inBodyID) override
  95. {
  96. BodyLockWrite lock(mSystem->GetBodyLockInterface(), inBodyID);
  97. Body &body = lock.GetBody();
  98. if (body.IsActive())
  99. body.ApplyBuoyancyImpulse(mSurfacePosition, mSurfaceNormal, 1.1f, 0.3f, 0.05f, Vec3::sZero(), mSystem->GetGravity(), mDeltaTime);
  100. }
  101. private:
  102. PhysicsSystem * mSystem;
  103. RVec3 mSurfacePosition;
  104. Vec3 mSurfaceNormal;
  105. float mDeltaTime;
  106. };
  107. MyCollector collector(mPhysicsSystem, surface_point, Vec3::sAxisY(), inParams.mDeltaTime);
  108. // Apply buoyancy to all bodies that intersect with the water
  109. AABox water_box(-Vec3(100, 100, 100), Vec3(100, 0, 100));
  110. water_box.Translate(Vec3(surface_point));
  111. mPhysicsSystem->GetBroadPhaseQuery().CollideAABox(water_box, collector, SpecifiedBroadPhaseLayerFilter(BroadPhaseLayers::MOVING), SpecifiedObjectLayerFilter(Layers::MOVING));
  112. }