I secretly have never understood lerping

  • linearly interpolate means to “change” doesn’t it?
  • I thought LERPing was a way of getting a number to change smoothly over a set amount of time, and works better than changing the number every update because you don’t know how often an update is going to happen. But that would just be animating wouldn’t it? So what is lerping?
function init(self)
    self.t = 0
end

function update(self, dt)
    self.t = self.t + dt
    if self.t <= 1 then
        local startpos = vmath.vector3(0, 600, 0)
        local endpos = vmath.vector3(600, 0, 0)
        local pos = vmath.lerp(self.t, startpos, endpos)
        go.set_position(pos, "go")
    end
end

In this code, i understand that startpos and endpos are where you want the lerp to start and finish. But what is self.t?

1 Like

You can think of it as the distance between the two values which you are interpolating between. The range is 0-1.

0.1 = 10% of the way, 0.5 = 50% of the way, 0.9 = 90% of the way

clamp_number = function (number, low, high)
	return math.min(math.max(number, low), high)
end

lerp_number = function (start, stop, amount)
	amount = clamp_number(amount, 0, 1) 
	return((1-amount) * start + amount * stop)
end

print(lerp_number(0,1,0.4)) -- 0.4
print(lerp_number(20,100,0.1)) -- 28
print(lerp_number(99,123,0.5)) -- 111
3 Likes

I have to admit that i still do not understand why you would choose to use lerping or exactly what it does. Could any point me in the direction of an example?

It’s not usually a matter of “choosing” to lerp or not to lerp. :smiley: It’s just a mathematical function, like multiplication, sine, or normalizing a vector. It gives you a “blend” between two values based on a percentage. For example if you want the vector position halfway between two points, you just do vmath.lerp(0.5, pos1, pos2), which is exactly the same as doing pos1 + (pos2 - pos1)*0.5.

A lot of times when people talk about “lerping”, they mean lerping over time, meaning you plug some time value in as the percentage and presumably use the result every frame. That’s what go.animate() does with the go.EASING_LINEAR easing mode. That’s all there is to it. It’s not some magical thing that makes everything smooth. It’s actually the most basic interpolation mode there is.

9 Likes

@ross.grams now that is a clear explanation. Thank you.

vmath.lerp(0.5, 0, 10) is 5, isn’t it?

1 Like

Yes.

If you use print(vmath.lerp(0.5, 0, 10)), you get 5.

I’m glad this got bumped. I also never understood lerping but was too afraid to ask =D

2 Likes