碰撞形状(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提供了以下基本碰撞形状类型:
SeparationRayShape2D (专为角色设计)
WorldBoundaryShape2D (无限平面)
你可以使用一个或多个原始形状来表示大多数较小物体的碰撞. 然而, 对于更复杂的物体, 如大型船舶或整个水平面, 你可能需要凸形或凹形来代替. 下面会有更多的介绍.
建议动态对象使用原始图形(如 RigidBodies 和 KinematicBodies),因为它们的行为是可靠的,通常也能提供更好的性能。
凸型碰撞形状
警告
Godot 目前没有提供创建 2D 凸形碰撞形状的内置方法。本节内容主要供参考。
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 CharacterBodies 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.
你可以在检查器中配置 CollisionPolygon2D 节点的 build mode。如果将其设置为 Solids**(默认),碰撞将包括多边形及其所包含的区域。如果将其设置为 **Segments,碰撞将仅包括多边形边缘。
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:
增长(像素): 数值越大,生成的碰撞多边形就越会相对于精灵边缘增长。需要注意的是,将 “增长 ”和 “收缩 ”设置为相等的值,可能会与将它们都设置为 0 产生的结果不同。
备注
如果你的图片包含许多小细节,建议创建一个简化版本并使用它来生成碰撞多边形。这样可以带来更好的性能表现和游戏体验,因为玩家不会被小小的、装饰性的细节阻碍。
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.
性能方面的注意事项
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%的精确碰撞。他们找到了一些具有创造性的方法来隐藏它,或者在正常的游戏中让它变得不被人注意到。