RagdollLoader.cpp 10 KB

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