collision_shapes_3d.rst 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. .. _doc_collision_shapes_3d:
  2. Collision shapes (3D)
  3. =====================
  4. This guide explains:
  5. - The types of collision shapes available in 3D in Godot.
  6. - Using a convex or a concave mesh as a collision shape.
  7. - Performance considerations regarding 3D 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_PhysicsBody3D` by adding one or more
  11. :ref:`CollisionShape3Ds <class_CollisionShape3D>` as *direct* child nodes.
  12. Indirect child nodes (i.e. children of child nodes) will be ignored and won't be
  13. used as collision shapes. Also, note that you must add a :ref:`class_Shape3D`
  14. *resource* to collision shape nodes in the Inspector dock.
  15. .. note::
  16. When you add multiple collision shapes to a single PhysicsBody, 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_BoxShape3D`
  22. - :ref:`class_SphereShape3D`
  23. - :ref:`class_CapsuleShape3D`
  24. - :ref:`class_CylinderShape3D`
  25. You can represent the collision of most smaller objects using one or more
  26. primitive shapes. However, for more complex objects, such as a large ship or a
  27. whole level, you may need convex or concave shapes instead. More on that below.
  28. We recommend favoring primitive shapes for dynamic objects such as RigidBodies
  29. and CharacterBodies as their behavior is the most reliable. They often provide
  30. better performance as well.
  31. Convex collision shapes
  32. -----------------------
  33. :ref:`Convex collision shapes <class_ConvexPolygonShape3D>` are a compromise
  34. between primitive collision shapes and concave collision shapes. They can
  35. represent shapes of any complexity, but with an important caveat. As their name
  36. implies, an individual shape can only represent a *convex* shape. For instance,
  37. a pyramid is *convex*, but a hollow box is *concave*. To define a concave object
  38. with a single collision shape, you need to use a concave collision shape.
  39. Depending on the object's complexity, you may get better performance by using
  40. multiple convex shapes instead of a concave collision shape. Godot lets you use
  41. *convex decomposition* to generate convex shapes that roughly match a hollow
  42. object. Note this performance advantage no longer applies after a certain amount
  43. of convex shapes. For large and complex objects such as a whole level, we
  44. recommend using concave shapes instead.
  45. You can generate one or several convex collision shapes from the editor by
  46. selecting a MeshInstance3D and using the **Mesh** menu at the top of the 3D
  47. viewport. The editor exposes two generation modes:
  48. - **Create Single Convex Collision Sibling** uses the Quickhull algorithm. It
  49. creates one CollisionShape node with an automatically generated convex
  50. collision shape. Since it only generates a single shape, it provides good
  51. performance and is ideal for small objects.
  52. - **Create Multiple Convex Collision Siblings** uses the V-HACD algorithm. It
  53. creates several CollisionShape nodes, each with a convex shape. Since it
  54. generates multiple shapes, it is more accurate for concave objects at the cost
  55. of performance. For objects with medium complexity, it will likely be faster
  56. than using a single concave collision shape.
  57. Concave or trimesh collision shapes
  58. -----------------------------------
  59. :ref:`Concave collision shapes <class_ConcavePolygonShape3D>`, also called trimesh
  60. collision shapes, can take any form, from a few triangles to thousands of
  61. triangles. Concave shapes are the slowest option but are also the most accurate
  62. in Godot. **You can only use concave shapes within StaticBodies.** They will not
  63. work with CharacterBodies or RigidBodies unless the RigidBody's mode is Static.
  64. .. note::
  65. Even though concave shapes offer the most accurate *collision*, contact
  66. reporting can be less precise than primitive shapes.
  67. When not using GridMaps for level design, concave shapes are the best approach
  68. for a level's collision. That said, if your level has small details, you may
  69. want to exclude those from collision for performance and game feel. To do so,
  70. you can build a simplified collision mesh in a 3D modeler and have Godot
  71. generate a collision shape for it automatically. More on that below
  72. Note that unlike primitive and convex shapes, a concave collision shape doesn't
  73. have an actual "volume". You can place objects both *outside* of the shape as
  74. well as *inside*.
  75. You can generate a concave collision shape from the editor by selecting a
  76. MeshInstance3D and using the **Mesh** menu at the top of the 3D viewport. The
  77. editor exposes two options:
  78. - **Create Trimesh Static Body** is a convenient option. It creates a StaticBody
  79. containing a concave shape matching the mesh's geometry.
  80. - **Create Trimesh Collision Sibling** creates a CollisionShape node with a
  81. concave shape matching the mesh's geometry.
  82. .. seealso::
  83. See :ref:`doc_importing_3d_scenes` for information on how to export models
  84. for Godot and automatically generate collision shapes on import.
  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.