Friday, September 23, 2016

Linear Interpolation (LERP) with C++ and Unreal Engine 4.13.0


In Unreal Engine 4 you will eventually need Linear Interpolation between two values to do something like ping pong between two float values across Time Delta. I wrote a "ghetto" function that does this and takes the arguments min, max, factor (scale), and then the value itself, which it increments/decrements accordingly. You can also do this with trig functions across Delta Time to achieve a similar effect. You can always get Delta Time from the Overridable Tick Method.

Here is the code:

float GameHelper::IncrementLerp(float x, float v) {
    return v+=x;
}

float GameHelper::xLerp (float mMin, float mMax, float mFactor, float value) {
    if (value>=mMin && goBackwards){
        value = IncrementLerp(-mFactor,value);
        if (value<=mMin) {
            goForwards = true;
            goBackwards = false;
        }
    } else if (goForwards && value<=mMax) {
        value = IncrementLerp(mFactor,value);
        if (value>=mMax) {
            goBackwards = true;
            goForwards = false;
        }
    }
    return value;
}

I use it for sky lighting in a game I am working on. The sky light appears to be pulsating from 0.7f to 2.7f intensity. I use this function and expose other functions in Blueprints. HD demo of my latest game, Jumper utilizing the pulsating::



No comments:

Post a Comment