collision.rst 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. .. _doc_3d_particles_collision:
  2. 3D Particle collisions
  3. ----------------------
  4. .. figure:: img/particle_collision.webp
  5. :alt: Particle collisions
  6. Since GPU particles are processed entirely on the GPU, they don't have access to the game's physical
  7. world. If you need particles to collide with the environment, you have to set up particle collision nodes.
  8. There are four of them: :ref:`class_GPUParticlesCollisionBox3D`, :ref:`class_GPUParticlesCollisionSphere3D`,
  9. :ref:`class_GPUParticlesCollisionSDF3D`, and :ref:`class_GPUParticlesCollisionHeightField3D`.
  10. Common properties
  11. ~~~~~~~~~~~~~~~~~
  12. .. figure:: img/particle_collision_common.webp
  13. :alt: Common particle collision properties
  14. :align: right
  15. Common collision properties
  16. There are some properties that you can find on all collision nodes. They're located in the
  17. ``GPUParticlesCollision3D`` section in the inspector.
  18. The ``Cull Mask`` property controls which particle systems are affected by a collision node based
  19. on each system's :ref:`visibility layers <class_VisualInstance3D>`. A particle system collides with a
  20. collision node only if at least one of the system's visibility layers is enabled in the
  21. collider's cull mask.
  22. .. warning::
  23. There is a `known issue <https://github.com/godotengine/godot/issues/61014>`_ with
  24. GPU particle collision that prevent the cull mask from working properly in Godot 4.0. We will
  25. update the documentation as soon as it is fixed.
  26. Box collision
  27. ~~~~~~~~~~~~~
  28. .. figure:: img/particle_collision_box_entry.webp
  29. :alt: Particle collision box
  30. :align: right
  31. Box collision in the node list
  32. Box collision nodes are shaped like a solid, rectangular box. You control their size with the ``Extents``
  33. property. Box extents always measure half of the sides of its bounds, so a value of ``(X=1.0,Y=1.0,Z=1.0)``
  34. creates a box that is 2 meters wide on each side. Box collision nodes are useful for simulating floor
  35. and wall geometry that particles should collide against.
  36. To create a box collision node, add a new child node to your scene and select ``GPUParticlesCollisionBox3D``
  37. from the list of available nodes. You can animate the box position or attach it to a
  38. moving node for more dynamic effects.
  39. .. figure:: img/particle_collision_box.webp
  40. :alt: Box collision with particle systems
  41. Two particle systems collide with a box collision node
  42. Sphere collision
  43. ~~~~~~~~~~~~~~~~
  44. .. figure:: img/particle_collision_sphere_entry.webp
  45. :alt: Particle collision sphere
  46. :align: right
  47. Sphere collision in the node list
  48. Sphere collision nodes are shaped like a solid sphere. The ``Radius`` property controls the size of the sphere.
  49. While box collision nodes don't have to be perfect cubes, sphere collision nodes will always be
  50. spheres. If you want to set width independently from height, you have to change the ``Scale``
  51. property in the ``Node3D`` section.
  52. To create a sphere collision node, add a new child node to your scene and select ``GPUParticlesCollisionSphere3D``
  53. from the list of available nodes. You can animate the sphere's position or attach it to a
  54. moving node for more dynamic effects.
  55. .. figure:: img/particle_collision_sphere.webp
  56. :alt: Sphere collision with particle systems
  57. Two particle systems collide with a sphere collision node
  58. Height field collision
  59. ~~~~~~~~~~~~~~~~~~~~~~
  60. .. figure:: img/particle_collision_height.webp
  61. :alt: Particle collision height field
  62. :align: right
  63. Height field collision in the node list
  64. Height field particle collision is very useful for large outdoor areas that need to collide with particles.
  65. At runtime, the node creates a height field from all the meshes within its bounds that match its cull mask.
  66. Particles collide against the mesh that this height field represents. Since the height field generation is
  67. done dynamically, it can follow the player camera around and react to changes in the level. Different
  68. settings for the height field density offer a wide range of performance adjustments.
  69. To create a height field collision node, add a new child node to your scene and select ``GPUParticlesCollisionHeightField3D``
  70. from the list of available nodes.
  71. A height field collision node is shaped like a box. The ``Extents`` property controls its size. Extents
  72. always measure half of the sides of its bounds, so a value of ``(X=1.0,Y=1.0,Z=1.0)`` creates a box that
  73. is 2 meters wide on each side. Anything outside of the node's extents is ignored for height field creation.
  74. The ``Resolution`` property controls how detailed the height field is. A lower resolution performs faster
  75. at the cost of accuracy. If the height field resolution is too low, it may look like particles penetrate level geometry
  76. or get stuck in the air during collision events. They might also ignore some smaller meshes completely.
  77. .. figure:: img/particle_heightfield_res.webp
  78. :alt: Height field resolutions
  79. At low resolutions, height field collision misses some finer details (left)
  80. The ``Update Mode`` property controls when the height field is recreated from the meshes within its
  81. bounds. Set it to ``When Moved`` to make it refresh only when it moves. This performs well and is
  82. suited for static scenes that don't change very often. If you need particles to collide with dynamic objects
  83. that change position frequently, you can select ``Always`` to refresh every frame. This comes with a
  84. cost to performance and should only be used when necessary.
  85. .. note::
  86. It's important to remember that when ``Update Mode`` is set to ``When Moved``, it is the *height field node*
  87. whose movement triggers an update. The height field is not updated when one of the meshes inside it moves.
  88. The ``Follow Camera Enabled`` property makes the height field follow the current camera when enabled. It will
  89. update whenever the camera moves. This property can be used to make sure that there is always particle collision
  90. around the player while not wasting performance on regions that are out of sight or too far away.
  91. SDF collision
  92. ~~~~~~~~~~~~~
  93. .. figure:: img/particle_collision_sdf_entry.webp
  94. :alt: Particle collision SDF
  95. :align: right
  96. SDF collision in the node list
  97. SDF collision nodes create a `signed distance field <https://www.reddit.com/r/explainlikeimfive/comments/k2zbos/eli5_what_are_distance_fields_in_graphics>`_
  98. that particles can collide with. SDF collision is similar to height field collision in that it turns multiple
  99. meshes within its bounds into a single collision volume for particles. A major difference is that signed distance
  100. fields can represent holes, tunnels and overhangs, which is impossible to do with height fields alone. The
  101. performance overhead is larger compared to height fields, so they're best suited for small-to-medium-sized environments.
  102. To create an SDF collision node, add a new child node to your scene and select ``GPUParticlesCollisionSDF3D``
  103. from the list of available nodes. SDF collision nodes have to be baked in order to have any effect on particles
  104. in the level. To do that, click the ``Bake SDF`` button in the viewport toolbar
  105. while the SDF collision node is selected and choose a directory to store the baked data. Since SDF collision needs
  106. to be baked in the editor, it's static and cannot change at runtime.
  107. .. figure:: img/particle_collision_sdf.webp
  108. :alt: SDF particle collision
  109. SDF particle collision allows for very detailed 3-dimensional collision shapes
  110. An SDF collision node is shaped like a box. The ``Extents`` property controls its size. Extents
  111. always measure half of the sides of its bounds, so a value of ``(X=1.0,Y=1.0,Z=1.0)`` creates a box that
  112. is 2 meters wide on each side. Anything outside of the node's extents is ignored for collision.
  113. The ``Resolution`` property controls how detailed the distance field is. A lower resolution performs faster
  114. at the cost of accuracy. If the resolution is too low, it may look like particles penetrate level geometry
  115. or get stuck in the air during collision events. They might also ignore some smaller meshes completely.
  116. .. figure:: img/particle_collision_sdf_res.webp
  117. :alt: Resolution comparison
  118. The same area covered by a signed distance field at different resolutions: 16 (left) and 256 (right)
  119. The ``Thickness`` property gives the distance field, which is usually hollow on the inside, a thickness to
  120. prevent particles from penetrating at high speeds. If you find that some particles don't collide with the
  121. level geometry and instead shoot right through it, try setting this property to a higher value.
  122. The ``Bake Mask`` property controls which meshes will be considered when the SDF is baked. Only meshes that
  123. render on the active layers in the bake mask contribute to particle collision.
  124. Troubleshooting
  125. ~~~~~~~~~~~~~~~
  126. For particle collision to work, the particle's :ref:`visibility AABB <doc_3d_particles_properties_draw>`
  127. must overlap with the collider's AABB. If collisions appear to be not working
  128. despite colliders being set up, generate an updated visibility AABB by selecting
  129. the GPUParticles3D node and choosing **GPUParticles3D > Generate Visibility AABB…**
  130. at the top of the 3D editor viewport.
  131. If the particles move fast and colliders are thin. There are two solutions for this:
  132. - Make the colliders thicker. For instance, if particles cannot get below a
  133. solid floor, you could make the collider representing the floor thicker than
  134. its actual visual representation. The heightfield collider automatically
  135. handles this by design, as heightfields cannot represent "room over room"
  136. collision.
  137. - Increased ``Fixed FPS`` in the GPUParticles3D node, which will perform collision
  138. checks more often. This comes at a performance cost, so avoid setting this too high.