Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

보간

보간법은 그래픽 프로그래밍에서 매우 기본적인 작업입니다. 그래픽 개발자로써의 시야를 넓히기 위해 이와 익숙해지는 것이 좋습니다.

기본적인 개념은 상태 A를 B로 전환하는 것입니다. 이 때, 값 t 는 그 둘 사이의 상태를 나타냅니다.

For 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.

Between two real (floating-point) numbers, an interpolation can be described as:

interpolation = A * (1 - t) + B * t

또한 종종 다음과 같이 간략화됩니다:

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 formula.

이 밖에도 여러 종류의 보간법이 있지만, 여기에선 설명하지 않겠습니다. 이 글을 읽은 뒤에 Bezier 를 읽어보는 것을 권장합니다.

벡터 보간

Vector types (Vector2 and Vector3) can also be interpolated, they come with handy functions to do it Vector2.lerp() and Vector3.lerp().

삼차 보간법의 경우엔, Vector2.cubic_interpolate() 스타일의 보간을 수행합니다.

Here is example pseudo-code for going from point A to B using interpolation:

var t = 0.0

func _physics_process(delta):
    t += delta * 0.4

    $Sprite2D.position = $A.position.lerp($B.position, t)

위의 코드는 다음과 같은 모션을 보여줍니다:

../../_images/interpolation_vector.gif

변형 보간

It is also possible to interpolate whole transforms (make sure they have either uniform scale or, at least, the same non-uniform scale). For this, the function Transform3D.interpolate_with() can be used.

아래는 원숭이 모델을 Position1에서 Position2로 변형하는 예입니다:

../../_images/interpolation_positions.png

다음 의사 코드를 사용하면:

var t = 0.0

func _physics_process(delta):
    t += delta

    $Monkey.transform = $Position1.transform.interpolate_with($Position2.transform, t)

아래와 같은 모션을 볼 수 있습니다:

../../_images/interpolation_monkey.gif

부드러운 모션 만들기

보간법은 움직임이나 회전 등을 부드럽게 하는 데에도 사용할 수 있습니다. 아래는 마우스 커서를 부드럽게 따라다니는 원을 만드는 예시입니다:

const FOLLOW_SPEED = 4.0

func _physics_process(delta):
    var mouse_pos = get_local_mouse_position()

    $Sprite2D.position = $Sprite2D.position.lerp(mouse_pos, delta * FOLLOW_SPEED)

결과는 다음과 같습니다:

../../_images/interpolation_follow.gif

이것은 부드러운 카메라 움직임, 여러분을 일정 거리 내에서 따라다니는 동료, 그 밖의 수많은 일반적인 게임 패턴을 구현하는 데 도움이 됩니다.