I did not understand well lerp() function?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Titoch

I watched a tutu by HeartBeast (Godot Engine 3 - Platformer Game Tutorial P3 - Smooth Character Movement) and he use the lerp() function to make the movement look smoother and i dont realy understand how the function work and even after i read the docs about it.
This is my code:

extends KinematicBody2D

const UP = Vector2(0, -1)
const ACC = 50
const GRAVITY = 20
const SPEED = 200
const JUMP_HEIGHT =  -550
var motion = Vector2()

func _physics_process(delta): 
	motion.y += GRAVITY
	if he_goes_right():
		$Sprite.flip_h = false
		motion.x = min(motion.x + ACC, SPEED)
	elif he_goes_left():
		$Sprite.flip_h = true
		motion.x = max(motion.x - ACC, -SPEED)
		motion.x = lerp(motion.x, 0, 0.2)
	if is_on_floor(): 
		if Input.is_action_just_pressed("ui_up"):
	motion = move_and_slide(motion, UP)
func he_goes_right():
	return Input.get_action_strength("ui_right")
func he_goes_left():
	return Input.get_action_strength("ui_left")

func jump():
	motion.y = JUMP_HEIGHT
:bust_in_silhouette: Reply From: Freeman

Lerp is just a linear interpolation. In case of movement, it is a way to find ‘points’ between two coordinates of your movement. If you move from Vector2(0,0) to Vector2(45, 45), lerp finds other points in the straight line between these points using float as X and Y values, creating possibly the most smooth, straight line between two coordinates.

:bust_in_silhouette: Reply From: Magso

Lerp is basically (from, to, how far between 0 and 1) so lerp(0, 10, 0.5) will equal 5. There is a commonly used easing trick becuase lerp only executes once when it is called it needs to go in the _process or _physics_process functions and they have the delta parameter which is the time elapsed since the last frame in seconds. By putting a current value in the first argument and delta in the last argument like this: value = lerp(value, 10, delta) it will ease the value to 10 because it’s constantly updating the ‘from’ argument.

:bust_in_silhouette: Reply From: 1234ab

It is not that complicated: it is like average.
The average of 0 and 10 e.g. is 5. This way the 3rd parameter is 0.5, as it is halfway there.
lerp(0,10,0.5) gives 5.
if I want to be closer to the first value, I would write a smaller number:
lerp(0,10,0.3) gives 3, because it’s 0.7 times the first value plus 0.3 times the second.
It’s the same as lerp(10,0,0.7), 3 too.
What you’re doing there is every 1/60 second reduce motion.x to 0.8 times itself, so over time it slows down to 0.
It should be the same as
motion.x *= 0.8 : is it the same?

:bust_in_silhouette: Reply From: njamster

i dont realy understand how the function work and even after i read the docs about it

I think the documentation is fine, looking at the examples it should be fairly obvious what a call tolerp() does! In the code you provided, instead of stopping the player immediately when no keys are pressed, it will slowly slow down:

motion.x = lerp(motion.x, 0, 0.2)

Here motion.x holds the player’s current movement speed. Our target value is 0. But instead of setting it directly with motion.x = 0, a lerp()-call with a weight of 0.2 is used. You could also read that as “drop the movement speed by 20%”. Assuming the player moves with the maximum speed (i.e. the value of SPEED), the first frame the player stops holding down the arrow key, it’s movement speed will drop from 200 to 160. The next frame it will drop from 160 to 128. The frame after that from 128 to ~102. And so on, until it eventually reaches a value that’s effectively zero.

EDIT: A weight of 0.2 does not reduce the value to 20% - but by 20%.

that’s makes sense now thank you.

Titoch | 2020-03-25 19:16

No, it doesn’t drop the speed TO 20%. Quite the opposite - your answer is wrong. It drops it per tick BY 20% per tick, not to 20%. eg NOT from 40 to 8, but from 40 to 32. lerp using 0.9 and speed is gone almost instantly. Change it to 0.05 and see it takes a lot longer.

Ralphie | 2020-08-30 15:00

Thanks for the hint. I edited my original answer.

njamster | 2020-08-30 16:45