collision_shapes_3d.rst 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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_PhysicsBody` by adding one or more
  11. :ref:`CollisionShapes <class_CollisionShape>` as child nodes. Note that you must
  12. add a :ref:`class_Shape` *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_BoxShape`
  21. - :ref:`class_SphereShape`
  22. - :ref:`class_CapsuleShape`
  23. - :ref:`class_CylinderShape`. It is only available when using the Bullet physics
  24. engine.
  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 KinematicBodies 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_ConvexPolygonShape>` 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 MeshInstance 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_ConcavePolygonShape>`, 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 KinematicBodies 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. MeshInstance 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. .. note::
  83. Suppose you need to make a RigidBody *slide* on a concave collision shape.
  84. In that case, you may notice that sometimes, the RigidBody will bump
  85. upwards. To solve this, open **Project > Project Settings** and enable
  86. **Physics > 3d > Smooth Trimesh Collision**.
  87. Once you've enabled smooth trimesh collision, make sure the concave shape is
  88. the only shape of your StaticBody and that it's located at its origin
  89. without any rotation. This way, the RigidBody should slide perfectly on the
  90. StaticBody.
  91. .. seealso::
  92. Godot can generate collision shapes for your imported 3D scenes
  93. automatically. See :ref:`doc_importing_scenes_import_hints` in the
  94. documentation for more information.
  95. Performance caveats
  96. -------------------
  97. You aren't limited to a single collision shape per PhysicsBody. Still, we
  98. recommend keeping the number of shapes as low as possible to improve
  99. performance, especially for dynamic objects like RigidBodies and
  100. KinematicBodies. On top of that, avoid translating, rotating, or scaling
  101. CollisionShapes to benefit from the physics engine's internal optimizations.
  102. When using a single non-transformed collision shape in a StaticBody, the
  103. engine's *broad phase* algorithm can discard inactive PhysicsBodies. The *narrow
  104. phase* will then only have to take into account the active bodies' shapes. If a
  105. StaticBody has many collision shapes, the broad phase will fail. The narrow
  106. phase, which is slower, must then perform a collision check against each shape.
  107. If you run into performance issues, you may have to make tradeoffs in terms of
  108. accuracy. Most games out there don't have a 100% accurate collision. They find
  109. creative ways to hide it or otherwise make it unnoticeable during normal
  110. gameplay.