CreateRigTest.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // SPDX-FileCopyrightText: 2022 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/Rig/CreateRigTest.h>
  5. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  6. #include <Jolt/Physics/Constraints/SwingTwistConstraint.h>
  7. #include <Application/DebugUI.h>
  8. #include <Layers.h>
  9. JPH_IMPLEMENT_RTTI_VIRTUAL(CreateRigTest)
  10. {
  11. JPH_ADD_BASE_CLASS(CreateRigTest, Test)
  12. }
  13. CreateRigTest::~CreateRigTest()
  14. {
  15. mRagdoll->RemoveFromPhysicsSystem();
  16. }
  17. void CreateRigTest::Initialize()
  18. {
  19. // Floor
  20. CreateFloor();
  21. // Create skeleton
  22. Ref<Skeleton> skeleton = new Skeleton;
  23. uint lower_body = skeleton->AddJoint("LowerBody");
  24. uint mid_body = skeleton->AddJoint("MidBody", lower_body);
  25. uint upper_body = skeleton->AddJoint("UpperBody", mid_body);
  26. /*uint head =*/ skeleton->AddJoint("Head", upper_body);
  27. uint upper_arm_l = skeleton->AddJoint("UpperArmL", upper_body);
  28. uint upper_arm_r = skeleton->AddJoint("UpperArmR", upper_body);
  29. /*uint lower_arm_l =*/ skeleton->AddJoint("LowerArmL", upper_arm_l);
  30. /*uint lower_arm_r =*/ skeleton->AddJoint("LowerArmR", upper_arm_r);
  31. uint upper_leg_l = skeleton->AddJoint("UpperLegL", lower_body);
  32. uint upper_leg_r = skeleton->AddJoint("UpperLegR", lower_body);
  33. /*uint lower_leg_l =*/ skeleton->AddJoint("LowerLegL", upper_leg_l);
  34. /*uint lower_leg_r =*/ skeleton->AddJoint("LowerLegR", upper_leg_r);
  35. // Create shapes for limbs
  36. Ref<Shape> shapes[] = {
  37. new CapsuleShape(0.15f, 0.10f), // Lower Body
  38. new CapsuleShape(0.15f, 0.10f), // Mid Body
  39. new CapsuleShape(0.15f, 0.10f), // Upper Body
  40. new CapsuleShape(0.075f, 0.10f), // Head
  41. new CapsuleShape(0.15f, 0.06f), // Upper Arm L
  42. new CapsuleShape(0.15f, 0.06f), // Upper Arm R
  43. new CapsuleShape(0.15f, 0.05f), // Lower Arm L
  44. new CapsuleShape(0.15f, 0.05f), // Lower Arm R
  45. new CapsuleShape(0.2f, 0.075f), // Upper Leg L
  46. new CapsuleShape(0.2f, 0.075f), // Upper Leg R
  47. new CapsuleShape(0.2f, 0.06f), // Lower Leg L
  48. new CapsuleShape(0.2f, 0.06f), // Lower Leg R
  49. };
  50. // Positions of body parts in world space
  51. Vec3 positions[] = {
  52. Vec3(0, 1.15f, 0), // Lower Body
  53. Vec3(0, 1.35f, 0), // Mid Body
  54. Vec3(0, 1.55f, 0), // Upper Body
  55. Vec3(0, 1.825f, 0), // Head
  56. Vec3(-0.425f, 1.55f, 0), // Upper Arm L
  57. Vec3(0.425f, 1.55f, 0), // Upper Arm R
  58. Vec3(-0.8f, 1.55f, 0), // Lower Arm L
  59. Vec3(0.8f, 1.55f, 0), // Lower Arm R
  60. Vec3(-0.15f, 0.8f, 0), // Upper Leg L
  61. Vec3(0.15f, 0.8f, 0), // Upper Leg R
  62. Vec3(-0.15f, 0.3f, 0), // Lower Leg L
  63. Vec3(0.15f, 0.3f, 0), // Lower Leg R
  64. };
  65. // Rotations of body parts in world space
  66. Quat rotations[] = {
  67. Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), // Lower Body
  68. Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), // Mid Body
  69. Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), // Upper Body
  70. Quat::sIdentity(), // Head
  71. Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), // Upper Arm L
  72. Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), // Upper Arm R
  73. Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), // Lower Arm L
  74. Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), // Lower Arm R
  75. Quat::sIdentity(), // Upper Leg L
  76. Quat::sIdentity(), // Upper Leg R
  77. Quat::sIdentity(), // Lower Leg L
  78. Quat::sIdentity() // Lower Leg R
  79. };
  80. // World space constraint positions
  81. Vec3 constraint_positions[] = {
  82. Vec3::sZero(), // Lower Body (unused, there's no parent)
  83. Vec3(0, 1.25f, 0), // Mid Body
  84. Vec3(0, 1.45f, 0), // Upper Body
  85. Vec3(0, 1.65f, 0), // Head
  86. Vec3(-0.225f, 1.55f, 0), // Upper Arm L
  87. Vec3(0.225f, 1.55f, 0), // Upper Arm R
  88. Vec3(-0.65f, 1.55f, 0), // Lower Arm L
  89. Vec3(0.65f, 1.55f, 0), // Lower Arm R
  90. Vec3(-0.15f, 1.05f, 0), // Upper Leg L
  91. Vec3(0.15f, 1.05f, 0), // Upper Leg R
  92. Vec3(-0.15f, 0.55f, 0), // Lower Leg L
  93. Vec3(0.15f, 0.55f, 0), // Lower Leg R
  94. };
  95. // World space twist axis directions
  96. Vec3 twist_axis[] = {
  97. Vec3::sZero(), // Lower Body (unused, there's no parent)
  98. Vec3::sAxisY(), // Mid Body
  99. Vec3::sAxisY(), // Upper Body
  100. Vec3::sAxisY(), // Head
  101. -Vec3::sAxisX(), // Upper Arm L
  102. Vec3::sAxisX(), // Upper Arm R
  103. -Vec3::sAxisX(), // Lower Arm L
  104. Vec3::sAxisX(), // Lower Arm R
  105. -Vec3::sAxisY(), // Upper Leg L
  106. -Vec3::sAxisY(), // Upper Leg R
  107. -Vec3::sAxisY(), // Lower Leg L
  108. -Vec3::sAxisY(), // Lower Leg R
  109. };
  110. // Constraint limits
  111. float twist_angle[] = {
  112. 0.0f, // Lower Body (unused, there's no parent)
  113. 5.0f, // Mid Body
  114. 5.0f, // Upper Body
  115. 90.0f, // Head
  116. 45.0f, // Upper Arm L
  117. 45.0f, // Upper Arm R
  118. 45.0f, // Lower Arm L
  119. 45.0f, // Lower Arm R
  120. 45.0f, // Upper Leg L
  121. 45.0f, // Upper Leg R
  122. 45.0f, // Lower Leg L
  123. 45.0f, // Lower Leg R
  124. };
  125. float normal_angle[] = {
  126. 0.0f, // Lower Body (unused, there's no parent)
  127. 10.0f, // Mid Body
  128. 10.0f, // Upper Body
  129. 45.0f, // Head
  130. 90.0f, // Upper Arm L
  131. 90.0f, // Upper Arm R
  132. 0.0f, // Lower Arm L
  133. 0.0f, // Lower Arm R
  134. 45.0f, // Upper Leg L
  135. 45.0f, // Upper Leg R
  136. 0.0f, // Lower Leg L
  137. 0.0f, // Lower Leg R
  138. };
  139. float plane_angle[] = {
  140. 0.0f, // Lower Body (unused, there's no parent)
  141. 10.0f, // Mid Body
  142. 10.0f, // Upper Body
  143. 45.0f, // Head
  144. 45.0f, // Upper Arm L
  145. 45.0f, // Upper Arm R
  146. 90.0f, // Lower Arm L
  147. 90.0f, // Lower Arm R
  148. 45.0f, // Upper Leg L
  149. 45.0f, // Upper Leg R
  150. 60.0f, // Lower Leg L (cheating here, a knee is not symmetric, we should have rotated the twist axis)
  151. 60.0f, // Lower Leg R
  152. };
  153. // Create ragdoll settings
  154. Ref<RagdollSettings> settings = new RagdollSettings;
  155. settings->mSkeleton = skeleton;
  156. settings->mParts.resize(skeleton->GetJointCount());
  157. for (int p = 0; p < skeleton->GetJointCount(); ++p)
  158. {
  159. RagdollSettings::Part &part = settings->mParts[p];
  160. part.SetShape(shapes[p]);
  161. part.mPosition = positions[p];
  162. part.mRotation = rotations[p];
  163. part.mMotionType = EMotionType::Dynamic;
  164. part.mObjectLayer = Layers::MOVING;
  165. // First part is the root, doesn't have a parent and doesn't have a constraint
  166. if (p > 0)
  167. {
  168. SwingTwistConstraintSettings *constraint = new SwingTwistConstraintSettings;
  169. constraint->mDrawConstraintSize = 0.1f;
  170. constraint->mPosition1 = constraint->mPosition2 = constraint_positions[p];
  171. constraint->mTwistAxis1 = constraint->mTwistAxis2 = twist_axis[p];
  172. constraint->mPlaneAxis1 = constraint->mPlaneAxis2 = Vec3::sAxisZ();
  173. constraint->mTwistMinAngle = -DegreesToRadians(twist_angle[p]);
  174. constraint->mTwistMaxAngle = DegreesToRadians(twist_angle[p]);
  175. constraint->mNormalHalfConeAngle = DegreesToRadians(normal_angle[p]);
  176. constraint->mPlaneHalfConeAngle = DegreesToRadians(plane_angle[p]);
  177. part.mToParent = constraint;
  178. }
  179. }
  180. // Optional: Stabilize the inertia of the limbs
  181. settings->Stabilize();
  182. // Disable parent child collisions so that we don't get collisions between constrained bodies
  183. settings->DisableParentChildCollisions();
  184. // Create ragdoll
  185. mRagdoll = settings->CreateRagdoll(0, 0, mPhysicsSystem);
  186. mRagdoll->AddToPhysicsSystem(EActivation::Activate);
  187. }