diff --git a/docs/api/en/math/MathUtils.html b/docs/api/en/math/MathUtils.html index 94fb762554919e..9b50a1b3c87daa 100644 --- a/docs/api/en/math/MathUtils.html +++ b/docs/api/en/math/MathUtils.html @@ -53,6 +53,17 @@
+ [page:Float x] - Current point.
+ [page:Float y] - Target point.
+ [page:Float lambda] - A higher lambda value will make the movement more sudden, and a lower value will make the movement more gradual.
+ [page:Float dt] - Delta time in seconds.
+
+ Smoothly interpolate a number from [page:Float x] toward [page:Float y] in a spring-like manner using the [page:Float dt] to maintain frame rate independent movement.
+ For details, see [link:http://www.rorydriscoll.com/2016/03/07/frame-rate-independent-damping-using-lerp/ Frame rate independent damping using lerp].
+
[page:Float x] — Value to be mapped.
diff --git a/docs/api/zh/math/MathUtils.html b/docs/api/zh/math/MathUtils.html
index 52c195fbd27018..9f3171a2f3a263 100644
--- a/docs/api/zh/math/MathUtils.html
+++ b/docs/api/zh/math/MathUtils.html
@@ -50,6 +50,17 @@
+ [page:Float x] - Current point.
+ [page:Float y] - Target point.
+ [page:Float lambda] - A higher lambda value will make the movement more sudden, and a lower value will make the movement more gradual.
+ [page:Float dt] - Delta time in seconds.
+
+ Smoothly interpolate a number from [page:Float x] toward [page:Float y] in a spring-like manner using the [page:Float dt] to maintain frame rate independent movement.
+ For details, see [link:http://www.rorydriscoll.com/2016/03/07/frame-rate-independent-damping-using-lerp/ Frame rate independent damping using lerp].
+
[page:Float x] — 用于映射的值。
diff --git a/src/math/MathUtils.d.ts b/src/math/MathUtils.d.ts
index 40527595c8f846..9314849b232ef5 100644
--- a/src/math/MathUtils.d.ts
+++ b/src/math/MathUtils.d.ts
@@ -85,6 +85,18 @@ export namespace MathUtils {
*/
export function lerp( x: number, y: number, t: number ): number;
+ /**
+ * Smoothly interpolate a number from x toward y in a spring-like
+ * manner using the dt to maintain frame rate independent movement.
+ *
+ * @param x Current point.
+ * @param y Target point.
+ * @param lambda A higher lambda value will make the movement more sudden, and a lower value will make the movement more gradual.
+ * @param dt Delta time in seconds.
+ * @return {number}
+ */
+ export function damp( x: number, y: number, lambda: number, dt: number ): number;
+
/**
* Returns a value that alternates between 0 and length.
*
diff --git a/src/math/MathUtils.js b/src/math/MathUtils.js
index 5da636a47deab8..9582bf458f9504 100644
--- a/src/math/MathUtils.js
+++ b/src/math/MathUtils.js
@@ -62,6 +62,14 @@ const MathUtils = {
},
+ // http://www.rorydriscoll.com/2016/03/07/frame-rate-independent-damping-using-lerp/
+
+ damp: function ( x, y, lambda, dt ) {
+
+ return MathUtils.lerp( x, y, 1 - Math.exp( - lambda * dt ) );
+
+ },
+
// https://www.desmos.com/calculator/vcsjnyz7x4
pingpong: function ( x, length = 1 ) {
diff --git a/test/unit/src/math/MathUtils.tests.js b/test/unit/src/math/MathUtils.tests.js
index 317a6ccf7b8bf6..f91efba3b549c0 100644
--- a/test/unit/src/math/MathUtils.tests.js
+++ b/test/unit/src/math/MathUtils.tests.js
@@ -55,6 +55,13 @@ export default QUnit.module( 'Maths', () => {
} );
+ QUnit.test( "damp", ( assert ) => {
+
+ assert.strictEqual( MathUtils.damp( 1, 2, 0, 0.016 ), 1, "Value equal to lower boundary" );
+ assert.strictEqual( MathUtils.damp( 1, 2, 10, 0.016 ), 1.1478562110337887, "Value within range" );
+
+ } );
+
QUnit.test( "smoothstep", ( assert ) => {
assert.strictEqual( MathUtils.smoothstep( - 1, 0, 2 ), 0, "Value lower than minimum" );