LoadSaveRigTest.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2023 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Tests/Rig/LoadSaveRigTest.h>
  6. #include <Jolt/Physics/Constraints/DistanceConstraint.h>
  7. #include <Jolt/ObjectStream/ObjectStreamOut.h>
  8. #include <Jolt/ObjectStream/ObjectStreamIn.h>
  9. #include <Utils/Log.h>
  10. #include <Utils/RagdollLoader.h>
  11. JPH_IMPLEMENT_RTTI_VIRTUAL(LoadSaveRigTest)
  12. {
  13. JPH_ADD_BASE_CLASS(LoadSaveRigTest, Test)
  14. }
  15. LoadSaveRigTest::~LoadSaveRigTest()
  16. {
  17. mRagdoll->RemoveFromPhysicsSystem();
  18. }
  19. void LoadSaveRigTest::Initialize()
  20. {
  21. // Floor
  22. CreateFloor();
  23. stringstream data;
  24. {
  25. // Load ragdoll
  26. Ref<RagdollSettings> settings = RagdollLoader::sLoad("Assets/Human.tof", EMotionType::Dynamic);
  27. // Add an additional constraint between the left and right arm to test loading/saving of additional constraints
  28. const Skeleton *skeleton = settings->GetSkeleton();
  29. int left_arm = skeleton->GetJointIndex("L_Wrist_sjnt_0");
  30. int right_arm = skeleton->GetJointIndex("R_Wrist_sjnt_0");
  31. Ref<DistanceConstraintSettings> constraint = new DistanceConstraintSettings;
  32. constraint->mSpace = EConstraintSpace::LocalToBodyCOM;
  33. constraint->mMaxDistance = 0.1f;
  34. constraint->mMinDistance = 0.1f;
  35. settings->mAdditionalConstraints.push_back(RagdollSettings::AdditionalConstraint(left_arm, right_arm , constraint));
  36. // Write ragdoll
  37. if (!ObjectStreamOut::sWriteObject(data, ObjectStream::EStreamType::Text, *settings))
  38. FatalError("Failed to save ragdoll");
  39. }
  40. // Read ragdoll back in
  41. Ref<RagdollSettings> settings;
  42. if (!ObjectStreamIn::sReadObject(data, settings))
  43. FatalError("Failed to load ragdoll");
  44. // Parent joint indices are not stored so need to be calculated again
  45. settings->GetSkeleton()->CalculateParentJointIndices();
  46. // Create ragdoll
  47. mRagdoll = settings->CreateRagdoll(0, 0, mPhysicsSystem);
  48. mRagdoll->AddToPhysicsSystem(EActivation::Activate);
  49. }