RagdollScene.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. // Jolt includes
  5. #include <Physics/Ragdoll/Ragdoll.h>
  6. #include <Physics/PhysicsScene.h>
  7. #include <Physics/Collision/CastResult.h>
  8. #include <Physics/Collision/RayCast.h>
  9. // Local includes
  10. #include "PerformanceTestScene.h"
  11. #include "Layers.h"
  12. // A scene that loads a part of a Horizon Zero Dawn level and drops many ragdolls on the terrain (motors enabled)
  13. class RagdollScene : public PerformanceTestScene
  14. {
  15. public:
  16. virtual const char * GetName() const override
  17. {
  18. return "Ragdoll";
  19. }
  20. virtual bool Load() override
  21. {
  22. // Load ragdoll
  23. if (!ObjectStreamIn::sReadObject("Assets/Human.tof", mRagdollSettings))
  24. {
  25. cerr << "Unable to load ragdoll" << endl;
  26. return false;
  27. }
  28. for (BodyCreationSettings &body : mRagdollSettings->mParts)
  29. body.mObjectLayer = Layers::MOVING;
  30. // Init ragdoll
  31. mRagdollSettings->GetSkeleton()->CalculateParentJointIndices();
  32. mRagdollSettings->Stabilize();
  33. mRagdollSettings->CalculateBodyIndexToConstraintIndex();
  34. mRagdollSettings->CalculateConstraintIndexToBodyIdxPair();
  35. // Load animation
  36. if (!ObjectStreamIn::sReadObject("Assets/Human/dead_pose1.tof", mAnimation))
  37. {
  38. cerr << "Unable to load animation" << endl;
  39. return false;
  40. }
  41. // Sample pose
  42. mPose.SetSkeleton(mRagdollSettings->GetSkeleton());
  43. mAnimation->Sample(0.0f, mPose);
  44. // Read the background scene
  45. if (!ObjectStreamIn::sReadObject("Assets/terrain2.bof", mBackground))
  46. {
  47. cerr << "Unable to load terrain" << endl;
  48. return false;
  49. }
  50. for (BodyCreationSettings &body : mBackground->GetBodies())
  51. body.mObjectLayer = Layers::NON_MOVING;
  52. mBackground->FixInvalidScales();
  53. return true;
  54. }
  55. virtual void StartTest(PhysicsSystem &inPhysicsSystem, EMotionQuality inMotionQuality) override
  56. {
  57. // Test configuration
  58. const float cHorizontalSeparation = 4.0f;
  59. const float cVerticalSeparation = 0.6f;
  60. #ifdef _DEBUG
  61. const int cPileSize = 5;
  62. const int cNumRows = 2;
  63. const int cNumCols = 2;
  64. #else
  65. const int cPileSize = 10;
  66. const int cNumRows = 4;
  67. const int cNumCols = 4;
  68. #endif
  69. // Set motion quality on ragdoll
  70. for (BodyCreationSettings &body : mRagdollSettings->mParts)
  71. body.mMotionQuality = inMotionQuality;
  72. // Add background geometry
  73. mBackground->CreateBodies(&inPhysicsSystem);
  74. // Create ragdoll piles
  75. mt19937 random;
  76. uniform_real_distribution<float> angle(0.0f, JPH_PI);
  77. CollisionGroup::GroupID group_id = 1;
  78. for (int row = 0; row < cNumRows; ++row)
  79. for (int col = 0; col < cNumCols; ++col)
  80. {
  81. // Determine start location of ray
  82. Vec3 start = Vec3(cHorizontalSeparation * (col - (cNumCols - 1) / 2.0f), 100, cHorizontalSeparation * (row - (cNumRows - 1) / 2.0f));
  83. // Cast ray down to terrain
  84. RayCastResult hit;
  85. Vec3 ray_direction(0, -200, 0);
  86. RayCast ray { start, ray_direction };
  87. if (inPhysicsSystem.GetNarrowPhaseQuery().CastRay(ray, hit, SpecifiedBroadPhaseLayerFilter(BroadPhaseLayers::NON_MOVING), SpecifiedObjectLayerFilter(Layers::NON_MOVING)))
  88. start = start + hit.mFraction * ray_direction;
  89. for (int i = 0; i < cPileSize; ++i)
  90. {
  91. // Create ragdoll
  92. Ref<Ragdoll> ragdoll = mRagdollSettings->CreateRagdoll(group_id++, nullptr, &inPhysicsSystem);
  93. // Override root
  94. SkeletonPose pose_copy = mPose;
  95. SkeletonPose::JointState &root = pose_copy.GetJoint(0);
  96. root.mTranslation = start + Vec3(0, cVerticalSeparation * (i + 1), 0);
  97. root.mRotation = Quat::sRotation(Vec3::sAxisY(), angle(random)) * root.mRotation;
  98. pose_copy.CalculateJointMatrices();
  99. // Drive to pose
  100. ragdoll->SetPose(pose_copy);
  101. ragdoll->DriveToPoseUsingMotors(pose_copy);
  102. ragdoll->AddToPhysicsSystem(EActivation::Activate);
  103. // Keep reference
  104. mRagdolls.push_back(ragdoll);
  105. }
  106. }
  107. }
  108. virtual void StopTest(PhysicsSystem &inPhysicsSystem) override
  109. {
  110. // Remove ragdolls
  111. for (Ragdoll *ragdoll : mRagdolls)
  112. ragdoll->RemoveFromPhysicsSystem();
  113. mRagdolls.clear();
  114. }
  115. private:
  116. Ref<RagdollSettings> mRagdollSettings;
  117. Ref<SkeletalAnimation> mAnimation;
  118. SkeletonPose mPose;
  119. Ref<PhysicsScene> mBackground;
  120. vector<Ref<Ragdoll>> mRagdolls;
  121. };