collision_shapes_2d.rst 5.9 KB

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