Physics Ragdoll System in Game Development

Introduction

Starting from version 3.1, Godot supports ragdoll physics. Ragdolls relies on physical simulation to create realistic procedural animations. They are used in death animations in many games.

In this tutorial, we will use the Platformer3D demo to set up the rag doll.

note

You can download the Platformer3D demo on GitHub or use the resource library.

Set ragdoll

Create physical bones

Like many other functions in the engine, there is a node to set up a ragdoll: the PhysicalBone node. In order to simplify the settings, you can use PhysicalBone to generate a node with the function of "creating a physical skeleton" in the skeleton node.

Open the platform demo in Godot and then open it in Robi scene. Select the Skeleton node. The skeleton button appears on the top bar menu:

../../_images/ragdoll_menu.png

Click it and select an option. Godot will generate nodes and collision shapes for each bone and pin joint in the bone to connect them together: Create physical skeletonPhysicalBone

../../_images/ragdoll_bones.png

Certain generated bones are not necessary: ​​MASTER such as bones. Therefore, we will clean up the skeleton by removing it.

Clean up the skeleton

Every Engine that PhysicalBone needs to simulate has a performance cost, so you want to delete all bones that are too small to work in the simulation and all practical bones.

For example, if we use humanoid creatures, you don’t want every finger to have bones. You can use one bone instead of the whole hand, or one bone for the palm, one bone for the thumb, and the last bone for the other four fingers.

Delete these physical bones MASTER, waist, neck, and headtracker. This provides us with an optimized skeleton and makes it easier to control the ragdoll.

Collision shape adjustment

The next task is to adjust the collision shape and the size of the physical bones to match the body parts that each bone should simulate.

Insert picture description here

Joint adjustment

After adjusting the collision shape, the rag doll is almost ready. You only need to adjust the pin connector to get a better simulation effect. PhysicalBone nodes are assigned unconstrained pin joints by default. To change the pin joint, select PhysicalBone and change the constraint type in the Joint section. There, you can change the direction of the constraints and their limits.

Insert picture description here

This is the final result:

../../_images/ragdoll_result.png

Rag doll

Now you can use the ragdoll. To start the simulation and play the ragdoll animation, you need to call this physical_bones_start_simulationmethod. Attach the script to the skeleton node, and then call the _readymethod in the method:

func _ready():
    physical_bones_start_simulation()

To stop the simulation, call this physical_bones_stop_simulation()method.

Insert picture description here

You can also limit the simulation to only a few bones. To do this, pass the bone name as a parameter. This is an example of part of the ragdoll simulation:

Insert picture description here

Collision layer and mask

Make sure to set the collision layer and mask correctly so that the KinematicBody capsule does not interfere with the physical simulation:

../../_images/ragdoll_layer.png

For more information, read about collision layers and masks .

Guess you like

Origin blog.csdn.net/qq_44273429/article/details/111593067