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