SoftKeyframedRigTest.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2025 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Tests/Rig/SoftKeyframedRigTest.h>
  6. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  7. #include <Jolt/Physics/StateRecorder.h>
  8. #include <Jolt/ObjectStream/ObjectStreamIn.h>
  9. #include <Application/DebugUI.h>
  10. #include <Layers.h>
  11. #include <Utils/Log.h>
  12. #include <Utils/AssetStream.h>
  13. JPH_IMPLEMENT_RTTI_VIRTUAL(SoftKeyframedRigTest)
  14. {
  15. JPH_ADD_BASE_CLASS(SoftKeyframedRigTest, Test)
  16. }
  17. SoftKeyframedRigTest::~SoftKeyframedRigTest()
  18. {
  19. mRagdoll->RemoveFromPhysicsSystem();
  20. }
  21. void SoftKeyframedRigTest::Initialize()
  22. {
  23. // Floor
  24. CreateFloor();
  25. // Wall
  26. RefConst<Shape> box_shape = new BoxShape(Vec3(0.2f, 0.2f, 0.2f), 0.01f);
  27. for (int i = 0; i < 3; ++i)
  28. for (int j = i / 2; j < 10 - (i + 1) / 2; ++j)
  29. {
  30. RVec3 position(-2.0f + j * 0.4f + (i & 1? 0.2f : 0.0f), 0.2f + i * 0.4f, -2.0f);
  31. mBodyInterface->CreateAndAddBody(BodyCreationSettings(box_shape, position, Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING), EActivation::DontActivate);
  32. }
  33. // Bar to hit head against
  34. mBodyInterface->CreateAndAddBody(BodyCreationSettings(new BoxShape(Vec3(2.0f, 0.1f, 0.1f), 0.01f), RVec3(0, 1.5f, -2.0f), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  35. // Load ragdoll
  36. mRagdollSettings = RagdollLoader::sLoad("Human.tof", EMotionType::Dynamic);
  37. // Limit max velocity of the bodies to avoid excessive jittering when the head hits the bar
  38. // Note that this also limits how fast an animation can be and as a result you can see
  39. // the ragdolls lag behind when the animation loops.
  40. // Note that the velocity doesn't need to be limited at body level, it can also be done
  41. // by calculating the needed velocities and clamping them instead of calling DriveToPoseUsingKinematics.
  42. for (BodyCreationSettings &bcs : mRagdollSettings->mParts)
  43. bcs.mMaxLinearVelocity = 10.0f;
  44. // Create ragdoll
  45. mRagdoll = mRagdollSettings->CreateRagdoll(0, 0, mPhysicsSystem);
  46. mRagdoll->AddToPhysicsSystem(EActivation::Activate);
  47. // Load animation
  48. AssetStream stream("Human/walk.tof", std::ios::in);
  49. if (!ObjectStreamIn::sReadObject(stream.Get(), mAnimation))
  50. FatalError("Could not open animation");
  51. // Initialize pose
  52. mPose.SetSkeleton(mRagdollSettings->GetSkeleton());
  53. // Position ragdoll
  54. mAnimation->Sample(0.0f, mPose);
  55. mPose.CalculateJointMatrices();
  56. mRagdoll->SetPose(mPose);
  57. }
  58. void SoftKeyframedRigTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  59. {
  60. // Sample previous pose and draw it (ragdoll should have achieved this position)
  61. mAnimation->Sample(mTime, mPose);
  62. mPose.CalculateJointMatrices();
  63. #ifdef JPH_DEBUG_RENDERER
  64. mPose.Draw(*inParams.mPoseDrawSettings, mDebugRenderer);
  65. #endif // JPH_DEBUG_RENDERER
  66. // Update time
  67. mTime += inParams.mDeltaTime;
  68. // Sample new pose
  69. mAnimation->Sample(mTime, mPose);
  70. mPose.CalculateJointMatrices();
  71. // Drive the ragdoll by setting velocities
  72. mRagdoll->DriveToPoseUsingKinematics(mPose, inParams.mDeltaTime);
  73. // Cancel gravity that will be applied in the next step
  74. mRagdoll->AddLinearVelocity(mPhysicsSystem->GetGravity() * inParams.mDeltaTime);
  75. }
  76. void SoftKeyframedRigTest::SaveState(StateRecorder &inStream) const
  77. {
  78. inStream.Write(mTime);
  79. }
  80. void SoftKeyframedRigTest::RestoreState(StateRecorder &inStream)
  81. {
  82. inStream.Read(mTime);
  83. }