What is range_lerp() ? What are it's function ?

ari484ari484 Posts: 23Member

What is range_lerp() ? What are it's function ?


Tags :

Best Answer

Answers

  • MegalomaniakMegalomaniak Posts: 4,173Admin
    edited September 2018 Accepted Answer

    from http://docs.godotengine.org/ko/latest/classes/class_@gdscript.html#class-gdscript-range-lerp
    Maps a value from range [istart, istop] to [ostart, ostop].
    range_lerp(75, 0, 100, -1, 1) # returns 0.5

    And it was added in this commit:
    https://github.com/godotengine/godot/commit/d28da86f9ff5a70284e4a2078fa08867d4858a57


    OK, so lets look at the above example again:
    range_lerp(75, 0, 100, -1, 1) # returns 0.5

    The 75 is the integer value plugged into it, and then two ranges are given to it. If we look at the ranges given, 0 to 100 and -1 to 1 here are stated to be equivalent so the input of 75 is effectively then interpreted as 0.5(since that is three fourths, or 75 percent of the way linearly from -1 to 1) which is the value returned by the function.

    As for the term lerp, it is short for linear interpolation.

  • Erich_LErich_L Posts: 342Member

    Would it be too much to ask for someone to share what range_lerp would look like in mathematical notation?

  • xyzxyz Posts: 452Member
    edited November 26

    @Erich_L said:
    Would it be too much to ask for someone to share what range_lerp would look like in mathematical notation?

    Imho, much better name for range_lerp would be remap. Because it remaps the value from one min/max range to another.

    Math is rather simple:

    func range_lerp(value, min1, max1, min2, max2):
        var value_norm = inverse_lerp(min1, max1, value)
        return lerp( min2, max2, value_norm)
    

    while lerp and inverse_lerp are:

    func lerp(min, max, parameter):
        return min + (max-min) * parameter
    
    func inverse_lerp( min, max, value):
        return (value-min) / (max-min)
    
  • Erich_LErich_L Posts: 342Member

    Thankyou @xyz, I think
    by

        var value_norm = inverse_lerp(min1, min2, value)
    

    you meant

        var value_norm = inverse_lerp(min1, max1, value)
    
  • xyzxyz Posts: 452Member

    @Erich_L Yep, good catch! Corrected it now.

  • Erich_LErich_L Posts: 342Member

    @xyz It really isn't that complicated but it was a bit much for my brain to chew on, so thanks again this helped me a lot. Also, interestingly, even thought I have min1 and max2 set to zero and therefore the equation can be simplified significantly, I found that continuing to use range_lerp() was five percent faster than the simplified version.

  • xyzxyz Posts: 452Member
    edited November 26

    Sure. It's always better to let native code do the grunt work. Adding two numbers in native code is much faster than doing so in the script.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file