RagdollLoader.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Utils/RagdollLoader.h>
  6. #include <Jolt/Physics/Ragdoll/Ragdoll.h>
  7. #include <Jolt/Physics/Constraints/PointConstraint.h>
  8. #include <Jolt/Physics/Constraints/FixedConstraint.h>
  9. #include <Jolt/Physics/Constraints/HingeConstraint.h>
  10. #include <Jolt/Physics/Constraints/SliderConstraint.h>
  11. #include <Jolt/Physics/Constraints/ConeConstraint.h>
  12. #include <Jolt/Physics/Constraints/SwingTwistConstraint.h>
  13. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  14. #include <Jolt/ObjectStream/ObjectStreamIn.h>
  15. #include <Jolt/ObjectStream/ObjectStreamOut.h>
  16. #include <Layers.h>
  17. #include <Utils/Log.h>
  18. #ifdef JPH_OBJECT_STREAM
  19. RagdollSettings *RagdollLoader::sLoad(const char *inFileName, EMotionType inMotionType, EConstraintOverride inConstraintOverride)
  20. {
  21. // Read the ragdoll
  22. RagdollSettings *ragdoll = nullptr;
  23. if (!ObjectStreamIn::sReadObject(inFileName, ragdoll))
  24. FatalError("Unable to read ragdoll");
  25. for (RagdollSettings::Part &p : ragdoll->mParts)
  26. {
  27. // Update motion type
  28. p.mMotionType = inMotionType;
  29. // Override layer
  30. p.mObjectLayer = Layers::MOVING;
  31. // Create new constraint
  32. Ref<SwingTwistConstraintSettings> original = DynamicCast<SwingTwistConstraintSettings>(p.mToParent);
  33. if (original != nullptr)
  34. switch (inConstraintOverride)
  35. {
  36. case EConstraintOverride::TypeFixed:
  37. {
  38. FixedConstraintSettings *settings = new FixedConstraintSettings();
  39. settings->mPoint1 = settings->mPoint2 = original->mPosition1;
  40. p.mToParent = settings;
  41. break;
  42. }
  43. case EConstraintOverride::TypePoint:
  44. {
  45. PointConstraintSettings *settings = new PointConstraintSettings();
  46. settings->mPoint1 = settings->mPoint2 = original->mPosition1;
  47. p.mToParent = settings;
  48. break;
  49. }
  50. case EConstraintOverride::TypeHinge:
  51. {
  52. HingeConstraintSettings *settings = new HingeConstraintSettings();
  53. settings->mPoint1 = original->mPosition1;
  54. settings->mHingeAxis1 = original->mPlaneAxis1;
  55. settings->mNormalAxis1 = original->mTwistAxis1;
  56. settings->mPoint2 = original->mPosition2;
  57. settings->mHingeAxis2 = original->mPlaneAxis2;
  58. settings->mNormalAxis2 = original->mTwistAxis2;
  59. settings->mLimitsMin = -original->mNormalHalfConeAngle;
  60. settings->mLimitsMax = original->mNormalHalfConeAngle;
  61. settings->mMaxFrictionTorque = original->mMaxFrictionTorque;
  62. p.mToParent = settings;
  63. break;
  64. }
  65. case EConstraintOverride::TypeSlider:
  66. {
  67. SliderConstraintSettings *settings = new SliderConstraintSettings();
  68. settings->mPoint1 = settings->mPoint2 = original->mPosition1;
  69. settings->mSliderAxis1 = settings->mSliderAxis2 = original->mTwistAxis1;
  70. settings->mNormalAxis1 = settings->mNormalAxis2 = original->mTwistAxis1.GetNormalizedPerpendicular();
  71. settings->mLimitsMin = -1.0f;
  72. settings->mLimitsMax = 1.0f;
  73. settings->mMaxFrictionForce = original->mMaxFrictionTorque;
  74. p.mToParent = settings;
  75. break;
  76. }
  77. case EConstraintOverride::TypeCone:
  78. {
  79. ConeConstraintSettings *settings = new ConeConstraintSettings();
  80. settings->mPoint1 = original->mPosition1;
  81. settings->mTwistAxis1 = original->mTwistAxis1;
  82. settings->mPoint2 = original->mPosition2;
  83. settings->mTwistAxis2 = original->mTwistAxis2;
  84. settings->mHalfConeAngle = original->mNormalHalfConeAngle;
  85. p.mToParent = settings;
  86. break;
  87. }
  88. case EConstraintOverride::TypeRagdoll:
  89. break;
  90. }
  91. }
  92. // Initialize the skeleton
  93. ragdoll->GetSkeleton()->CalculateParentJointIndices();
  94. // Stabilize the constraints of the ragdoll
  95. ragdoll->Stabilize();
  96. // Calculate body <-> constraint map
  97. ragdoll->CalculateBodyIndexToConstraintIndex();
  98. ragdoll->CalculateConstraintIndexToBodyIdxPair();
  99. return ragdoll;
  100. }
  101. #endif // JPH_OBJECT_STREAM
  102. RagdollSettings *RagdollLoader::sCreate()
  103. {
  104. // Create skeleton
  105. Ref<Skeleton> skeleton = new Skeleton;
  106. uint lower_body = skeleton->AddJoint("LowerBody");
  107. uint mid_body = skeleton->AddJoint("MidBody", lower_body);
  108. uint upper_body = skeleton->AddJoint("UpperBody", mid_body);
  109. /*uint head =*/ skeleton->AddJoint("Head", upper_body);
  110. uint upper_arm_l = skeleton->AddJoint("UpperArmL", upper_body);
  111. uint upper_arm_r = skeleton->AddJoint("UpperArmR", upper_body);
  112. /*uint lower_arm_l =*/ skeleton->AddJoint("LowerArmL", upper_arm_l);
  113. /*uint lower_arm_r =*/ skeleton->AddJoint("LowerArmR", upper_arm_r);
  114. uint upper_leg_l = skeleton->AddJoint("UpperLegL", lower_body);
  115. uint upper_leg_r = skeleton->AddJoint("UpperLegR", lower_body);
  116. /*uint lower_leg_l =*/ skeleton->AddJoint("LowerLegL", upper_leg_l);
  117. /*uint lower_leg_r =*/ skeleton->AddJoint("LowerLegR", upper_leg_r);
  118. // Create shapes for limbs
  119. Ref<Shape> shapes[] = {
  120. new CapsuleShape(0.15f, 0.10f), // Lower Body
  121. new CapsuleShape(0.15f, 0.10f), // Mid Body
  122. new CapsuleShape(0.15f, 0.10f), // Upper Body
  123. new CapsuleShape(0.075f, 0.10f), // Head
  124. new CapsuleShape(0.15f, 0.06f), // Upper Arm L
  125. new CapsuleShape(0.15f, 0.06f), // Upper Arm R
  126. new CapsuleShape(0.15f, 0.05f), // Lower Arm L
  127. new CapsuleShape(0.15f, 0.05f), // Lower Arm R
  128. new CapsuleShape(0.2f, 0.075f), // Upper Leg L
  129. new CapsuleShape(0.2f, 0.075f), // Upper Leg R
  130. new CapsuleShape(0.2f, 0.06f), // Lower Leg L
  131. new CapsuleShape(0.2f, 0.06f), // Lower Leg R
  132. };
  133. // Positions of body parts in world space
  134. RVec3 positions[] = {
  135. RVec3(0, 1.15f, 0), // Lower Body
  136. RVec3(0, 1.35f, 0), // Mid Body
  137. RVec3(0, 1.55f, 0), // Upper Body
  138. RVec3(0, 1.825f, 0), // Head
  139. RVec3(-0.425f, 1.55f, 0), // Upper Arm L
  140. RVec3(0.425f, 1.55f, 0), // Upper Arm R
  141. RVec3(-0.8f, 1.55f, 0), // Lower Arm L
  142. RVec3(0.8f, 1.55f, 0), // Lower Arm R
  143. RVec3(-0.15f, 0.8f, 0), // Upper Leg L
  144. RVec3(0.15f, 0.8f, 0), // Upper Leg R
  145. RVec3(-0.15f, 0.3f, 0), // Lower Leg L
  146. RVec3(0.15f, 0.3f, 0), // Lower Leg R
  147. };
  148. // Rotations of body parts in world space
  149. Quat rotations[] = {
  150. Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), // Lower Body
  151. Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), // Mid Body
  152. Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), // Upper Body
  153. Quat::sIdentity(), // Head
  154. Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), // Upper Arm L
  155. Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), // Upper Arm R
  156. Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), // Lower Arm L
  157. Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), // Lower Arm R
  158. Quat::sIdentity(), // Upper Leg L
  159. Quat::sIdentity(), // Upper Leg R
  160. Quat::sIdentity(), // Lower Leg L
  161. Quat::sIdentity() // Lower Leg R
  162. };
  163. // World space constraint positions
  164. RVec3 constraint_positions[] = {
  165. RVec3::sZero(), // Lower Body (unused, there's no parent)
  166. RVec3(0, 1.25f, 0), // Mid Body
  167. RVec3(0, 1.45f, 0), // Upper Body
  168. RVec3(0, 1.65f, 0), // Head
  169. RVec3(-0.225f, 1.55f, 0), // Upper Arm L
  170. RVec3(0.225f, 1.55f, 0), // Upper Arm R
  171. RVec3(-0.65f, 1.55f, 0), // Lower Arm L
  172. RVec3(0.65f, 1.55f, 0), // Lower Arm R
  173. RVec3(-0.15f, 1.05f, 0), // Upper Leg L
  174. RVec3(0.15f, 1.05f, 0), // Upper Leg R
  175. RVec3(-0.15f, 0.55f, 0), // Lower Leg L
  176. RVec3(0.15f, 0.55f, 0), // Lower Leg R
  177. };
  178. // World space twist axis directions
  179. Vec3 twist_axis[] = {
  180. Vec3::sZero(), // Lower Body (unused, there's no parent)
  181. Vec3::sAxisY(), // Mid Body
  182. Vec3::sAxisY(), // Upper Body
  183. Vec3::sAxisY(), // Head
  184. -Vec3::sAxisX(), // Upper Arm L
  185. Vec3::sAxisX(), // Upper Arm R
  186. -Vec3::sAxisX(), // Lower Arm L
  187. Vec3::sAxisX(), // Lower Arm R
  188. -Vec3::sAxisY(), // Upper Leg L
  189. -Vec3::sAxisY(), // Upper Leg R
  190. -Vec3::sAxisY(), // Lower Leg L
  191. -Vec3::sAxisY(), // Lower Leg R
  192. };
  193. // Constraint limits
  194. float twist_angle[] = {
  195. 0.0f, // Lower Body (unused, there's no parent)
  196. 5.0f, // Mid Body
  197. 5.0f, // Upper Body
  198. 90.0f, // Head
  199. 45.0f, // Upper Arm L
  200. 45.0f, // Upper Arm R
  201. 45.0f, // Lower Arm L
  202. 45.0f, // Lower Arm R
  203. 45.0f, // Upper Leg L
  204. 45.0f, // Upper Leg R
  205. 45.0f, // Lower Leg L
  206. 45.0f, // Lower Leg R
  207. };
  208. float normal_angle[] = {
  209. 0.0f, // Lower Body (unused, there's no parent)
  210. 10.0f, // Mid Body
  211. 10.0f, // Upper Body
  212. 45.0f, // Head
  213. 90.0f, // Upper Arm L
  214. 90.0f, // Upper Arm R
  215. 0.0f, // Lower Arm L
  216. 0.0f, // Lower Arm R
  217. 45.0f, // Upper Leg L
  218. 45.0f, // Upper Leg R
  219. 0.0f, // Lower Leg L
  220. 0.0f, // Lower Leg R
  221. };
  222. float plane_angle[] = {
  223. 0.0f, // Lower Body (unused, there's no parent)
  224. 10.0f, // Mid Body
  225. 10.0f, // Upper Body
  226. 45.0f, // Head
  227. 45.0f, // Upper Arm L
  228. 45.0f, // Upper Arm R
  229. 90.0f, // Lower Arm L
  230. 90.0f, // Lower Arm R
  231. 45.0f, // Upper Leg L
  232. 45.0f, // Upper Leg R
  233. 60.0f, // Lower Leg L (cheating here, a knee is not symmetric, we should have rotated the twist axis)
  234. 60.0f, // Lower Leg R
  235. };
  236. // Create ragdoll settings
  237. RagdollSettings *settings = new RagdollSettings;
  238. settings->mSkeleton = skeleton;
  239. settings->mParts.resize(skeleton->GetJointCount());
  240. for (int p = 0; p < skeleton->GetJointCount(); ++p)
  241. {
  242. RagdollSettings::Part &part = settings->mParts[p];
  243. part.SetShape(shapes[p]);
  244. part.mPosition = positions[p];
  245. part.mRotation = rotations[p];
  246. part.mMotionType = EMotionType::Dynamic;
  247. part.mObjectLayer = Layers::MOVING;
  248. // First part is the root, doesn't have a parent and doesn't have a constraint
  249. if (p > 0)
  250. {
  251. SwingTwistConstraintSettings *constraint = new SwingTwistConstraintSettings;
  252. constraint->mDrawConstraintSize = 0.1f;
  253. constraint->mPosition1 = constraint->mPosition2 = constraint_positions[p];
  254. constraint->mTwistAxis1 = constraint->mTwistAxis2 = twist_axis[p];
  255. constraint->mPlaneAxis1 = constraint->mPlaneAxis2 = Vec3::sAxisZ();
  256. constraint->mTwistMinAngle = -DegreesToRadians(twist_angle[p]);
  257. constraint->mTwistMaxAngle = DegreesToRadians(twist_angle[p]);
  258. constraint->mNormalHalfConeAngle = DegreesToRadians(normal_angle[p]);
  259. constraint->mPlaneHalfConeAngle = DegreesToRadians(plane_angle[p]);
  260. part.mToParent = constraint;
  261. }
  262. }
  263. // Optional: Stabilize the inertia of the limbs
  264. settings->Stabilize();
  265. // Disable parent child collisions so that we don't get collisions between constrained bodies
  266. settings->DisableParentChildCollisions();
  267. // Calculate the map needed for GetBodyIndexToConstraintIndex()
  268. settings->CalculateBodyIndexToConstraintIndex();
  269. return settings;
  270. }