using_physics_interpolation.rst 7.5 KB

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