lerpy
v1.1.3
Published
Lerp with snapping
Downloads
59
Maintainers
Readme
A simple Linear interpolation function that returns the change instead of the final value and snaps it when lower than threshold.
Usage
import lerp from 'lerpy';
let change = lerp(current, target, alpha, snapThreshold);
current += change;
Why? A note
- With the change insted of the final value, you can decide to do or not do something ( for performance reasons ).
- I always want snapping of the value at some point.
You can do these two things with the regular lerp. But with the change they become more intuitive. (And the automatic snap is nice)
This is what they call in the biz "Inversion of control".
With regular lerp.
Normally, the regular lerp would give you the lerped final value, and you would set that to your variable. Like so:
import lerp from "lerp";
let speed = 1;
let targetSpeed = 0;
speed = lerp(speed, targetSpeed, 0.1);
someClass.setSpeed(speed);
Issues
- Possible bad performance: Always calling
.setSpeed
even if it's not updated :( What if that's.- We could add
speed !== targetSpeed
to update whenever it's not equal to the target. But that would mean the last tick, whenever it's equal, won't ever happen. - Also, doing the "update when not equal to target" is confusing to think about ->speed !== targetSpeed
- It's better and easier to wrap your head around "update when it changes" ->Math.abs(speedChange) !== 0
- We could add
- No snapping: Speed won't ever reach it's target
0.
To fix these issues, you end up fighting against the lerp by substracting the speed again. What it gives you, it's not what you really want.
import lerp from "lerp";
let speed = 1;
let speedChange = lerp(speed, 0, 0.1) - speed;
speed += speedChange;
speed = Math.round(speed / 1000) * 1000;
if (Math.abs(speedChange) !== 0) {
someClass.setSpeed(speed);
}
Things you need to rememeber:
- Get the change by substract the last value to the new
- Add the change back to the last value
- Do some weird decimal round.
- Change check
With my cute lerpy
You only have to thing about a few things.
- Add the change back to the last value
- Change check
let speed = 0;
let speedChange = lerp(speed, 0, 0.1, 0.001);
speed += speedChange;
if (Math.abs(speedChange) !== 0) {
someClass.setSpeed(speed);
}
Or, if you wanted it even shorter: ( Using boolean cohersion )
let speed = 0;
let speedChange = lerp(speed, 0, 0.1, 0.001);
speed += speedChange;
if (!speedChange) {
someClass.setSpeed(speed);
}
Thanks for comming to my ted Talk