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

Post your own tutorials, guides and demos.

### » Sun Apr 17, 2011 11:08 am

[quote="newt":3phpb3i2]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.[/quote:3phpb3i2]
That's no problem with math prior to lerp. Set a variable (we call it 'm' here) to:
[code:3phpb3i2]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
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

### » Sun Apr 17, 2011 12:51 pm

Thanks for the examples, I think I'm starting to understand how it will be useful for me.
B
135
S
65
G
17
Posts: 1,766
Reputation: 19,727

### » Sun Apr 17, 2011 1:47 pm

[quote="tulamide":1zzglhlo][quote="newt":1zzglhlo]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.[/quote:1zzglhlo]
That's no problem with math prior to lerp. Set a variable (we call it 'm' here) to:
[code:1zzglhlo]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[/quote:1zzglhlo]
It still don't want to rotate correctly
http://dl.dropbox.com/u/24325446/lerpangle.cap
B
2
S
2
G
2
Posts: 158
Reputation: 1,366

### » Sun Apr 17, 2011 2:23 pm

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)
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

### » Sun Apr 17, 2011 2:37 pm

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.
viewtopic.php?f=29&t=9155
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
Spriter Dev
B
90
S
21
G
12
Posts: 3,246
Reputation: 16,548

### » Sun Apr 17, 2011 2:45 pm

Oh, that's cool. I missed that one, indeed.
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

### » Sun Apr 17, 2011 3:04 pm

It's nice plugin, but I prefer to don't use plugins if goal can be reached with events. I think there must be way to do it with pure Construct
B
2
S
2
G
2
Posts: 158
Reputation: 1,366

### » Sun Apr 17, 2011 3:34 pm

[quote="tulamide":zsicte4h]If you can't see lerp being useful for anything, then you did not understand it.[/quote:zsicte4h]

Agreed, lerp is probably the number one most useful math function I've ever come across in game programming, with clamp as a close second. I use them both constantly, they're really indispensable. When I learned to use them properly it was like a whole new world opened up in my brains.
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

### » Sun Apr 17, 2011 4:19 pm

Let's not forget the wonder that is qerp
Did someone say SCORCHED EARTH?!?!
B
3
S
2
G
5
Posts: 351
Reputation: 2,377

### » Sun Apr 17, 2011 4:26 pm

[quote="alastair":1qf8fnnx]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.[/quote:1qf8fnnx]
this is what I meant by health bar:
http://dl.dropbox.com/u/1013446/lerpbar.cap

you could make a much cooler more complex one, but I wanted to keep it simple. your health goes up the higher your mouse is on the screen. Also, you can resize the health bar, change the screensize, etc, and this same code will still work, with no changes whatsoever

here's tulamide's example again, but using a few extra lerps in some other ways. You can change the global variable 'numberofballs' in start of layout, and you'll notice they always perfectly space apart and behave correctly, and the color blend works perfectly regardless of position. Something like this would be impossible without lerp, or some mathematical equivalent:
http://dl.dropbox.com/u/1013446/tulerpmide.cap

[quote="DtrQ":1qf8fnnx]It's nice plugin, but I prefer to don't use plugins if goal can be reached with events. I think there must be way to do it with pure Construct[/quote:1qf8fnnx]
everything in construct is a plugin. every object and behavior is created with the same sdk. the only difference is some are official plugins made by ash, davo, and rich, and some aren't. I can definitely respect wanting to understand the math well enough to do it without the new plug, and tulamide already showed how, but avoiding using the faster way once you do understand it is kinda pointless, imho. Especially since in circumstances where it takes more than one construct expression to achieve it, the plugin will always be faster, both in the time you take to write and read your own code, but also in the execution speed, as the c++ within a plugin runs faster than construct's interpreted expressions. that speed difference won't matter in most caps, but good to know anyway
Spriter Dev
B
90
S
21
G
12
Posts: 3,246
Reputation: 16,548

PreviousNext