PoweredRigTest.cpp 2.5 KB

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