RagdollLoader.cpp 11 KB

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