Math question: How to transpose a float range onto another so that they're proportionate

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Macryc

Hi. My math gaps have caught up with me, please help.

I have a speed range that goes from 0 to 5 (0 stop, 5 - run).

I want to transpose it onto a range between -1 to 1, so that I can use the speed value to control an animation blend3 which goes from -1 to 1 in my animation tree.

Basically, the blend3 blends 3 anims: idle(-1), walk(0), run(1). I want the blend position of this blend to be set my the speed value.

In math/Godot how do you transpose/translate a range of 0 to 5 onto -1 to 1?

Thank you.

:bust_in_silhouette: Reply From: fenobus

Does this help?

var ranging = []

for i in range(6):
	ranging.append(i)

print(ranging)

#Divide by 2.5
#Then -1
for i in range(ranging.size()):
	ranging[i] = ranging[i]/2.5 - 1

print(ranging)

RESULT
[0, 1, 2, 3, 4, 5]
[-1, -0.6, -0.2, 0.2, 0.6, 1]

:bust_in_silhouette: Reply From: jgodfrey

You’re after range_lerp(), which maps one range of values into a new range. Using your above example:

for i in range(6):
	print("org val: %s, new val: %s" % [i, range_lerp(i, 0, 5, -1, 1)])

Prints:

org val: 0, new val: -1
org val: 1, new val: -0.6
org val: 2, new val: -0.2
org val: 3, new val: 0.2
org val: 4, new val: 0.6
org val: 5, new val: 1