Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

Collision shapes (2D)

本指南解释:

  • The types of collision shapes available in 2D in Godot.

  • Using an image converted to a polygon as a collision shape.

  • Performance considerations regarding 2D collisions.

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

You can define the shape of a PhysicsBody2D by adding one or more CollisionShape2Ds or CollisionPolygon2Ds as child nodes. Note that you must add a Shape2D resource to collision shape nodes in the Inspector dock.

备注

When you add multiple collision shapes to a single PhysicsBody2D, you don't have to worry about them overlapping. They won't "collide" with each other.

基本碰撞形状

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

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

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

凸型碰撞形状

警告

Godot currently doesn't offer a built-in way to create 2D convex collision shapes. This section is mainly here for reference purposes.

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可以使用 凸分解 来生成与空心物体大致匹配的凸形. 请注意, 在一定数量的凸形之后, 就没有了这种性能优势, 对于大而复杂的对象, 如整个关卡, 建议使用凹形代替.

凹面或三面体碰撞形状

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 KinematicBodies or RigidBodies unless the RigidBody's mode is Static.

备注

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

When not using TileMaps for level design, concave shapes are the best approach for a level's collision.

You can configure the CollisionPolygon2D node's build mode in the inspector. If it is set to Solids (the default), collisions will include the polygon and its contained area. If it is set to Segments, collisions will only include the polygon edges.

You can generate a concave collision shape from the editor by selecting a Sprite2D and using the Sprite2D menu at the top of the 2D viewport. The Sprite2D menu dropdown exposes an option called Create CollisionPolygon2D Sibling. Once you click it, it displays a menu with 3 settings:

  • Simplification: Higher values will result in a less detailed shape, which improves performance at the cost of accuracy.

  • Shrink (Pixels): Higher values will shrink the generated collision polygon relative to the sprite's edges.

  • Grow (Pixels): Higher values will grow the generated collision polygon relative to the sprite's edges. Note that setting Grow and Shrink to equal values may yield different results than leaving both of them on 0.

备注

If you have an image with many small details, it's recommended to create a simplified version and use it to generate the collision polygon. This can result in better performance and game feel, since the player won't be blocked by small, decorative details.

To use a separate image for collision polygon generation, create another Sprite2D, generate a collision polygon sibling from it then remove the Sprite2D node. This way, you can exclude small details from the generated collision.

性能方面的注意事项

每个PhysicsBody(物理体)不限于一个碰撞形状. 尽管如此, 我们还是建议尽量减少碰撞形状的数量以提高性能, 特别是对于像RigidBodies (刚体)和KinematicBodies(运动体)这样的动态对象. 除此之外, 避免translating(平移), rotating(旋转)或scaling(缩放)碰撞形状, 可以提高物理引擎运行效率.

在静态体中使用单个非转换碰撞形状时, 引擎的宽相位算法可以丢弃不活跃的物理体. 这个窄相只需考虑到活跃物体的形状. 如果一个StaticBody有许多碰撞形状, 那么宽相位就会失败. 较慢的窄相位必须对每个形状执行碰撞检查.

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