using_jolt_physics.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. .. _doc_using_jolt_physics:
  2. Using Jolt Physics
  3. ==================
  4. Introduction
  5. ------------
  6. The Jolt physics engine was added as an alternative to the existing Godot Physics
  7. physics engine in 4.4. Jolt is developed by Jorrit Rouwe with a focus on games and
  8. VR applications. Previously it was available as an extension but is now built into
  9. Godot. By default, new projects will use it as the physics engine.
  10. The existing extension is now considered in maintenance mode. That means bug fixes
  11. will be merged, and it will be kept compatible with new versions of Godot until
  12. the built-in module has feature parity with the extension. The only thing missing at
  13. this point is related joints, which you can read about on this page. The extension
  14. can be found `here on GitHub <https://github.com/godot-jolt/godot-jolt>`_ and in
  15. Godot's asset library.
  16. To change the 3D physics engine to be Jolt Physics, set
  17. :ref:`Project Settings > Physics > 3D > Physics Engine<class_ProjectSettings_property_physics/3D/Physics_Engine>`
  18. to ``Jolt Physics``. Once you've done that, click the **Save & Restart** button.
  19. When the editor opens again, 3D scenes should now be using Jolt for physics.
  20. Notable differences to Godot Physics
  21. ------------------------------------
  22. There are many differences between the existing Godot Physics engine and Jolt.
  23. Joint properties
  24. ~~~~~~~~~~~~~~~~
  25. The current interfaces for the 3D joint nodes don't quite line up with the interface
  26. of Jolt's own joints. As such, there are a number of joint properties that are not
  27. supported, mainly ones related to configuring the joint's soft limits.
  28. The unsupported properties are:
  29. - PinJoint3D: ``bias``, ``damping``, ``impulse_clamp``
  30. - HingeJoint3D: ``bias``, ``softness``, ``relaxation``
  31. - SliderJoint3D: ``angular_\*``, ``\*_limit/softness``, ``\*_limit/restitution``, ``\*_limit/damping``
  32. - ConeTwistJoint3D: ``bias``, ``relaxation``, ``softness``
  33. - Generic6DOFJoint3D: ``*_limit_*/softness``, ``*_limit_*/restitution``, ``*_limit_*/damping``, ``*_limit_*/erp``
  34. Currently a warning is emitted if you set these properties to anything but their
  35. default values.
  36. Single-body joints
  37. ~~~~~~~~~~~~~~~~~~
  38. You can, in Godot, omit one of the joint bodies for a two-body joint and effectively
  39. have "the world" be the other body. However, the node path that you assign your body
  40. to (:ref:`node_a<class_Joint3D_property_node_a>` vs :ref:`node_b<class_Joint3D_property_node_b>`)
  41. is ignored. Godot Physics will always behave as if you
  42. assigned it to ``node_a``, and since ``node_a`` is also what defines the frame of reference
  43. for the joint limits, you end up with inverted limits and a potentially strange
  44. limit shape, especially if your limits allow both linear and angular degrees of
  45. freedom.
  46. Jolt will behave as if you assigned the body to ``node_b`` instead, with ``node_a``
  47. representing "the world". There is a project setting called :ref:`Physics > Jolt Physics 3D > Joints > World Node<class_ProjectSettings_property_physics/jolt_physics_3d/joints/world_node>`
  48. that lets you toggle this behavior, if you need compatibility for an existing project.
  49. Collision margins
  50. ~~~~~~~~~~~~~~~~~
  51. Jolt (and other similar physics engines) uses something that Jolt refers to as
  52. "convex radius" to help improve the performance and behavior of the types of
  53. collision detection that Jolt relies on for convex shapes. Other physics engines
  54. (Godot included) might refer to these as "collision margins" instead. Godot exposes
  55. these as the ``margin`` property on every Shape3D-derived class, but Godot Physics
  56. itself does not use them for anything.
  57. What these collision margins sometimes do in other engines (as described in Godot's
  58. documentation) is effectively add a "shell" around the shape, slightly increasing
  59. its size while also rounding off any edges/corners. In Jolt however, these margins
  60. are first used to shrink the shape, and then the "shell" is applied, resulting in
  61. edges/corners being similarly rounded off, but without increasing the size of the
  62. shape.
  63. To prevent having to tweak this margin property manually, since its default value
  64. can be problematic for smaller shapes, the Jolt module exposes a project setting
  65. called :ref:`Physics > Jolt Physics 3D > Collisions > Collision Margin Fraction<class_ProjectSettings_property_physics/jolt_physics_3d/collisions/collision_margin_fraction>`
  66. which is multiplied with the smallest axis of the shape's AABB to calculate the
  67. actual margin. The margin property of the shape is then instead used as an upper
  68. bound.
  69. These margins should, for most use-cases, be more or less transparent, but can
  70. sometimes result in odd collision normals when performing shape queries. You can
  71. lower the above mentioned project setting to mitigate some of this, including
  72. setting it to ``0.0``, but too small of a margin can also cause odd collision results,
  73. so is generally not recommended.
  74. Baumgarte stabilization
  75. ~~~~~~~~~~~~~~~~~~~~~~~
  76. Baumgarte stabilization is a method to resolve penetrating bodies and push them to a
  77. state where they are just touching. In Godot Physics this works like a spring. This
  78. means that bodies can accelerate and may cause the bodies to overshoot and separate
  79. completely. With Jolt, the stabilization is only applied to the position and not to
  80. the velocity of the body. This means it cannot overshoot but it may take longer to
  81. resolve the penetration.
  82. The strength of this stabilization can be tweaked using the project setting
  83. :ref:`Physics > Jolt Physics 3D > Simulation > Baumgarte Stabilization Factor<class_ProjectSettings_property_physics/jolt_physics_3d/simulation/baumgarte_stabilization_factor>`.
  84. Setting this project setting to ``0.0`` will turn Baumgarte stabilization off.
  85. Setting it to ``1.0`` will resolve penetration in 1 simulation step. This is fast
  86. but often also unstable.
  87. Ghost collisions
  88. ~~~~~~~~~~~~~~~~
  89. Jolt employs two techniques to mitigate ghost collisions, meaning collisions with
  90. internal edges of shapes/bodies that result in collision normals that oppose the
  91. direction of movement.
  92. The first technique, called "active edge detection", marks edges of triangles in
  93. :ref:`class_ConcavePolygonShape3D` or :ref:`class_HeightMapShape3D` as either "active" or "inactive", based on
  94. the angle to the neighboring triangle. When a collision happens with an inactive
  95. edge the collision normal will be replaced with the triangle's normal instead, to
  96. lessen the effect of ghost collisions.
  97. The angle threshold for this active edge detection is configurable through the
  98. project setting :ref:`Physics >Jolt Physics 3D > Collisions > Active Edge Threshold<class_ProjectSettings_property_physics/jolt_physics_3d/collisions/active_edge_threshold>`.
  99. The second technique, called "enhanced internal edge removal", instead adds runtime
  100. checks to detect whether an edge is active or inactive, based on the contact points
  101. of the two bodies. This has the benefit of applying not only to collisions with
  102. :ref:`class_ConcavePolygonShape3D` and :ref:`class_HeightMapShape3D`, but also edges between any shapes within
  103. the same body.
  104. Enhanced internal edge removal can be toggled on and off for the various contexts to
  105. which it's applied, using the :ref:`Physics >Jolt Physics 3D > Simulation > Use Enhanced Internal Edge Removal<class_ProjectSettings_property_physics/jolt_physics_3d/simulation/use_enhanced_internal_edge_removal>`,
  106. project setting, and the similar settings for :ref:`queries<class_ProjectSettings_property_physics/jolt_physics_3d/queries/use_enhanced_internal_edge_removal>`
  107. and :ref:`motion queries<class_ProjectSettings_property_physics/jolt_physics_3d/motion_queries/use_enhanced_internal_edge_removal>`.
  108. Note that neither the active edge detection nor enhanced internal edge removal apply
  109. when dealing with ghost collisions between two different bodies.
  110. Memory usage
  111. ~~~~~~~~~~~~
  112. Jolt uses a stack allocator for temporary allocations within its simulation step.
  113. This stack allocator requires allocating a set amount of memory up front, which can
  114. be configured using the :ref:`Physics > Jolt Physics 3D > Limits > Temporary Memory Buffer Size<class_ProjectSettings_property_physics/jolt_physics_3d/limits/temporary_memory_buffer_size>`
  115. project setting.
  116. Ray-cast face index
  117. ~~~~~~~~~~~~~~~~~~~
  118. The ``face_index`` property returned in the results of :ref:`intersect_ray()<class_PhysicsDirectSpaceState3D_method_intersect_ray>`
  119. and RayCast3D will by default always be ``-1`` with Jolt. The project setting :ref:`Physics > Jolt Physics 3D > Queries > Enable Ray Cast Face Index<class_ProjectSettings_property_physics/jolt_physics_3d/queries/enable_ray_cast_face_index>`
  120. will enable them.
  121. Note that enabling this setting will increase the memory requirement of :ref:`class_ConcavePolygonShape3D`
  122. with about 25%.
  123. Kinematic RigidBody3D contacts
  124. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  125. When using Jolt, a :ref:`class_RigidBody3D` frozen with :ref:`FREEZE_MODE_KINEMATIC<class_RigidBody3D_constant_FREEZE_MODE_KINEMATIC>`
  126. will by default not report contacts from collisions with other static/kinematic
  127. bodies, for performance reasons, even when setting a non-zero :ref:`max_contacts_reported<class_RigidBody3D_property_max_contacts_reported>`.
  128. If you have many/large kinematic bodies overlapping with complex static geometry,
  129. such as :ref:`class_ConcavePolygonShape3D` or :ref:`class_HeightMapShape3D`, you can
  130. end up wasting a significant amount of CPU performance and memory without realizing
  131. it.
  132. For this reason this behavior is opt-in through the project setting
  133. :ref:`Physics > Jolt Physics 3D > Simulation > Generate All Kinematic Contacts<class_ProjectSettings_property_physics/jolt_physics_3d/simulation/generate_all_kinematic_contacts>`.
  134. Contact impulses
  135. ~~~~~~~~~~~~~~~~
  136. Due to limitations internal to Jolt, the contact impulses provided by :ref:`PhysicsDirectBodyState3D.get_contact_impulse()<class_physicsdirectbodystate3d_method_get_contact_impulse>`
  137. are estimated ahead of time based on things like the contact manifold and velocities
  138. of the colliding bodies. This means that the reported impulses will only be accurate
  139. in cases where the two bodies in question are not colliding with any other bodies.
  140. Area3D and SoftBody3D
  141. ~~~~~~~~~~~~~~~~~~~~~
  142. Jolt does not currently support any interactions between :ref:`class_SoftBody3D`
  143. and :ref:`class_Area3D`, such as the wind and gravity properties found on
  144. :ref:`class_Area3D`.
  145. WorldBoundaryShape3D
  146. ~~~~~~~~~~~~~~~~~~~~
  147. :ref:`class_WorldBoundaryShape3D`, which is meant to represent an infinite plane, is
  148. implemented a bit differently in Jolt compared to Godot Physics. Both engines have
  149. an upper limit for how big the effective size of this plane can be, but this size is
  150. much smaller when using Jolt, in order to avoid precision issues.
  151. You can configure this size using the :ref:`Physics > Jolt Physics 3D > Limits > World Boundary Shape Size<class_ProjectSettings_Property_physics/jolt_physics_3d/limits/world_boundary_shape_size>`
  152. project setting.
  153. Notable differences to the Godot Jolt extension
  154. -----------------------------------------------
  155. While the built-in Jolt module is largely a straight port of the Godot Jolt
  156. extension, there are a few things that are different.
  157. Project settings
  158. ~~~~~~~~~~~~~~~~
  159. All project settings have been moved from the ``physics/jolt_3d`` category to
  160. ``physics/jolt_physics_3d``.
  161. On top of that, there's been some renaming and refactoring of the individual project
  162. settings as well. These include:
  163. - ``sleep/enabled`` is now ``simulation/allow_sleep.``
  164. - ``sleep/velocity_threshold`` is now ``simulation/sleep_velocity_threshold.``
  165. - ``sleep/time_threshold`` is now ``simulation/sleep_time_threshold.``
  166. - ``collisions/use_shape_margins`` is now ``collisions/collision_margin_fraction``,
  167. where a value of 0 is equivalent to disabling it.
  168. - ``collisions/use_enhanced_internal_edge_removal`` is now ``simulation/use_enhanced_internal_edge_removal.``
  169. - ``collisions/areas_detect_static_bodies`` is now ``simulation/areas_detect_static_bodies.``
  170. - ``collisions/report_all_kinematic_contacts`` is now ``simulation/generate_all_kinematic_contacts.``
  171. - ``collisions/soft_body_point_margin`` is now ``simulation/soft_body_point_radius.``
  172. - ``collisions/body_pair_cache_enabled is now simulation/body_pair_contact_cache_enabled.``
  173. - ``collisions/body_pair_cache_distance_threshold`` is ``now simulation/body_pair_contact_cache_distance_threshold.``
  174. - ``collisions/body_pair_cache_angle_threshold is now simulation/body_pair_contact_cache_angle_threshold.``
  175. - ``continuous_cd/movement_threshold`` is now ``simulation/continuous_cd_movement_threshold``,
  176. but expressed as a fraction instead of a percentage.
  177. - ``continuous_cd/max_penetration`` is now ``simulation/continuous_cd_max_penetration``,
  178. but expressed as a fraction instead of a percentage.
  179. - ``kinematics/use_enhanced_internal_edge_removal`` is now ``motion_queries/use_enhanced_internal_edge_removal.``
  180. - ``kinematics/recovery_iterations`` is now ``motion_queries/recovery_iterations``,
  181. but expressed as a fraction instead of a percentage.
  182. - ``kinematics/recovery_amount`` is now ``motion_queries/recovery_amount.``
  183. - ``queries/use_legacy_ray_casting`` has been removed.
  184. - ``solver/position_iterations`` is now ``simulation/position_steps.``
  185. - ``solver/velocity_iterations`` is now ``simulation/velocity_steps.``
  186. - ``solver/position_correction`` is now ``simulation/baumgarte_stabilization_factor``,
  187. but expressed as a fraction instead of a percentage.
  188. - ``solver/active_edge_threshold`` is now ``collisions/active_edge_threshold.``
  189. - ``solver/bounce_velocity_threshold`` is now ``simulation/bounce_velocity_threshold.``
  190. - ``solver/contact_speculative_distance`` is now ``simulation/speculative_contact_distance.``
  191. - ``solver/contact_allowed_penetration`` is now ``simulation/penetration_slop.``
  192. - ``limits/max_angular_velocity`` is now stored as radians instead.
  193. - ``limits/max_temporary_memory`` is now ``limits/temporary_memory_buffer_size.``
  194. Joint nodes
  195. ~~~~~~~~~~~
  196. The joint nodes that are exposed in the Godot Jolt extension (JoltPinJoint3D,
  197. JoltHingeJoint3D, JoltSliderJoint3D, JoltConeTwistJoint3D, and JoltGeneric6DOFJoint)
  198. have not been included in the Jolt module.
  199. Thread safety
  200. ~~~~~~~~~~~~~
  201. Unlike the Godot Jolt extension, the Jolt module does have thread-safety,
  202. including support for the :ref:`Physics > 3D > Run On Separate Thread<class_ProjectSettings_Property_physics/3d/run_on_separate_thread>`
  203. project setting. However this has not been tested very thoroughly, so it should be
  204. considered experimental.