WaterShapeTest.cpp 6.5 KB

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