collision_shapes_2d.rst 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. .. _doc_collision_shapes_2d:
  2. Collision shapes (2D)
  3. =====================
  4. This guide explains:
  5. - The types of collision shapes available in 2D in Godot.
  6. - Using an image converted to a polygon as a collision shape.
  7. - Performance considerations regarding 2D collisions.
  8. Godot provides many kinds of collision shapes, with different performance and
  9. accuracy tradeoffs.
  10. You can define the shape of a :ref:`class_PhysicsBody2D` by adding one or more
  11. :ref:`CollisionShape2Ds <class_CollisionShape2D>` or
  12. :ref:`CollisionPolygon2Ds <class_CollisionPolygon2D>` as *direct* child nodes.
  13. Indirect child nodes (i.e. children of child nodes) will be ignored and won't be
  14. used as collision shapes. Also, note that you must add a :ref:`class_Shape2D`
  15. *resource* to collision shape nodes in the Inspector dock.
  16. .. note::
  17. When you add multiple collision shapes to a single PhysicsBody2D, you don't
  18. have to worry about them overlapping. They won't "collide" with each other.
  19. Primitive collision shapes
  20. --------------------------
  21. Godot provides the following primitive collision shape types:
  22. - :ref:`class_RectangleShape2D`
  23. - :ref:`class_CircleShape2D`
  24. - :ref:`class_CapsuleShape2D`
  25. - :ref:`class_SegmentShape2D`
  26. - :ref:`class_SeparationRayShape2D` (designed for characters)
  27. - :ref:`class_WorldBoundaryShape2D` (infinite plane)
  28. You can represent the collision of most smaller objects using one or more
  29. primitive shapes. However, for more complex objects, such as a large ship or a
  30. whole level, you may need convex or concave shapes instead. More on that below.
  31. We recommend favoring primitive shapes for dynamic objects such as RigidBodies
  32. and CharacterBodies as their behavior is the most reliable. They often provide
  33. better performance as well.
  34. Convex collision shapes
  35. -----------------------
  36. .. warning::
  37. Godot currently doesn't offer a built-in way to create 2D convex collision
  38. shapes. This section is mainly here for reference purposes.
  39. :ref:`Convex collision shapes <class_ConvexPolygonShape2D>` are a compromise
  40. between primitive collision shapes and concave collision shapes. They can
  41. represent shapes of any complexity, but with an important caveat. As their name
  42. implies, an individual shape can only represent a *convex* shape. For instance,
  43. a pyramid is *convex*, but a hollow box is *concave*. To define a concave object
  44. with a single collision shape, you need to use a concave collision shape.
  45. Depending on the object's complexity, you may get better performance by using
  46. multiple convex shapes instead of a concave collision shape. Godot lets you use
  47. *convex decomposition* to generate convex shapes that roughly match a hollow
  48. object. Note this performance advantage no longer applies after a certain amount
  49. of convex shapes. For large and complex objects such as a whole level, we
  50. recommend using concave shapes instead.
  51. Concave or trimesh collision shapes
  52. -----------------------------------
  53. :ref:`Concave collision shapes <class_ConcavePolygonShape2D>`, also called trimesh
  54. collision shapes, can take any form, from a few triangles to thousands of
  55. triangles. Concave shapes are the slowest option but are also the most accurate
  56. in Godot. **You can only use concave shapes within StaticBodies.** They will not
  57. work with CharacterBodies or RigidBodies unless the RigidBody's mode is Static.
  58. .. note::
  59. Even though concave shapes offer the most accurate *collision*, contact
  60. reporting can be less precise than primitive shapes.
  61. When not using TileMaps for level design, concave shapes are the best approach
  62. for a level's collision.
  63. You can configure the CollisionPolygon2D node's *build mode* in the inspector.
  64. If it is set to **Solids** (the default), collisions will include the polygon
  65. and its contained area. If it is set to **Segments**, collisions will only
  66. include the polygon edges.
  67. You can generate a concave collision shape from the editor by selecting a Sprite2D
  68. and using the **Sprite2D** menu at the top of the 2D viewport. The Sprite2D menu
  69. dropdown exposes an option called **Create CollisionPolygon2D Sibling**.
  70. Once you click it, it displays a menu with 3 settings:
  71. - **Simplification:** Higher values will result in a less detailed shape, which
  72. improves performance at the cost of accuracy.
  73. - **Shrink (Pixels):** Higher values will shrink the generated collision polygon
  74. relative to the sprite's edges.
  75. - **Grow (Pixels):** Higher values will grow the generated collision polygon
  76. relative to the sprite's edges. Note that setting Grow and Shrink to equal
  77. values may yield different results than leaving both of them on 0.
  78. .. note::
  79. If you have an image with many small details, it's recommended to create a
  80. simplified version and use it to generate the collision polygon. This
  81. can result in better performance and game feel, since the player won't
  82. be blocked by small, decorative details.
  83. To use a separate image for collision polygon generation, create another
  84. Sprite2D, generate a collision polygon sibling from it then remove the Sprite2D
  85. node. This way, you can exclude small details from the generated collision.
  86. Performance caveats
  87. -------------------
  88. You aren't limited to a single collision shape per PhysicsBody. Still, we
  89. recommend keeping the number of shapes as low as possible to improve
  90. performance, especially for dynamic objects like RigidBodies and
  91. CharacterBodies. On top of that, avoid translating, rotating, or scaling
  92. CollisionShapes to benefit from the physics engine's internal optimizations.
  93. When using a single non-transformed collision shape in a StaticBody, the
  94. engine's *broad phase* algorithm can discard inactive PhysicsBodies. The *narrow
  95. phase* will then only have to take into account the active bodies' shapes. If a
  96. StaticBody has many collision shapes, the broad phase will fail. The narrow
  97. phase, which is slower, must then perform a collision check against each shape.
  98. If you run into performance issues, you may have to make tradeoffs in terms of
  99. accuracy. Most games out there don't have a 100% accurate collision. They find
  100. creative ways to hide it or otherwise make it unnoticeable during normal
  101. gameplay.