布娃娃系统

简介

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.

在本教程中,我们将使用Platformer3D演示来设置布娃娃。

注解

您可以在 GitHub 上下载Platformer3D演示或使用 Asset Library

设置布娃娃

创建物理骨骼

与引擎中的许多其他功能一样,有一个节点可以设置一个布娃娃: PhysicalBone 节点。 为了简化设置,您可以使用骨架节点中的“创建物理骨架”功能生成 PhysicalBone 节点。

在Godot中打开平台演示,然后在Robi场景中打开。 选择 Skeleton 节点。 顶部栏菜单上显示骨架按钮:

../../_images/ragdoll_menu.png

单击它并选择 创建物理骨架 选项。 Godot将为骨架中的每个骨骼生成 PhysicalBone 节点和碰撞形状,并将针脚连接在一起以将它们连接在一起:

../../_images/ragdoll_bones.png

一些生成的骨骼不是必需的:例如 MASTER 骨骼。 因此,我们将通过删除它们来清理骨架。

清理骨骼

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.

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.

Remove these physical bones: MASTER, waist, neck, headtracker. This gives us an optimized skeleton and makes it easier to control the ragdoll.

碰撞形状调整

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.

../../_images/ragdoll_shape_adjust.gif

关节调整

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.

../../_images/ragdoll_joint_adjust.gif

这是最终效果:

../../_images/ragdoll_result.png

模拟布娃娃

布娃娃现在可以使用了。 要开始模拟并播放布娃娃动画,您需要调用 physical_bones_start_simulation 方法。 将脚本附加到骨架节点并在 _ready 方法中调用该方法:

GDScript

  1. func _ready():
  2. physical_bones_start_simulation()

要停止模拟,请调用 physical_bones_stop_simulation() 方法。

../../_images/ragdoll_sim_stop.gif

您还可以将模拟限制为仅几个骨骼。 为此,请将骨骼名称作为参数传递。 这是部分布娃娃模拟的一个示例:

../../_images/ragdoll_sim_part.gif

碰撞层与遮罩

确保正确设置碰撞层和遮罩,这样“KinematicBody”的胶囊不会妨碍物理模拟:

../../_images/ragdoll_layer.png

有关更多信息,请阅读 物理介绍