RagdollScene.h 4.2 KB

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