PoweredRigTest.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/Rig/PoweredRigTest.h>
  5. #include <Physics/StateRecorder.h>
  6. #include <Application/DebugUI.h>
  7. #include <Utils/RagdollLoader.h>
  8. #include <Utils/Log.h>
  9. JPH_IMPLEMENT_RTTI_VIRTUAL(PoweredRigTest)
  10. {
  11. JPH_ADD_BASE_CLASS(PoweredRigTest, Test)
  12. }
  13. const char *PoweredRigTest::sAnimations[] =
  14. {
  15. "Neutral",
  16. "Walk",
  17. "Sprint",
  18. "Dead_Pose1",
  19. "Dead_Pose2",
  20. "Dead_Pose3",
  21. "Dead_Pose4"
  22. };
  23. const char *PoweredRigTest::sAnimationName = "Dead_Pose1";
  24. PoweredRigTest::~PoweredRigTest()
  25. {
  26. mRagdoll->RemoveFromPhysicsSystem();
  27. }
  28. void PoweredRigTest::Initialize()
  29. {
  30. // Floor
  31. CreateFloor();
  32. // Load ragdoll
  33. mRagdollSettings = RagdollLoader::sLoad("Assets/Human.tof", EMotionType::Dynamic);
  34. // Create ragdoll
  35. mRagdoll = mRagdollSettings->CreateRagdoll(0, nullptr, mPhysicsSystem);
  36. mRagdoll->AddToPhysicsSystem(EActivation::Activate);
  37. // Load animation
  38. string filename = string("Assets/Human/") + sAnimationName + ".tof";
  39. if (!ObjectStreamIn::sReadObject(filename.c_str(), mAnimation))
  40. FatalError("Could not open animation");
  41. // Initialize pose
  42. mPose.SetSkeleton(mRagdollSettings->GetSkeleton());
  43. // Position ragdoll
  44. mAnimation->Sample(0.0f, mPose);
  45. mPose.CalculateJointMatrices();
  46. mRagdoll->SetPose(mPose);
  47. }
  48. void PoweredRigTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  49. {
  50. // Update time
  51. mTime += inParams.mDeltaTime;
  52. // Sample new pose
  53. mAnimation->Sample(mTime, mPose);
  54. // Place the root joint on the first body so that we draw the pose in the right place
  55. SkeletonPose::JointState &joint = mPose.GetJoint(0);
  56. mRagdoll->GetRootTransform(joint.mTranslation, joint.mRotation);
  57. mPose.CalculateJointMatrices();
  58. #ifdef JPH_DEBUG_RENDERER
  59. mPose.Draw(*inParams.mPoseDrawSettings, mDebugRenderer);
  60. #endif // JPH_DEBUG_RENDERER
  61. mRagdoll->DriveToPoseUsingMotors(mPose);
  62. }
  63. void PoweredRigTest::CreateSettingsMenu(DebugUI *inUI, UIElement *inSubMenu)
  64. {
  65. inUI->CreateTextButton(inSubMenu, "Select Animation", [this, inUI]() {
  66. UIElement *animation_name = inUI->CreateMenu();
  67. for (uint i = 0; i < size(sAnimations); ++i)
  68. inUI->CreateTextButton(animation_name, sAnimations[i], [this, i]() { sAnimationName = sAnimations[i]; RestartTest(); });
  69. inUI->ShowMenu(animation_name);
  70. });
  71. }
  72. void PoweredRigTest::SaveState(StateRecorder &inStream) const
  73. {
  74. inStream.Write(mTime);
  75. }
  76. void PoweredRigTest::RestoreState(StateRecorder &inStream)
  77. {
  78. inStream.Read(mTime);
  79. }