collision.rst 9.2 KB

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