interpolation.rst 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. .. _doc_interpolation:
  2. Interpolation
  3. =============
  4. Introduction
  5. ~~~~~~~~~~~~
  6. Interpolation is a very basic operation in graphics programming. It's good to become familiar with it in order to expand your horizons as a graphics developer.
  7. The basic idea is that you want to transition from A to B. A value *t*, represents the states in-between.
  8. As an example if *t* is 0, then the state is A. If *t* is 1, then the state is B. Anything in-between is an *interpolation*.
  9. Between two real (floating point) numbers, a simple interpolation is usually described as:
  10. .. tabs::
  11. .. code-tab:: gdscript GDScript
  12. interpolation = A * (t-1) + B * t
  13. And often simplified to:
  14. .. tabs::
  15. .. code-tab:: gdscript GDScript
  16. interpolation = A + (B - A) * t
  17. which is exactly the same.
  18. The name of this type of interpolation, which transforms a value into another at *constant speed* is *"Linear"*. So, when you hear about *Linear Interpolation*, you know they are referring to this simple formula.
  19. There are other types of interpolations, which will not be covered here. A recommended read afterwards is the :ref:`Bezier <doc_beziers_and_curves>` page.
  20. Vector Interpolation
  21. --------------------
  22. Vector types (Vector2 and Vector3) can also be interpolated, they come with handy functions to do it
  23. :ref:`Vector2.linear_interpolate()<class_Vector2_method_linear_interpolate>` and :ref:`Vector3.linear_interpolate()<class_Vector3_method_linear_interpolate>`.
  24. For cubic interpolation, there are also :ref:`Vector2.cubic_interpolate()<class_Vector2_method_linear_interpolate>` and :ref:`Vector3.cubic_interpolate()<class_Vector3_method_linear_interpolate>`, which do a :ref:`Bezier <doc_beziers_and_curves>` style interpolation.
  25. Here is simple pseudo-code in going from point A to B using interpolation:
  26. .. tabs::
  27. .. code-tab:: gdscript GDScript
  28. func _physics_process(delta):
  29. t+=delta*0.4
  30. $Sprite.position = $A.position.linear_interpolate( $B.position,t )
  31. It will produce the following motion:
  32. .. image:: img/interpolation_vector.gif
  33. Transform interpolation
  34. --------------------
  35. It is also possible to interpolate whole transforms (make sure they have either uniform scale or, at least, the same non-uniform scale).
  36. For this, the function :ref:`Transform.interpolate_with()<class_Transform_method_interpolate_with>` can be used.
  37. Here is an example of transforming a monkey from Position1 to Position2:
  38. .. image:: img/interpolation_positions.png
  39. Using the following pseudocode:
  40. .. tabs::
  41. .. code-tab:: gdscript GDScript
  42. var t = 0.0
  43. func _process(delta):
  44. t+=delta
  45. $Monkey.transform = $Position1.transform.interpolate_with( $Position2.transform, t )
  46. And again, it will produce the following motion:
  47. .. image:: img/interpolation_monkey.gif
  48. Smoothing Motion
  49. ----------------
  50. Interpolation can be used to smooth movement, rotation, etc. Here is an example of a circle following the mouse using smoothed motion:
  51. .. tabs::
  52. .. code-tab:: gdscript GDScript
  53. const FOLLOW_SPEED = 4.0
  54. func _physics_process(delta):
  55. var mouse_pos = get_local_mouse_position()
  56. $Sprite.position = $Sprite.position.linear_interpolate( mouse_pos, delta * FOLLOW_SPEED )
  57. Here is how it looks:
  58. .. image:: img/interpolation_follow.gif
  59. This useful for smoothing camera movement, allies following you (ensuring they stay within a certain range), and many other common game patterns.