I have a function that will take a range of values, which need to be normalized to the range [-1, 1]. MathHelper.Clamp() just takes values that are outside the range and sets them to be whichever bound it exceeds. Does XNA or C# have anything that does what I’m looking for? (I’m looking through the documentation but can’t find it.)
This is the get mouse movement. The values are the difference in mouse coordinates between one tick and the next. I’m not sure what the max value for that is.
This function will take values one at a time – so there is no iterating over all values to scale them all or see what the min and max is.
This is what I currently have:
// max - min cannot == 0
private float normalizeToRange(float value, float min, float max) {
return (2.0f * (value - min) / (max - min)) -1.0f;
}
Applying the formula 2*(x-min)/(max-min) + -1 to each value should normalize it for you.
(x-min)/(max-min) gives you a value between 0 and 1
multiplying by 2 gives you a value between 0 and 2
subtracting 1 gives you a value between -1 and 1