BigWorldTest.cpp 5.1 KB

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