ragdoll_system.rst 12 KB

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