ragdoll_system.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. .. _doc_ragdoll_system:
  2. Ragdoll system
  3. ==============
  4. Introduction
  5. ------------
  6. Godot supports ragdoll physics. Ragdolls rely on physics simulation to create
  7. realistic procedural animation. They are used for death animations in many games.
  8. In this tutorial, we will be using the Platformer 3D demo to set up a ragdoll.
  9. .. note::
  10. You can download the Platformer 3D demo on
  11. `GitHub <https://github.com/godotengine/godot-demo-projects/tree/master/3d/platformer>`_
  12. or using the `Asset Library <https://godotengine.org/asset-library/asset/2748>`_.
  13. Setting up the ragdoll
  14. ----------------------
  15. Creating physical bones
  16. ~~~~~~~~~~~~~~~~~~~~~~~
  17. Like many other features in the engine, there are two nodes which are used
  18. to set up a ragdoll:
  19. - A :ref:`PhysicalBoneSimulator3D <class_PhysicalBoneSimulator3D>` node.
  20. This node is the parent of all physical bones and is responsible
  21. for controlling the simulation.
  22. - One or more :ref:`PhysicalBone3D <class_PhysicalBone3D>` children.
  23. Each node represents a single bone in the ragdoll.
  24. Open the platformer demo in Godot, and then the ``player/player.tscn`` scene.
  25. Select the ``Skeleton3D`` node. A skeleton button appears at the top of the
  26. 3D editor viewport:
  27. .. figure:: img/ragdoll_system_create_physical_skeleton.webp
  28. :align: center
  29. :alt: Creating a physical skeleton in the editor
  30. Creating a physical skeleton in the editor
  31. Click it and select the :menu:`Create Physical Skeleton` option. Godot will generate
  32. PhysicalBone3D nodes and collision shapes for each bone in the skeleton and
  33. pin joints to connect them together:
  34. .. figure:: img/ragdoll_system_skeleton_scene_tree.webp
  35. :align: center
  36. :alt: Scene tree of the player scene after creating a physical skeleton
  37. Scene tree of the player scene after creating a physical skeleton
  38. Some of the generated bones aren't necessary, such as the ``MASTER`` bone in this scene.
  39. We're going to clean up the skeleton by removing them.
  40. Clean up and optimize the skeleton
  41. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  42. For each PhysicalBone3D the engine needs to simulate, there is a performance cost.
  43. You'll want to remove every bone that is too small to make a difference in the simulation,
  44. as well as all utility bones.
  45. For example, if we take a humanoid, you don't need to have physical bones for each finger.
  46. You can use a single bone for the entire hand instead, or one for the palm, one for the thumb,
  47. and a last one for the other four fingers.
  48. Remove these PhysicalBone3D nodes: ``MASTER``, ``waist``, ``neck``, ``headtracker``.
  49. This gives us an optimized skeleton and makes it easier to control the ragdoll.
  50. Adjust joints and constraints
  51. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  52. Once you adjusted the collision shapes, your ragdoll is almost ready. Now, you need
  53. to adjust the pin joints to get a better simulation. PhysicalBone3D nodes have an
  54. unconstrained pin joint assigned to them by default. To change the pin joint,
  55. select a PhysicalBone3D node and change the constraint type in the :menu:`Joint` section
  56. of the inspector. There, you can change the constraint's orientation and its limits.
  57. Joints have a gizmo visible in the 3D editor as well, so you can see their constraints
  58. in action.
  59. .. figure:: img/ragdoll_system_adjust_joints_inspector.webp
  60. :align: center
  61. :alt: Adjusting joints in the inspector after selecting a PhysicalBone3D node
  62. Adjusting joints in the inspector after selecting a PhysicalBone3D node
  63. .. tip::
  64. To get a better view when editing joints and collision shapes, you can do the following:
  65. - Hide PhysicalBone3D nodes you aren't currently working on, so you can focus
  66. on the ones you're adjusting.
  67. - Hide the MeshInstance3D of the character by clicking the eye icon next to it
  68. in the scene tree dock.
  69. - Hide the Skeleton3D gizmos, so that the orange triangles that represent the skeleton
  70. don't clutter the viewport while leaving the rest visible.
  71. To do so, click :menu:`View > Gizmos > Skeleton3D` at the top of the 3D editor
  72. viewport until the eye icon appears closed.
  73. - Disable the preview environment by clicking the globe icon at the top of
  74. the 3D editor viewport.
  75. - Set the **Default Clear Color** project setting to pure black in the Project Settings.
  76. This is only effective if the preview environment is disabled.
  77. - Change the debug draw mode using the :menu:`Perspective` button
  78. in the top-left corner of the 3D editor viewport. The :menu:`Display Wireframe`
  79. and :menu:`Display Overdraw` options are particularly useful when adjusting
  80. collision shapes, as they allow you to see through the original mesh.
  81. - Use the orthographic camera by clicking the :button:`X`/:button:`Y`/:button:`Z`
  82. buttons in the top-right corner of the 3D editor viewport.
  83. Here is the list of joints available:
  84. - **None:** Does not perform any constraint.
  85. - **ConeJoint:** Ball-and-socket. Useful for shoulders, hips, neck.
  86. - **HingeJoint:** Provides an angular constraint; think of it like a door hinge.
  87. Useful for elbows and knees.
  88. - **PinJoint:** Keeps two bodies connected *(default)*.
  89. Leads to "crumpling" of the bones, so it's recommended to use other joint types
  90. for most characters instead.
  91. - **SliderJoint:** Slides one bone along another on a specific axis.
  92. - **6DOFJoint:** Most powerful joint, offering both linear and angular constraints,
  93. but also the most complex to configure.
  94. If in doubt, start with HingeJoint and ConeJoint, as they cover most use cases:
  95. - For HingeJoint, make sure to enable **Angular Limit** in the
  96. :menu:`Joint Constraints` section of the inspector. After enabling it,
  97. you can see the angle that it's being constrained to in the viewport.
  98. You can rotate the PhysicalBone3D to change the axis where the joint
  99. is constrained, then adjust the angles.
  100. - For ConeJoint, it's usually best to limit **Swing Span** between 20 and
  101. 90 degrees, and the **Twist Span** between 20 and 45 degrees.
  102. Adjust collision shapes
  103. ~~~~~~~~~~~~~~~~~~~~~~~
  104. The next task is adjusting the collision shape and the size of the physical bones
  105. to match the part of the body that each bone should simulate.
  106. It's recommended to adjust collision shapes *after* adjusting joints and constraints,
  107. as rotating a joint will also rotate the collision shape. To avoid having
  108. to adjust collision shapes twice, it's better to adjust joints first.
  109. Note that it's possible to have multiple collision shapes as a child of a
  110. PhysicalBone3D node. This can be useful to represent particularly complex
  111. shapes of limbs that are otherwise rigid.
  112. .. tip::
  113. To pause animation playback while adjusting the ragdoll, select the
  114. ``AnimationTree`` node and disable the **Active** property in the Inspector.
  115. Remember to enable it again when you're done, as it controls animation playback
  116. during gameplay.
  117. .. figure:: img/ragdoll_system_adjust_collision_shapes.webp
  118. :align: center
  119. :alt: Adjusting collision shapes in the 3D editor
  120. Adjusting collision shapes in the 3D editor
  121. This is the final result:
  122. .. figure:: img/ragdoll_system_result.webp
  123. :align: center
  124. :alt: Result after adjusting joints and collision shapes (player mesh is hidden for visibility)
  125. Result after adjusting joints and collision shapes (player mesh is hidden for visibility)
  126. Simulate the ragdoll
  127. --------------------
  128. The ragdoll is now ready to use. To start the simulation and play the ragdoll animation,
  129. you need to call the
  130. :ref:`PhysicalBoneSimulator3D.physical_bones_start_simulation() <class_PhysicalBoneSimulator3D_method_physical_bones_start_simulation>`
  131. method. Attach a script to the :ref:`PhysicalBoneSimulator3D <class_PhysicalBoneSimulator3D>` node
  132. that is the parent of all the PhysicalBone3D nodes in our scene, then call it in the script's
  133. ``_ready`` method:
  134. .. tabs::
  135. .. code-tab:: gdscript GDScript
  136. func _ready():
  137. physical_bones_start_simulation()
  138. .. code-tab:: csharp
  139. public override void _Ready()
  140. {
  141. PhysicalBonesStartSimulation();
  142. }
  143. To stop the simulation, call the
  144. :ref:`PhysicalBoneSimulator3D.physical_bones_stop_simulation() <class_PhysicalBoneSimulator3D_method_physical_bones_stop_simulation>`
  145. method.
  146. .. video:: video/ragdoll_system_full_simulation.webm
  147. :alt: Full simulation of ragdoll system, with the player falling to the ground
  148. :autoplay:
  149. :loop:
  150. :muted:
  151. :align: default
  152. :width: 100%
  153. You can also limit the simulation to only a few bones. This can be useful
  154. to create effects such as ragdoll limbs or attachments that can interact
  155. with the world. To do so, pass the bone names (*not* the PhysicalBone3D
  156. node names) as a parameter. To see the bone name, look at the
  157. **Bone Name** property in the inspector after selecting a PhysicalBone3D node.
  158. .. tip::
  159. When using an automatically generated physical skeleton as shown in this tutorial,
  160. the bone name is also contained in the node name. For example, in
  161. ``Physical Bone l-arm``, ``l-arm`` is the bone name.
  162. .. tabs::
  163. .. code-tab:: gdscript GDScript
  164. func _ready():
  165. physical_bones_start_simulation(["l-arm", "r-arm"])
  166. .. code-tab:: csharp
  167. public override void _Ready()
  168. {
  169. PhysicalBonesStartSimulation(["l-arm", "r-arm"]);
  170. }
  171. Note that nonexistent bone names will not print any error or warning. If
  172. nothing happens when starting the simulation (or if the whole body is ragdolled
  173. instead of only specific bones), double-check the list of provided bones.
  174. Here's an example of partial ragdoll simulation:
  175. .. video:: video/ragdoll_system_partial_simulation.webm
  176. :alt: Partial simulation of ragdoll system, with arms flailing while the player is walking
  177. :autoplay:
  178. :loop:
  179. :muted:
  180. :align: default
  181. :width: 100%
  182. .. tip::
  183. To control how strongly the partial ragdoll simulation affects the overall animation,
  184. you can adjust the **Influence** property in the
  185. :ref:`PhysicalBoneSimulator3D <class_PhysicalBoneSimulator3D>` node that is the
  186. parent of all PhysicalBone3D nodes. By default, it's set to ``1.0``, which means
  187. the ragdoll simulation fully overrides the rest of the animation.
  188. Collision layer and mask
  189. ~~~~~~~~~~~~~~~~~~~~~~~~
  190. Make sure to set up your collision layers and masks properly so the
  191. CharacterBody3D's capsule doesn't get in the way of the physics simulation.
  192. Remember to adjust the collision layer and mask in the coin scene
  193. as well, so that the player can still collect coins:
  194. .. figure:: img/ragdoll_system_collision_layers_masks.webp
  195. :align: center
  196. :alt: Layers and masks must be adjusted to these values in the inspector for each node
  197. Layers and masks must be adjusted to these values in the inspector for each node
  198. You can find the GridMap in the 3D platformer demo in ``stage/grid_map.scn``.
  199. The coin's Area3D node (on which the layers and masks must be adjusted)
  200. can be found at ``coin/coin.tscn``.
  201. If this is not done, collision will behave incorrectly as the player will collide
  202. with its own (inactive) ragdoll. This can cause the player to wildly
  203. bounce around or get stuck.
  204. Like RigidBody3D, PhysicalBone3D supports collision exceptions through code
  205. using the :ref:`physical_bones_add_collision_exception() <class_PhysicalBoneSimulator3D_method_physical_bones_add_collision_exception>`
  206. and :ref:`physical_bones_remove_collision_exception() <class_PhysicalBoneSimulator3D_method_physical_bones_remove_collision_exception>`
  207. methods. This can be used to prevent collisions with a specific object
  208. without relying on layers and masks.
  209. .. seealso::
  210. For more information, see :ref:`doc_physics_introduction_collision_layers_and_masks`.