using_physics_interpolation.rst 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. .. _doc_using_physics_interpolation:
  2. Using physics interpolation
  3. ===========================
  4. How do we incorporate physics interpolation into a Godot game? Are there any
  5. caveats?
  6. We have tried to make the system as easy to use as possible, and many existing
  7. games will work with few changes. That said there are some situations which require
  8. special treatment, and these will be described.
  9. Turn on the physics interpolation setting
  10. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  11. The first step is to turn on physics interpolation in :ref:`ProjectSettings.physics/common/physics_interpolation<class_ProjectSettings_property_physics/common/physics_interpolation>`.
  12. You can now run your game.
  13. It is likely that nothing looks hugely different, particularly if you are running
  14. physics at 60 TPS or a multiple of it. However, quite a bit more is happening
  15. behind the scenes.
  16. .. tip::
  17. To convert an existing game to use interpolation, it is highly recommended that
  18. you temporarily set :ref:`ProjectSettings.physics/common/physics_ticks_per_second<class_ProjectSettings_property_physics/common/physics_ticks_per_second>`
  19. to a low value such as 10, which will make interpolation problems more obvious.
  20. Move (almost) all game logic from _process to _physics_process
  21. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  22. The most fundamental requirement for physics interpolation (which you may be doing
  23. already) is that you should be moving and performing game logic on your objects
  24. within ``_physics_process`` (which runs at a physics tick) rather than ``_process``
  25. (which runs on a rendered frame). This means your scripts should typically be doing
  26. the bulk of their processing within ``_physics_process``, including responding to
  27. input and AI.
  28. Setting the transform of objects only within physics ticks allows the automatic
  29. interpolation to deal with transforms *between* physics ticks, and ensures the game
  30. will run the same whatever machine it is run on. As a bonus, this also reduces CPU
  31. usage if the game is rendering at high FPS, since AI logic (for example) will no
  32. longer run on every rendered frame.
  33. .. note:: If you attempt to set the transform of interpolated objects *outside* the
  34. physics tick, the calculations for the interpolated position will be
  35. incorrect, and you will get jitter. This jitter may not be visible on
  36. your machine, but it *will* occur for some players. For this reason,
  37. setting the transform of interpolated objects should be avoided outside
  38. of the physics tick. Godot will attempt to produce warnings in the editor
  39. if this case is detected.
  40. .. tip:: This is only a *soft rule*. There are some occasions where you might want
  41. to teleport objects outside of the physics tick (for instance when
  42. starting a level, or respawning objects). Still, in general, you should be
  43. applying transforms from the physics tick.
  44. Ensure that all indirect movement happens during physics ticks
  45. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  46. Consider that in Godot, Nodes can be moved not just directly in your own scripts,
  47. but also by automatic methods such as tweening, animation, and navigation. All
  48. these methods should also have their timing set to operate on the physics tick
  49. rather than each frame ("idle"), **if** you are using them to move objects (*these
  50. methods can also be used to control properties that are not interpolated*).
  51. .. note:: Also consider that nodes can be moved not just by moving themselves, but
  52. also by moving parent nodes in the :ref:`SceneTree<class_SceneTree>`. The
  53. movement of parents should therefore also only occur during physics ticks.
  54. Choose a physics tick rate
  55. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  56. When using physics interpolation, the rendering is decoupled from physics, and you
  57. can choose any value that makes sense for your game. You are no longer limited to
  58. values that are multiples of the user's monitor refresh rate (for stutter-free
  59. gameplay if the target FPS is reached).
  60. As a rough guide:
  61. .. csv-table::
  62. :header: "Low tick rates (10-30)", "Medium tick rates (30-60)", "High tick rates (60+)"
  63. :widths: 20, 20, 20
  64. "Better CPU performance","Good physics behavior in complex scenes","Good with fast physics"
  65. "Add some delay to input","Good for first person games","Good for racing games"
  66. "Simple physics behaviour"
  67. .. note:: You can always change the tick rate as you develop, it is as simple as
  68. changing the project setting.
  69. Call ``reset_physics_interpolation()`` when teleporting objects
  70. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  71. Most of the time, interpolation is what you want between two physics ticks.
  72. However, there is one situation in which it may *not* be what you want. That is
  73. when you are initially placing objects, or moving them to a new location. Here, you
  74. don't want a smooth motion between where the object was (e.g. the origin) and the
  75. initial position - you want an instantaneous move.
  76. The solution to this is to call the :ref:`Node.reset_physics_interpolation<class_Node_method_reset_physics_interpolation>`
  77. function. What this function does under the hood is set the internally stored
  78. *previous transform* of the object to be equal to the *current transform*. This
  79. ensures that when interpolating between these two equal transforms, there will be
  80. no movement.
  81. Even if you forget to call this, it will usually not be a problem in most
  82. situations (especially at high tick rates). This is something you can easily leave
  83. to the polishing phase of your game. The worst that will happen is seeing a
  84. streaking motion for a frame or so when you move them - you will know when you need
  85. it!
  86. There are actually two ways to use ``reset_physics_interpolation()``:
  87. *Standing start (e.g. player)*
  88. 1) Set the initial transform
  89. 2) Call ``reset_physics_interpolation()``
  90. The previous and current transforms will be identical, resulting in no initial
  91. movement.
  92. *Moving start (e.g. bullet)*
  93. 1) Set the initial transform
  94. 2) Call ``reset_physics_interpolation()``
  95. 3) Immediately set the transform expected after the first tick of motion
  96. The previous transform will be the starting position, and the current transform
  97. will act as though a tick of simulation has already taken place. This will
  98. immediately start moving the object, instead of having a tick delay standing still.
  99. .. important:: Make sure you set the transform and call
  100. ``reset_physics_interpolation()`` in the correct order as shown
  101. above, otherwise you will see unwanted "streaking".
  102. Testing and debugging tips
  103. --------------------------
  104. Even if you intend to run physics at 60 TPS, in order to thoroughly test your
  105. interpolation and get the smoothest gameplay, it is highly recommended to
  106. temporarily set the physics tick rate to a low value such as 10 TPS.
  107. The gameplay may not work perfectly, but it should enable you to more easily see
  108. cases where you should be calling :ref:`Node.reset_physics_interpolation<class_Node_method_reset_physics_interpolation>`,
  109. or where you should be using your own custom interpolation on e.g. a
  110. :ref:`Camera3D<class_Camera3D>`. Once you have these cases fixed, you can set the
  111. physics tick rate back to the desired setting.
  112. The other great advantage to testing at a low tick rate is you can often notice
  113. other game systems that are synchronized to the physics tick and creating glitches
  114. which you may want to work around. Typical examples include setting animation blend
  115. values, which you may decide to set in ``_process()`` and interpolate manually.