PoweredRigTest.cpp 2.7 KB

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