Ragdoll system
Introduction
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. In this tutorial, we will be using the Platformer3D demo to set up a ragdoll. Note GitHub or using the Asset Library.
Setting up the ragdoll
Creating physical bones
PhysicalBone node. To simplify the setup, you can generate PhysicalBone nodes with the “Create physical skeleton” feature in the skeleton node.
Skeleton node. A skeleton button appears on the top bar menu:
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:
MASTER bone for example. So we’re going to clean up the skeleton by removing them.
Cleaning up the skeleton
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.
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.
MASTER, waist, neck, headtracker. This gives us an optimized skeleton and makes it easier to control the ragdoll.
Collision shape adjustment
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.
Joints adjustment
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.
This is the final result:
Simulating the ragdoll
physical_bones_start_simulation method. Attach a script to the skeleton node and call the method in the _ready method:
GDScript
func _ready(): physical_bones_start_simulation()
physical_bones_stop_simulation() method.
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:
Collision layer and mask
KinematicBody‘s capsule doesn’t get in the way of the physics simulation:
Collision layers and masks.
