BigWorldTest.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2022 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Tests/Rig/BigWorldTest.h>
  6. #include <Jolt/Physics/PhysicsScene.h>
  7. #include <Jolt/Skeleton/Skeleton.h>
  8. #include <Jolt/Skeleton/SkeletalAnimation.h>
  9. #include <Jolt/Skeleton/SkeletonPose.h>
  10. #include <Jolt/Core/StringTools.h>
  11. #include <Jolt/ObjectStream/ObjectStreamIn.h>
  12. #include <Utils/RagdollLoader.h>
  13. #include <Application/DebugUI.h>
  14. #include <Renderer/DebugRendererImp.h>
  15. #include <Layers.h>
  16. #include <Utils/Log.h>
  17. #include <random>
  18. JPH_IMPLEMENT_RTTI_VIRTUAL(BigWorldTest)
  19. {
  20. JPH_ADD_BASE_CLASS(BigWorldTest, Test)
  21. }
  22. BigWorldTest::~BigWorldTest()
  23. {
  24. for (Pile &pile : mPiles)
  25. for (Ragdoll *r : pile.mRagdolls)
  26. r->RemoveFromPhysicsSystem();
  27. }
  28. void BigWorldTest::Initialize()
  29. {
  30. constexpr int cPileSize = 5;
  31. // Default terrain
  32. Body &floor = CreateMeshTerrain();
  33. RefConst<Shape> shape = floor.GetShape();
  34. // Load ragdoll
  35. Ref<RagdollSettings> settings = RagdollLoader::sLoad("Assets/Human.tof", EMotionType::Dynamic);
  36. // Load animation
  37. Ref<SkeletalAnimation> animation;
  38. if (!ObjectStreamIn::sReadObject("Assets/Human/Dead_Pose1.tof", animation))
  39. FatalError("Could not open animation");
  40. SkeletonPose pose;
  41. pose.SetSkeleton(settings->GetSkeleton());
  42. animation->Sample(0.0f, pose);
  43. // Determine rotation for each ragdoll in the pile
  44. default_random_engine random;
  45. uniform_real_distribution<float> angle(0.0f, JPH_PI);
  46. Array<Quat> rotation;
  47. for (int i = 0; i < cPileSize; ++i)
  48. rotation.push_back(Quat::sRotation(Vec3::sAxisY(), angle(random)) * pose.GetJoint(0).mRotation);
  49. // Create piles at various distances
  50. Real distances[] = { 0.0_r, 1.0e3_r, 5.0e3_r, 1.0e4_r, 5.0e4_r, 1.0e5_r, 1.0e6_r, 1.0e7_r, 1.0e8_r };
  51. for (Real distance : distances)
  52. {
  53. // Calculate origin for this simulation assuming we want to be 'distance' away and the same distance along each coordinate axis
  54. RVec3 origin = RVec3::sReplicate(distance) / sqrt(3.0_r);
  55. // Create floor (floor at 0 was already created)
  56. if (distance != 0.0f)
  57. mBodyInterface->CreateAndAddBody(BodyCreationSettings(shape, origin, Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  58. // Create pile of ragdolls
  59. Pile pile;
  60. pile.mOrigin = origin;
  61. for (int i = 0; i < cPileSize; ++i)
  62. {
  63. // Create ragdoll
  64. Ref<Ragdoll> ragdoll = settings->CreateRagdoll(0, 0, mPhysicsSystem);
  65. // Override root
  66. SkeletonPose::JointState &root = pose.GetJoint(0);
  67. root.mTranslation = Vec3::sZero();
  68. root.mRotation = rotation[i];
  69. pose.SetRootOffset(origin + Vec3(0, 2.0f + 0.6f * i, 0));
  70. pose.CalculateJointMatrices();
  71. // Drive to pose
  72. ragdoll->SetPose(pose);
  73. ragdoll->DriveToPoseUsingMotors(pose);
  74. ragdoll->AddToPhysicsSystem(EActivation::Activate);
  75. pile.mRagdolls.push_back(ragdoll);
  76. }
  77. mPiles.push_back(pile);
  78. }
  79. }
  80. void BigWorldTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  81. {
  82. int pile_idx = 0;
  83. for (Pile &pile : mPiles)
  84. if (!pile.mOrigin.IsNearZero()) // Pile at 0 is drawn normally
  85. {
  86. // Check if we need to draw this pile
  87. if ((sDrawPileMask & (1 << pile_idx)) != 0)
  88. {
  89. Color color = Color::sGetDistinctColor(pile_idx);
  90. bool first = true;
  91. for (Ragdoll *r : pile.mRagdolls)
  92. {
  93. for (const BodyID &id : r->GetBodyIDs())
  94. {
  95. BodyLockRead lock(mPhysicsSystem->GetBodyLockInterface(), id);
  96. if (lock.Succeeded())
  97. {
  98. const Body &body = lock.GetBody();
  99. // Shift the transform back to the origin
  100. RMat44 transform = body.GetCenterOfMassTransform();
  101. transform.SetTranslation(transform.GetTranslation() - pile.mOrigin);
  102. // Draw distance label for the first body
  103. if (first)
  104. {
  105. mDebugRenderer->DrawText3D(transform.GetTranslation(), pile.GetLabel().c_str(), color, 0.2f);
  106. first = false;
  107. }
  108. #ifdef JPH_DEBUG_RENDERER
  109. // Draw the shape
  110. body.GetShape()->Draw(mDebugRenderer, transform, Vec3::sReplicate(1.0f), color, false, sDrawWireframe);
  111. #endif // JPH_DEBUG_RENDERER
  112. }
  113. }
  114. }
  115. }
  116. pile_idx++;
  117. }
  118. }
  119. void BigWorldTest::CreateSettingsMenu(DebugUI *inUI, UIElement *inSubMenu)
  120. {
  121. // Draw in wireframe?
  122. inUI->CreateCheckBox(inSubMenu, "Draw distant scenes in wireframe", sDrawWireframe, [](UICheckBox::EState inState) { sDrawWireframe = inState == UICheckBox::STATE_CHECKED; });
  123. // Enable / disable drawing of a particular distance
  124. int pile_idx = 0;
  125. for (Pile &pile : mPiles)
  126. if (!pile.mOrigin.IsNearZero())
  127. {
  128. uint32 mask = 1 << pile_idx;
  129. inUI->CreateCheckBox(inSubMenu, "Draw pile at " + pile.GetLabel(), (sDrawPileMask & mask) != 0, [mask](UICheckBox::EState inState) { if (inState == UICheckBox::STATE_CHECKED) sDrawPileMask |= mask; else sDrawPileMask &= ~mask; });
  130. pile_idx++;
  131. }
  132. // Goto pile at a particular distance
  133. for (Pile &pile : mPiles)
  134. inUI->CreateTextButton(inSubMenu, "Goto pile at " + pile.GetLabel(), [this, &pile]() { sPivot = pile.mOrigin; RestartTest(); });
  135. }
  136. RMat44 BigWorldTest::GetCameraPivot(float inCameraHeading, float inCameraPitch) const
  137. {
  138. return RMat44::sTranslation(sPivot);
  139. }
  140. RVec3 BigWorldTest::GetDrawOffset() const
  141. {
  142. return sPivot;
  143. }