soft_body.rst 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. .. _doc_soft_body:
  2. Using SoftBody3D
  3. ================
  4. Soft bodies (or *soft-body dynamics*) simulate movement, changing shape and other
  5. physical properties of deformable objects. For example, this can be used to simulate
  6. cloth or to create more realistic characters.
  7. Physics engine considerations
  8. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. Support for soft bodies is generally more robust in Jolt Physics compared to GodotPhysics3D.
  10. You can switch physics engines by changing **Physics > 3D > Physics Engine**
  11. in the Project Settings. Projects created in Godot 4.6 and later use Jolt Physics
  12. by default, but existing projects will have to be switched over manually.
  13. Additionally, :ref:`physics interpolation <doc_physics_interpolation>` currently
  14. does not affect soft bodies. If you want soft body simulation to look smoother at
  15. higher framerates, you'll have to increase the **Physics > Common > Physics Ticks per Second**
  16. project setting, which comes at a performance cost.
  17. Basic setup
  18. ~~~~~~~~~~~
  19. A :ref:`SoftBody3D <class_SoftBody3D>` node is used for soft body simulations.
  20. Unlike other physics body nodes like :ref:`RigidBody3D <class_RigidBody3D>`
  21. or :ref:`StaticBody3D <class_StaticBody3D>`, it does **not** have a
  22. :ref:`CollisionShape3D <class_CollisionShape3D>` or a :ref:`MeshInstance3D <class_MeshInstance3D>`
  23. child node. Instead, the collision shape is derived from the mesh assigned to the node.
  24. This mesh is also directly used for rendering, which means you don't need to
  25. create any child nodes for a functional, visible setup.
  26. We will create a bouncy cube to demonstrate the setup of a soft body.
  27. Create a new scene with a Node3D node as root. Then, create a SoftBody3D node.
  28. Add a BoxMesh in the **Mesh** property of the node in the inspector
  29. and increase the subdivision of the mesh for simulation.
  30. The subdivision level determines the precision level of the deformation,
  31. with higher values allowing for smaller and more detailed deformations,
  32. at the cost of performance. In this example, we'll set it to 3 on each axis:
  33. .. figure:: img/soft_body_box_mesh.webp
  34. :align: center
  35. :alt: Adjusting BoxMesh properties in the inspector
  36. Adjusting BoxMesh properties in the inspector
  37. Now, set the parameters to obtain the type of soft body you aim for.
  38. Try to keep the **Simulation Precision** above 5; otherwise,
  39. the soft body may collapse.
  40. .. figure:: img/soft_body_inspector.webp
  41. :align: center
  42. :alt: Adjusting SoftBody3D simulation properties in the inspector
  43. Adjusting SoftBody3D simulation properties in the inspector
  44. .. note::
  45. Handle some parameters with care, as some values can lead to strange results.
  46. For example, if the shape is not completely closed and you set pressure
  47. to a value greater than ``0.0``, the soft body will fly around like a plastic bag
  48. under strong wind.
  49. Run the scene to view the simulation. Here's an example of what it should look like:
  50. .. video:: video/soft_body_box_simulation.webm
  51. :alt: Soft body box simulation example
  52. :autoplay:
  53. :loop:
  54. :muted:
  55. :align: default
  56. :width: 100%
  57. .. tip::
  58. To improve the simulation's result, increase the **Simulation Precision**.
  59. This can give a significant improvement at the cost of performance.
  60. Alternatively, you can increase the **Physics > Common > Physics Ticks per Second**
  61. project setting, which will also affect soft body simulation quality.
  62. Cloak simulation
  63. ~~~~~~~~~~~~~~~~
  64. Let's make a cloak in the Platformer 3D demo.
  65. .. note::
  66. You can download the Platformer 3D demo on
  67. `GitHub <https://github.com/godotengine/godot-demo-projects/tree/master/3d/platformer>`_
  68. or `the Asset Library <https://godotengine.org/asset-library/asset/2748>`_.
  69. Open the ``player/player.tscn`` scene, add a ``SoftBody3D`` node below the root node,
  70. then assign a PlaneMesh resource to it in its **Mesh** property.
  71. Open the PlaneMesh's properties and set the size to ``(0.5, 1.0)``,
  72. then set **Subdivide Width** and **Subdivide Depth** to ``5``. Adjust the
  73. SoftBody3D node's position and rotation so that the plane appears to be close to
  74. the character's back. You should end up with something like this:
  75. .. figure:: img/soft_body_cloak_subdivide.webp
  76. :align: center
  77. :alt: Subdividing the PlaneMesh and placing it on the character's back
  78. Subdividing the PlaneMesh and placing it on the character's back
  79. .. tip::
  80. Subdivision generates a more tessellated mesh for better simulations.
  81. However, higher subdivision levels will impact performance. Try
  82. to find a balance between performance and quality. This depends on the number
  83. of soft body simulations that you expect to be active at a given time,
  84. as well as the distance between the camera and the soft body.
  85. Add a :ref:`BoneAttachment3D <class_BoneAttachment3D>` node under the skeleton
  86. node and select the Neck bone to attach the cloak to the character skeleton.
  87. .. note::
  88. The BoneAttachment3D node is used to attach objects to a bone of an armature.
  89. The attached object will follow the bone's movement. For example, a character's
  90. held weapon can be attached this way.
  91. Do **not** move the SoftBody3D node under the BoneAttachment3D node as of now.
  92. Instead, we'll configure its *pinned points* to follow the BoneAttachment3D node.
  93. .. figure:: img/soft_body_cloak_bone_attach.webp
  94. :align: center
  95. :alt: Configuring the BoneAttachment3D node in the inspector
  96. Configuring the BoneAttachment3D node in the inspector
  97. To create pinned points, select the upper vertices in the SoftBody3D node. A pinned
  98. point appears blue in the 3D editor viewport:
  99. .. figure:: img/soft_body_cloak_pinned.webp
  100. :align: center
  101. :alt: Pinning the SoftBody3D's points in the inspector
  102. Pinning the SoftBody3D's points in the inspector
  103. The pinned joints can be found in SoftBody3D's **Attachments** section,
  104. which is under the **Collision** section that must be expanded first.
  105. Choose the BoneAttachment3D node as the **Spatial Attachment Path** for each
  106. pinned joint. The pinned joints are now attached to the neck.
  107. .. tip::
  108. To assign the properties faster, you can drag-and-drop the BoneAttachment3D node
  109. from the scene tree dock to the **Spatial Attachment Path** property field.
  110. Note that you may have to deselect then reselect the SoftBody3D node for the
  111. **Attachments** section to appear.
  112. .. figure:: img/soft_body_cloak_pinned_attach.webp
  113. :align: center
  114. :alt: Configuring pinned points to be attached to the BoneAttachment3D node in the SoftBody3D inspector
  115. Configuring pinned points to be attached to the BoneAttachment3D node in the SoftBody3D inspector
  116. The last step is to avoid clipping by adding the CharacterBody3D ``Player`` (the scene's root node)
  117. to the **Parent Collision Ignore** property of the SoftBody3D.
  118. .. figure:: img/soft_body_cloak_ignore.webp
  119. :align: center
  120. :alt: Setting up the collision exception in the SoftBody3D inspector
  121. Setting up the collision exception in the SoftBody3D inspector
  122. Play the scene and the cloak should simulate correctly.
  123. .. figure:: img/soft_body_cloak_finish.webp
  124. :align: center
  125. :alt: Final result when running the project's main scene
  126. Final result when running the project's main scene
  127. This covers the basic settings of a soft body simulation. Experiment with the parameters
  128. to achieve the effect you are aiming for when making your game.
  129. .. note::
  130. The cloak will not appear when viewed from certain angles due to backface culling.
  131. To resolve this, you can disable backface culling by assigning a new StandardMaterial3D,
  132. then setting its cull mode to **Disabled**. This will make the material render
  133. both sides of the plane.
  134. Using imported meshes
  135. ~~~~~~~~~~~~~~~~~~~~~
  136. The **Save to File** option in the Advanced Import Settings dialog allows you
  137. to save a mesh to a standalone resource file that you can then attach to
  138. SoftBody3D nodes.
  139. You may also want to disable LOD generation or change the LOD generation options
  140. when importing a mesh for use with SoftBody3D. The default import settings
  141. will produce an LOD that merges adjacent faces that are nearly flat with
  142. respect to each other, even at very close render distances. This works well for
  143. static meshes, but is often undesirable for use with SoftBody3D if you want
  144. these faces to be able to bend and move with respect to each other, instead of
  145. being rendered as a single plane.
  146. See :ref:`doc_importing_3d_scenes_import_configuration` and :ref:`doc_mesh_lod`
  147. for more details.