what is lerp? (2 minute tutorial for the non-math

This forum is currently in read-only mode.
1 favourites
• 33 posts
From the Asset Store
12 looping retro style music tracks for your indie games and projects
• lerp(a,b,t) is a math function that lets you find a value between two other values

• a is the first value
• b is the second value
• t is a number between 0 and 1 that decides how far between a and b you want the value to be
• 0 is all the way at a
• 1 is all the way at b
• 0.5 is halfway between

to better understand it

try this exe:

experiment with different values for a,b,t until you have a firm grasp of how it works

simply type in values for a and b, and drag the red slider to set values for t

try me!

watch the equation change, and watch the bar at the bottom

lerp can be used for everything from health bars, to color fades, smooth angle changes, to smoothly fading sound effect volume. you can also use it in a loop to set up a bunch of sprites in a neat line. there are endless uses. once you understand what it does, you'll find it comes up often as a useful tool when designing games

if you have any questions about how to do any of the above mentioned, or if lerp simply still doesn't make sense. please ask away

once you have a firm grasp on this, you can easily learn cosp, qerp, and cubic, which are even more awesome

update: here's another tutorial with qarp and cubic=

lerp/qarp/cubic tutorial

math is power

• Lerpin angles is a bit complicated, might do an example of that.

lerp(sprite.angle, new angle,x*timedelta)= boo hoo when new angle is greater than 360, or is less than 0.

Also a two node path using lerp versus qarp would be a good example.

• Great example. I'd love to see what cosp, qarp, and cubic are capable of hint hint

• This is probably the most helpful thing to happen to me in a while... thank you so much!

• Here's a little .cap I made a while ago to implement an aiming system similar to a lot of space sims (in this case, I modeled it after Privateer 2 )

It uses lerp quite a bit. Have fun

http://dl.dropbox.com/u/1289341/AimTest.cap

• Very useful!! Thanks Lucid.

• Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• Thanks Lucid, I understand it, just don't think it will be useful for anything. Things like health bars are already easy to do with variables, just subtract/add.

• Did you have a look at the cap I posted a couple of replies ago?

It uses lerp on both the X and Y coordinates of the aiming reticle to predict where to shoot to guaruntee a hit on any of the enemy objects. I'm pretty sure that functionality is now built into the system object however, but they're essentially the same thing

Although, correct me if I'm wrong, I think I've actually created quadratic interpolation with two sets of lerp in that example?

Lerp is great for extrapolating and predicting things!

Because while the third argument is usually a value between 0 and 1, you can go BEYOND 1 to start predicting where a value will be based on the other 2 values

I may have a twisted take on the whole thing, as I'm no mathmatician, I basically just learnt lerp from playing around with it on Construct!

• Thanks Lucid, I understand it, just don't think it will be useful for anything. Things like health bars are already easy to do with variables, just subtract/add.

If you can't see lerp being useful for anything, then you did not understand it. Just a very simple example:

lerp.cap

(click and drag the blue sprites)

Now recreate this without lerp. It's possible, but needs more effort. And this is just a very simple example.

• That's a great example!

• Lerpin angles is a bit complicated, might do an example of that.

lerp(sprite.angle, new angle,x*timedelta)= boo hoo when new angle is greater than 360, or is less than 0.

That's no problem with math prior to lerp. Set a variable (we call it 'm' here) to:

m = (abs(new angle) % 360) * Sign(new angle) + 360[/code:3phpb3i2]

then use lerp with:
[code:3phpb3i2]lerp(sprite.angle, m % 360,x*timedelta)[/code:3phpb3i2]

Whatever value 'new angle' has, it will be (mathematically correct) shifted to the range 0-360
• Thanks for the examples, I think I'm starting to understand how it will be useful for me.

• > Lerpin angles is a bit complicated, might do an example of that.

> lerp(sprite.angle, new angle,x*timedelta)= boo hoo when new angle is greater than 360, or is less than 0.

>

That's no problem with math prior to lerp. Set a variable (we call it 'm' here) to:

m = (abs(new angle) % 360) * Sign(new angle) + 360[/code:1zzglhlo]

then use lerp with:
[code:1zzglhlo]lerp(sprite.angle, m % 360,x*timedelta)[/code:1zzglhlo]

Whatever value 'new angle' has, it will be (mathematically correct) shifted to the range 0-360

It still don't want to rotate correctly

http://dl.dropbox.com/u/24325446/lerpangle.cap

• That's what I meant by "mathematically correct". You see, there are two problems here:

1) sprite.angle returns a value between 0-360, and it expects a positive value in th range 0-360

2) when crossing 0, the highest and lowest value change, and therefore it's lerping "backwards"

My Post solves only issue 1)

• just in case anyone missed it, my new math plugin (only has 2 expressions for now, but will be growing, and safe to use starting now) has anglelerp.

it doesn't handle negative angles or angles larger than 720, it's just for object.angles in Construct for now, but it just makes it simple since you don't have to pay attention to the 0 mark. It assumes you want the shortest path from an angle to the next, meaning, if you do anglelerp(350,10,whatever)

it'll assume you want to go up from 350 to get to 10

if you want to take the long way around, you have to find another way