no slow down towards end of lerp /linear_interpolate

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

I am currently testing out lerp and linear_interpolate for movement.
I know there are other method to move a node,but I am just testing out different stuff.
I notice that there is a slow down as the node close to the destination. Is there a way to do it so the movement speed is the same all the way ?

:bust_in_silhouette: Reply From: lowpolygon

Alright…I dig a little deeper. This is what I found

timelerped += delta
position = position.linear_interpolate(targetPos,timelerped / timetolerp)

to use this , you do need to know how long you want the obj to move to destination
which is time = distance / speed. It kinda works for what I wanted to do.
Just in case anyone needing this

Using a Tween node is probably more suited in most cases :slight_smile:

Calinou | 2019-04-12 21:39

:bust_in_silhouette: Reply From: Ruckus T-Boom

You need to keep track of how far you’ve gone through the motion. Basically, you need to keep track of

  • where you started: origin
  • where you want to end up: target
  • when you started: start
  • how long you want to take: duration
  • current time: now

If you have all that information, you can do

origin.linear_interpolate(target, (now - start) / duration)

(Assuming now, start, and duration are all the same unit of time (most likely seconds).)

If you keep track of how long it has been since you started (offset) instead of start time and current time, you can shorten it to

origin.linear_interpolate(target, offset / duration)

NOTE: It’s important that you do not use the current position. You need to store your starting position and lerp between that and the target. Lerping from your current position is great for smooth dampening using a constant offset:

position = position.linear_interpolate(target, 0.9)

Thanks , this made a lot of sense to me. I am gonna try it out later. Not by my pc at the moment

lowpolygon | 2019-04-12 22:58

Thanks this help me out.

morningkingdom | 2021-03-30 18:32

:bust_in_silhouette: Reply From: TheRoque

You can use the Tween node that is just for this purpose: make one property go from one value to another, within a certain time, and with various transition curves

Here’s the doc for this node: Tween — Documentation de Godot Engine (4.x) en français

And a cool cheat sheet for the curves: