03-08-2014, 01:24 AM
So, I know that this has already become a thing that people know about, I just wanted to say I found out myself how to do it, and would like to post it here as a reference for others. In case you aren't familiar with the concept of mapping ranges, it's a very commonly used formula by programmers, especially robot enthusiasts like myself. The concept treats the input value as a percent of the way through a certain range, then maps that to a percent of the way through another range.
Here's an example. Say we have a floating-point input from our joystick axis that ranges from 0 to 1, but we want it to change so it ranges from 0.5 to 1. Now, say that the current input from the joystick axis is 0.25 exactly. 0.25 is 25% of the way through the range of 0 to 1, and we want it 25% of the way through the range of 0.5 to 1.We can map it to the value we want by using this formula -- (x – a) * (d – c) / (b – a) + c -- where x ranges from a to b, and we are mapping it to the range c to d. When we substitute in our values, we get the expression (0.25 – 0) * (1 – 0.5) / (1 – 0) + 0.5. When we simplify that, we get 0.625, which is 25% of the way through the target range.
This can also be used to map to backwards ranges, i.e. from 0 to 1 to 1 to 0. If you want to use this formula to do this, make sure the target range is the backwards one. If you try to map from 1 to 0, to 0 to 1, it will derp.
Here's an example. Say we have a floating-point input from our joystick axis that ranges from 0 to 1, but we want it to change so it ranges from 0.5 to 1. Now, say that the current input from the joystick axis is 0.25 exactly. 0.25 is 25% of the way through the range of 0 to 1, and we want it 25% of the way through the range of 0.5 to 1.We can map it to the value we want by using this formula -- (x – a) * (d – c) / (b – a) + c -- where x ranges from a to b, and we are mapping it to the range c to d. When we substitute in our values, we get the expression (0.25 – 0) * (1 – 0.5) / (1 – 0) + 0.5. When we simplify that, we get 0.625, which is 25% of the way through the target range.
This can also be used to map to backwards ranges, i.e. from 0 to 1 to 1 to 0. If you want to use this formula to do this, make sure the target range is the backwards one. If you try to map from 1 to 0, to 0 to 1, it will derp.
I'M BAAAAAAACK!