ragdoll_system.rst 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. .. _doc_ragdoll_system:
  2. Ragdoll system
  3. ==============
  4. Introduction
  5. ------------
  6. Since version 3.1, Godot supports ragdoll physics. Ragdolls rely on physics simulation to create realistic procedural animation. They are used for death animations in many games.
  7. In this tutorial, we will be using the Platformer3D demo to set up a ragdoll.
  8. .. note:: You can download the Platformer3D demo on `GitHub <https://github.com/godotengine/godot-demo-projects/tree/master/3d/platformer>`_ or using the `Asset Library <https://godotengine.org/asset-library/asset/2748>`_.
  9. Setting up the ragdoll
  10. ----------------------
  11. Creating physical bones
  12. ~~~~~~~~~~~~~~~~~~~~~~~
  13. Like many other features in the engine, there is a node to set up a ragdoll: the :ref:`PhysicalBone3D <class_PhysicalBone3D>` node. To simplify the setup, you can generate ``PhysicalBone`` nodes with the "Create physical skeleton" feature in the skeleton node.
  14. Open the platformer demo in Godot, and then the Robi scene. Select the ``Skeleton`` node. A skeleton button appears on the top bar menu:
  15. .. image:: img/ragdoll_menu.png
  16. Click it and select the ``Create physical skeleton`` option. Godot will generate ``PhysicalBone`` nodes and collision shapes for each bone in the skeleton and pin joints to connect them together:
  17. .. image:: img/ragdoll_bones.png
  18. Some of the generated bones aren't necessary: the ``MASTER`` bone for example. So we're going to clean up the skeleton by removing them.
  19. Cleaning up the skeleton
  20. ~~~~~~~~~~~~~~~~~~~~~~~~
  21. Each ``PhysicalBone`` the engine needs to simulate has a performance cost, so you want to remove every bone that is too small to make a difference in the simulation, as well as all utility bones.
  22. For example, if we take a humanoid, you do not want to have physical bones for each finger. You can use a single bone for the entire hand instead, or one for the palm, one for the thumb, and a last one for the other four fingers.
  23. Remove these physical bones: ``MASTER``, ``waist``, ``neck``, ``headtracker``. This gives us an optimized skeleton and makes it easier to control the ragdoll.
  24. Collision shape adjustment
  25. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  26. The next task is adjusting the collision shape and the size of physical bones to match the part of the body that each bone should simulate.
  27. .. image:: img/ragdoll_shape_adjust.gif
  28. Joints adjustment
  29. ~~~~~~~~~~~~~~~~~
  30. Once you adjusted the collision shapes, your ragdoll is almost ready. You just want to adjust the pin joints to get a better simulation. ``PhysicalBone`` nodes have an unconstrained pin joint assigned to them by default. To change the pin joint, select the ``PhysicalBone`` and change the constraint type in the ``Joint`` section. There, you can change the constraint's orientation and its limits.
  31. .. image:: img/ragdoll_joint_adjust.gif
  32. This is the final result:
  33. .. image:: img/ragdoll_result.png
  34. Simulating the ragdoll
  35. ----------------------
  36. The ragdoll is now ready to use. To start the simulation and play the ragdoll animation, you need to call the ``physical_bones_start_simulation`` method. Attach a script to the skeleton node and call the method in the ``_ready`` method:
  37. .. tabs::
  38. .. code-tab:: gdscript GDScript
  39. func _ready():
  40. physical_bones_start_simulation()
  41. .. code-tab:: csharp
  42. public override void _Ready()
  43. {
  44. PhysicalBonesStartSimulation();
  45. }
  46. To stop the simulation, call the ``physical_bones_stop_simulation()`` method.
  47. .. image:: img/ragdoll_sim_stop.gif
  48. You can also limit the simulation to only a few bones. To do so, pass the bone names as a parameter. Here's an example of partial ragdoll simulation:
  49. .. image:: img/ ragdoll_sim_part.gif
  50. Collision layer and mask
  51. ~~~~~~~~~~~~~~~~~~~~~~~~
  52. Make sure to set up your collision layers and masks properly so the ``CharacterBody3D``'s capsule doesn't get in the way of the physics simulation:
  53. .. image:: img/ragdoll_layer.png
  54. For more information, read :ref:`doc_physics_introduction_collision_layers_and_masks`.