Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

布娃娃系统

前言

Godot 从 3.1 版本开始支持布娃娃物理。布娃娃依靠物理模拟来创建逼真的程序式动画,它们被用于许多游戏中的死亡动画。

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

备注

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

设置布娃娃

创建物理骨骼

Like many other features in the engine, there is a node to set up a ragdoll: the PhysicalBone3D node. To simplify the setup, you can generate PhysicalBone nodes with the "Create physical skeleton" feature in the skeleton node.

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

../../_images/ragdoll_menu.png

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

../../_images/ragdoll_bones.png

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

清理骨骼

引擎需要模拟的每一个 PhysicalBone 都有性能成本, 所以你要把每一个太小的骨头都去掉, 以便所有的实用骨头在模拟中发挥作用.

例如, 如果我们拿一个人形动物来说, 不希望每个手指都有物理骨骼. 可以用一根骨头代替整个手, 或者用一根骨头代替手掌, 一根骨头代替拇指, 最后一根骨头代替其他四个手指.

删除这些物理骨骼:MASTERwaistneckheadtracker。这样就有了一个优化的骨架,使其更容易控制布娃娃。

碰撞形状调整

接下来的任务是调整碰撞形状和物理骨骼的大小, 以匹配每个骨骼应该模拟的身体部位.

../../_images/ragdoll_shape_adjust.gif

关节调整

一旦你调整了碰撞形状,布娃娃就差不多准备好了。只需要调整钉关节以获得更好的模拟效果。PhysicalBone 节点在默认情况下有一个不受限制的钉关节。要改变钉关节,请选择 PhysicalBone 并在 Joint 部分改变约束类型。在那里,你可以改变约束的方向和限制。

../../_images/ragdoll_joint_adjust.gif

这是最终效果:

../../_images/ragdoll_result.png

模拟布娃娃

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

func _ready():
    physical_bones_start_simulation()

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

../../_images/ragdoll_sim_stop.gif

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

../../_images/ragdoll_sim_part.gif

碰撞层与遮罩

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:

../../_images/ragdoll_layer.png

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