Up to date

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

碰撞形状(3D)

本指南解释:

  • Godot 3D 中可用的碰撞形状类型。

  • 使用凸形或凹形网格作为碰撞形状.

  • 有关3D碰撞的性能注意事项.

Godot提供多种碰撞形状, 具有不同的性能和精度权衡.

You can define the shape of a PhysicsBody3D by adding one or more CollisionShape3Ds as child nodes. Note that you must add a Shape3D resource to collision shape nodes in the Inspector dock.

备注

当你将多个碰撞形状添加到一个PhysicsBody中时, 你不必担心它们会重叠, 它们不会相互 "碰撞".

基本碰撞形状

Godot提供了以下基本碰撞形状类型:

你可以使用一个或多个原始形状来表示大多数较小物体的碰撞. 然而, 对于更复杂的物体, 如大型船舶或整个水平面, 你可能需要凸形或凹形来代替. 下面会有更多的介绍.

建议动态对象使用原始图形(如 RigidBodies 和 KinematicBodies),因为它们的行为是可靠的,通常也能提供更好的性能。

凸型碰撞形状

Convex collision shapes are a compromise between primitive collision shapes and concave collision shapes. They can represent shapes of any complexity, but with an important caveat. As their name implies, an individual shape can only represent a convex shape. For instance, a pyramid is convex, but a hollow box is concave. To define a concave object with a single collision shape, you need to use a concave collision shape.

根据对象的复杂程度, 可能要通过使用多个凸形而不是一个凹形碰撞形状来获得更好的性能.Godot可以使用 凸分解 来生成与空心物体大致匹配的凸形. 请注意, 在一定数量的凸形之后, 就没有了这种性能优势, 对于大而复杂的对象, 如整个关卡, 建议使用凹形代替.

You can generate one or several convex collision shapes from the editor by selecting a MeshInstance3D and using the Mesh menu at the top of the 3D viewport. The editor exposes two generation modes:

  • 创建单凸碰撞同级 使用Quickhull算法, 创建一个CollisionShape碰撞形状节点, 并自动生成一个凸碰撞形状, 由于只生成单个形状, 因此提供了良好的性能, 非常适合小对象.

  • 创建多个凸形碰撞同级 使用V-HACD算法. 创建多个CollisionShape碰撞形状节点, 每个节点都有一个凸形, 由于它能生成多个形状, 所以对于凹形物体来说, 精度更高, 但性能不佳. 对于中等复杂度的对象, 可能会比使用单个凹形碰撞形状更快.

凹面或三面体碰撞形状

Concave collision shapes, also called trimesh collision shapes, can take any form, from a few triangles to thousands of triangles. Concave shapes are the slowest option but are also the most accurate in Godot. You can only use concave shapes within StaticBodies. They will not work with CharacterBodies or RigidBodies unless the RigidBody's mode is Static.

备注

即使凹形提供了最准确的 碰撞, 但触碰信息的精度可能不如基础形状.

当不使用网络地图进行关卡设计时, 凹形是关卡碰撞的最佳方法, 也就是说, 如果关卡有一些小细节, 可能希望将这些细节排除碰撞之外, 以保证性能和游戏体验, 要做到这一点, 可以在3D建模中建立一个简化的碰撞网格, 并让Godot为其自动生成一个碰撞形状. 下面会有更多的介绍

请注意, 与基础形状和凸形状不同, 凹形碰撞形状没有实际的 "体积", 既可以将对象放置在形状的 外侧, 也可以放置在 内侧.

You can generate a concave collision shape from the editor by selecting a MeshInstance3D and using the Mesh menu at the top of the 3D viewport. The editor exposes two options:

  • 创建三网格静态体是个方便的选择. 它创建一个包含与网格几何学匹配的凹形的静态体.

  • 创建Trimesh(三角面)碰撞同级 创建具有与网格几何体匹配的凹面形状的CollisionShape(碰撞形状)节点.

参见

See 导入 3D 场景 for information on how to export models for Godot and automatically generate collision shapes on import.

性能方面的注意事项

You aren't limited to a single collision shape per PhysicsBody. Still, we recommend keeping the number of shapes as low as possible to improve performance, especially for dynamic objects like RigidBodies and CharacterBodies. On top of that, avoid translating, rotating, or scaling CollisionShapes to benefit from the physics engine's internal optimizations.

When using a single non-transformed collision shape in a StaticBody, the engine's broad phase algorithm can discard inactive PhysicsBodies. The narrow phase will then only have to take into account the active bodies' shapes. If a StaticBody has many collision shapes, the broad phase will fail. The narrow phase, which is slower, must then perform a collision check against each shape.

如果遇到性能问题,你可能需要在准确性方面进行权衡。大多数游戏都没有100%的精确碰撞。他们找到了一些具有创造性的方法来隐藏它,或者在正常的游戏中让它变得不被人注意到。