2022-03-18 17:46:08 +01:00
.. _doc_interpolation:
Interpolation
=============
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.
2023-01-12 19:43:03 +01:00
The basic idea is that you want to transition from A to B. A value `t` , represents the states in-between.
2022-03-18 17:46:08 +01:00
2023-01-12 19:43:03 +01:00
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* .
2022-03-18 17:46:08 +01:00
Between two real (floating-point) numbers, a simple interpolation is usually described as:
::
interpolation = A * (1 - t) + B * t
And often simplified to:
::
interpolation = A + (B - A) * t
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.
2023-01-12 19:29:11 +01:00
There are other types of interpolations, which will not be covered here. A recommended read afterwards is the `Bezier <doc_beziers_and_curves>` page.
2022-03-18 17:46:08 +01:00
Vector interpolation
--------------------
2023-01-12 19:30:47 +01:00
Vector types (`Vector2`) can also be interpolated, they come with handy functions to do it
`Vector2.linear_interpolate()` .
2022-03-18 17:46:08 +01:00
2023-01-12 19:30:47 +01:00
For cubic interpolation, there are also `Vector2.cubic_interpolate()` style interpolation.
2022-03-18 17:46:08 +01:00
Here is simple pseudo-code for going from point A to B using interpolation:
2023-01-12 18:31:02 +01:00
gdscript GDScript
2022-03-18 17:46:08 +01:00
2023-01-12 18:31:02 +01:00
```
2022-03-18 17:46:08 +01:00
var t = 0.0
func _physics_process(delta):
t += delta * 0.4
$Sprite.position = $A.position.linear_interpolate($B.position, t)
2023-01-12 18:31:02 +01:00
```
2022-03-18 17:46:08 +01:00
It will produce the following motion:
.. image:: img/interpolation_vector.gif
Transform interpolation
-----------------------
It is also possible to interpolate whole transforms (make sure they have either uniform scale or, at least, the same non-uniform scale).
2023-01-12 19:30:47 +01:00
For this, the function `Transform.interpolate_with()` can be used.
2022-03-18 17:46:08 +01:00
Here is an example of transforming a monkey from Position1 to Position2:
.. image:: img/interpolation_positions.png
Using the following pseudocode:
2023-01-12 18:31:02 +01:00
gdscript GDScript
2022-03-18 17:46:08 +01:00
2023-01-12 18:31:02 +01:00
```
2022-03-18 17:46:08 +01:00
var t = 0.0
func _physics_process(delta):
t += delta
$Monkey.transform = $Position1.transform.interpolate_with($Position2.transform, t)
2023-01-12 18:31:02 +01:00
```
2022-03-18 17:46:08 +01:00
And again, it will produce the following motion:
.. image:: img/interpolation_monkey.gif
Smoothing motion
----------------
Interpolation can be used to smooth movement, rotation, etc. Here is an example of a circle following the mouse using smoothed motion:
2023-01-12 18:31:02 +01:00
gdscript GDScript
2022-03-18 17:46:08 +01:00
2023-01-12 18:31:02 +01:00
```
2022-03-18 17:46:08 +01:00
const FOLLOW_SPEED = 4.0
func _physics_process(delta):
var mouse_pos = get_local_mouse_position()
$Sprite.position = $Sprite.position.linear_interpolate(mouse_pos, delta * FOLLOW_SPEED)
2023-01-12 18:31:02 +01:00
```
2022-03-18 17:46:08 +01:00
Here is how it looks:
.. image:: img/interpolation_follow.gif
This useful for smoothing camera movement, allies following you (ensuring they stay within a certain range), and many other common game patterns.