collision_shapes_3d.rst 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 child nodes. Note that you must
  12. add a :ref:`class_Shape3D` *resource* to collision shape nodes in the Inspector
  13. dock.
  14. .. note::
  15. When you add multiple collision shapes to a single PhysicsBody, you don't
  16. have to worry about them overlapping. They won't "collide" with each other.
  17. Primitive collision shapes
  18. --------------------------
  19. Godot provides the following primitive collision shape types:
  20. - :ref:`class_BoxShape3D`
  21. - :ref:`class_SphereShape3D`
  22. - :ref:`class_CapsuleShape3D`
  23. - :ref:`class_CylinderShape3D`
  24. You can represent the collision of most smaller objects using one or more
  25. primitive shapes. However, for more complex objects, such as a large ship or a
  26. whole level, you may need convex or concave shapes instead. More on that below.
  27. We recommend favoring primitive shapes for dynamic objects such as RigidBodies
  28. and CharacterBodies as their behavior is the most reliable. They often provide
  29. better performance as well.
  30. Convex collision shapes
  31. -----------------------
  32. :ref:`Convex collision shapes <class_ConvexPolygonShape3D>` are a compromise
  33. between primitive collision shapes and concave collision shapes. They can
  34. represent shapes of any complexity, but with an important caveat. As their name
  35. implies, an individual shape can only represent a *convex* shape. For instance,
  36. a pyramid is *convex*, but a hollow box is *concave*. To define a concave object
  37. with a single collision shape, you need to use a concave collision shape.
  38. Depending on the object's complexity, you may get better performance by using
  39. multiple convex shapes instead of a concave collision shape. Godot lets you use
  40. *convex decomposition* to generate convex shapes that roughly match a hollow
  41. object. Note this performance advantage no longer applies after a certain amount
  42. of convex shapes. For large and complex objects such as a whole level, we
  43. recommend using concave shapes instead.
  44. You can generate one or several convex collision shapes from the editor by
  45. selecting a MeshInstance3D and using the **Mesh** menu at the top of the 3D
  46. viewport. The editor exposes two generation modes:
  47. - **Create Single Convex Collision Sibling** uses the Quickhull algorithm. It
  48. creates one CollisionShape node with an automatically generated convex
  49. collision shape. Since it only generates a single shape, it provides good
  50. performance and is ideal for small objects.
  51. - **Create Multiple Convex Collision Siblings** uses the V-HACD algorithm. It
  52. creates several CollisionShape nodes, each with a convex shape. Since it
  53. generates multiple shapes, it is more accurate for concave objects at the cost
  54. of performance. For objects with medium complexity, it will likely be faster
  55. than using a single concave collision shape.
  56. Concave or trimesh collision shapes
  57. -----------------------------------
  58. :ref:`Concave collision shapes <class_ConcavePolygonShape3D>`, also called trimesh
  59. collision shapes, can take any form, from a few triangles to thousands of
  60. triangles. Concave shapes are the slowest option but are also the most accurate
  61. in Godot. **You can only use concave shapes within StaticBodies.** They will not
  62. work with CharacterBodies or RigidBodies unless the RigidBody's mode is Static.
  63. .. note::
  64. Even though concave shapes offer the most accurate *collision*, contact
  65. reporting can be less precise than primitive shapes.
  66. When not using GridMaps for level design, concave shapes are the best approach
  67. for a level's collision. That said, if your level has small details, you may
  68. want to exclude those from collision for performance and game feel. To do so,
  69. you can build a simplified collision mesh in a 3D modeler and have Godot
  70. generate a collision shape for it automatically. More on that below
  71. Note that unlike primitive and convex shapes, a concave collision shape doesn't
  72. have an actual "volume". You can place objects both *outside* of the shape as
  73. well as *inside*.
  74. You can generate a concave collision shape from the editor by selecting a
  75. MeshInstance3D and using the **Mesh** menu at the top of the 3D viewport. The
  76. editor exposes two options:
  77. - **Create Trimesh Static Body** is a convenient option. It creates a StaticBody
  78. containing a concave shape matching the mesh's geometry.
  79. - **Create Trimesh Collision Sibling** creates a CollisionShape node with a
  80. concave shape matching the mesh's geometry.
  81. .. seealso::
  82. See :ref:`doc_importing_3d_scenes` for information on how to export models
  83. for Godot and automatically generate collision shapes on import.
  84. Performance caveats
  85. -------------------
  86. You aren't limited to a single collision shape per PhysicsBody. Still, we
  87. recommend keeping the number of shapes as low as possible to improve
  88. performance, especially for dynamic objects like RigidBodies and
  89. CharacterBodies. On top of that, avoid translating, rotating, or scaling
  90. CollisionShapes to benefit from the physics engine's internal optimizations.
  91. When using a single non-transformed collision shape in a StaticBody, the
  92. engine's *broad phase* algorithm can discard inactive PhysicsBodies. The *narrow
  93. phase* will then only have to take into account the active bodies' shapes. If a
  94. StaticBody has many collision shapes, the broad phase will fail. The narrow
  95. phase, which is slower, must then perform a collision check against each shape.
  96. If you run into performance issues, you may have to make tradeoffs in terms of
  97. accuracy. Most games out there don't have a 100% accurate collision. They find
  98. creative ways to hide it or otherwise make it unnoticeable during normal
  99. gameplay.