mapValue function:
float mapValue( float inVal, float inFrom, float inTo, float outFrom, float outTo )
{
float inScale = (inFrom != inTo)
? ( ( inVal - inFrom ) / ( inTo - inFrom ) )
: 0.0f;
float outVal = outFrom + ( inScale * ( outTo - outFrom ) );
outVal = (outFrom < outTo )
? clamp( outVal, outFrom, outTo )
: clamp( outVal, outTo, outFrom );
return outVal;
}
It takes a value, converts it to a proportion within a range, and then scales that relative to another range. Like a double-lerp.
You can use it to normalise stuff:
float minDamage = 0.0f; float maxDamage = 300.0f;
float normalisedDamage = mapValue(damange, minDamage, maxDamage, 0.0f, 1.0f);
Or you can convert from one range to another:
float brakeStrength = mapValue(timeToCollision,
0.0f, 10.0f, // seconds
1.0f, 0.2f // brake values
);
Notice in the second example that the out range is a different order to the in range.
It doesn't look like much, but I use this little fella all over the place.