incheon
Version:
A Node.js based real-time game server
41 lines (34 loc) • 1.59 kB
JavaScript
;
class MathUtils {
// interpolate from start to end, advancing "percent" of the way
static interpolate(start, end, percent) {
return (end - start) * percent + start;
}
// interpolate from start to end, advancing "percent" of the way
//
// returns just the delta. i.e. the value that must be added to the start value
static interpolateDelta(start, end, percent) {
return (end - start) * percent;
}
// interpolate from start to end, advancing "percent" of the way
// and noting that the dimension wraps around {x >= wrapMin, x < wrapMax}
//
// returns just the delta. i.e. the value that must be added to the start value
static interpolateDeltaWithWrapping(start, end, percent, wrapMin, wrapMax) {
let wrapTest = wrapMax - wrapMin;
if (start - end > wrapTest / 2) end += wrapTest;
else if (end - start > wrapTest / 2) start += wrapTest;
if (Math.abs(start - end) > wrapTest / 3) {
console.log('wrap interpolation is close to limit. Not sure which edge to wrap to.');
}
return (end - start) * percent;
}
static interpolateWithWrapping(start, end, percent, wrapMin, wrapMax) {
let interpolatedVal = start + this.interpolateDeltaWithWrapping(start, end, percent, wrapMin, wrapMax);
let wrapLength = wrapMax - wrapMin;
if (interpolatedVal >= wrapLength) interpolatedVal -= wrapLength;
if (interpolatedVal < 0) interpolatedVal += wrapLength;
return interpolatedVal;
}
}
module.exports = MathUtils;