PoweredRigTest.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #include <Utils/AssetStream.h>
  12. JPH_IMPLEMENT_RTTI_VIRTUAL(PoweredRigTest)
  13. {
  14. JPH_ADD_BASE_CLASS(PoweredRigTest, Test)
  15. }
  16. const char *PoweredRigTest::sAnimations[] =
  17. {
  18. "neutral",
  19. "walk",
  20. "sprint",
  21. "dead_pose1",
  22. "dead_pose2",
  23. "dead_pose3",
  24. "dead_pose4"
  25. };
  26. const char *PoweredRigTest::sAnimationName = "sprint";
  27. PoweredRigTest::~PoweredRigTest()
  28. {
  29. mRagdoll->RemoveFromPhysicsSystem();
  30. }
  31. void PoweredRigTest::Initialize()
  32. {
  33. // Floor
  34. CreateFloor();
  35. // Load ragdoll
  36. mRagdollSettings = RagdollLoader::sLoad("Human.tof", EMotionType::Dynamic);
  37. // Create ragdoll
  38. mRagdoll = mRagdollSettings->CreateRagdoll(0, 0, mPhysicsSystem);
  39. mRagdoll->AddToPhysicsSystem(EActivation::Activate);
  40. // Load animation
  41. AssetStream stream(String("Human/") + sAnimationName + ".tof", std::ios::in);
  42. if (!ObjectStreamIn::sReadObject(stream.Get(), mAnimation))
  43. FatalError("Could not open animation");
  44. // Initialize pose
  45. mPose.SetSkeleton(mRagdollSettings->GetSkeleton());
  46. // Position ragdoll
  47. mAnimation->Sample(0.0f, mPose);
  48. mPose.CalculateJointMatrices();
  49. mRagdoll->SetPose(mPose);
  50. }
  51. void PoweredRigTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  52. {
  53. // Update time
  54. mTime += inParams.mDeltaTime;
  55. // Sample new pose
  56. mAnimation->Sample(mTime, mPose);
  57. // Place the root joint on the first body so that we draw the pose in the right place
  58. RVec3 root_offset;
  59. SkeletonPose::JointState &joint = mPose.GetJoint(0);
  60. joint.mTranslation = Vec3::sZero(); // All the translation goes into the root offset
  61. mRagdoll->GetRootTransform(root_offset, joint.mRotation);
  62. mPose.SetRootOffset(root_offset);
  63. mPose.CalculateJointMatrices();
  64. #ifdef JPH_DEBUG_RENDERER
  65. mPose.Draw(*inParams.mPoseDrawSettings, mDebugRenderer);
  66. #endif // JPH_DEBUG_RENDERER
  67. mRagdoll->DriveToPoseUsingMotors(mPose);
  68. }
  69. void PoweredRigTest::CreateSettingsMenu(DebugUI *inUI, UIElement *inSubMenu)
  70. {
  71. inUI->CreateTextButton(inSubMenu, "Select Animation", [this, inUI]() {
  72. UIElement *animation_name = inUI->CreateMenu();
  73. for (uint i = 0; i < size(sAnimations); ++i)
  74. inUI->CreateTextButton(animation_name, sAnimations[i], [this, i]() { sAnimationName = sAnimations[i]; RestartTest(); });
  75. inUI->ShowMenu(animation_name);
  76. });
  77. }
  78. void PoweredRigTest::SaveState(StateRecorder &inStream) const
  79. {
  80. inStream.Write(mTime);
  81. }
  82. void PoweredRigTest::RestoreState(StateRecorder &inStream)
  83. {
  84. inStream.Read(mTime);
  85. }