Browse Source

MathUtils: Added .inverseLerp(). (#21544)

* MathUtils: Added .inverseLerp().

https://www.gamedev.net/tutorials/programming/general-and-gameplay-programming/inverse-lerp-a-super-useful-yet-often-overlooked-function-r5230/ for more information and to why its useful

* Update MathUtils.js

* Update MathUtils.js

Update variable names to match `lerp()`.

* Update MathUtils.js

Co-authored-by: Michael Herzog <[email protected]>
Hoodgail 4 years ago
parent
commit
2916db98f0
1 changed files with 16 additions and 0 deletions
  1. 16 0
      src/math/MathUtils.js

+ 16 - 0
src/math/MathUtils.js

@@ -54,6 +54,22 @@ const MathUtils = {
 
 	},
 
+	// https://www.gamedev.net/tutorials/programming/general-and-gameplay-programming/inverse-lerp-a-super-useful-yet-often-overlooked-function-r5230/
+
+	inverseLerp: function ( x, y, value ) {
+
+		if ( x !== y ) {
+
+			return ( value - x ) / ( y - x );
+
+		 } else {
+
+			return 0;
+
+		 }
+
+	},
+
 	// https://en.wikipedia.org/wiki/Linear_interpolation
 
 	lerp: function ( x, y, t ) {