From 49b732896100e174fd378f33925221ece172440e Mon Sep 17 00:00:00 2001 From: Marco Fugaro Date: Sun, 17 Jan 2021 22:21:21 +0100 Subject: [PATCH 1/2] Add MathUtils.damp() --- docs/api/en/math/MathUtils.html | 11 +++++++++++ docs/api/zh/math/MathUtils.html | 11 +++++++++++ src/math/MathUtils.d.ts | 12 ++++++++++++ src/math/MathUtils.js | 8 ++++++++ 4 files changed, 42 insertions(+) diff --git a/docs/api/en/math/MathUtils.html b/docs/api/en/math/MathUtils.html index 94fb762554919e..9b80962e799ef7 100644 --- a/docs/api/en/math/MathUtils.html +++ b/docs/api/en/math/MathUtils.html @@ -71,6 +71,17 @@

[method:Float pingpong]( [param:Float x], [param:Float length] )

Returns a value that alternates between 0 and [param:Float length].

+

[method:Float damp]( [param:Float x], [param:Float y], [param:Float lambda], [param:Float dt] )

+

+ [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]. +

+

[method:Integer ceilPowerOfTwo]( [param:Number n] )

Returns the smallest power of 2 that is greater than or equal to [page:Number n].

diff --git a/docs/api/zh/math/MathUtils.html b/docs/api/zh/math/MathUtils.html index 52c195fbd27018..95367f4c898a06 100644 --- a/docs/api/zh/math/MathUtils.html +++ b/docs/api/zh/math/MathUtils.html @@ -68,6 +68,17 @@

[method:Float pingpong]( [param:Float x], [param:Float length] )

Returns a value that alternates between 0 and [param:Float length].

+

[method:Float damp]( [param:Float x], [param:Float y], [param:Float lambda], [param:Float dt] )

+

+ [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]. +

+

[method:Integer ceilPowerOfTwo]( [param:Number n] )

返回大于等于 [page:Number n] 的2的最小次幂。

diff --git a/src/math/MathUtils.d.ts b/src/math/MathUtils.d.ts index 40527595c8f846..8475602a79fe98 100644 --- a/src/math/MathUtils.d.ts +++ b/src/math/MathUtils.d.ts @@ -94,6 +94,18 @@ export namespace MathUtils { */ export function pingpong( x: number, length?: 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; + /** * @deprecated Use {@link Math#floorPowerOfTwo .floorPowerOfTwo()} */ diff --git a/src/math/MathUtils.js b/src/math/MathUtils.js index 5da636a47deab8..93a8871ec5c495 100644 --- a/src/math/MathUtils.js +++ b/src/math/MathUtils.js @@ -70,6 +70,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 ) ); + + }, + // http://en.wikipedia.org/wiki/Smoothstep smoothstep: function ( x, min, max ) { From 691bf0497d7e38dbd1b473a08bf69130f4103b3d Mon Sep 17 00:00:00 2001 From: Marco Fugaro Date: Sun, 17 Jan 2021 22:39:38 +0100 Subject: [PATCH 2/2] Add tests for MathUtils.damp() --- docs/api/en/math/MathUtils.html | 22 +++++++++++----------- docs/api/zh/math/MathUtils.html | 22 +++++++++++----------- src/math/MathUtils.d.ts | 18 +++++++++--------- src/math/MathUtils.js | 12 ++++++------ test/unit/src/math/MathUtils.tests.js | 7 +++++++ 5 files changed, 44 insertions(+), 37 deletions(-) diff --git a/docs/api/en/math/MathUtils.html b/docs/api/en/math/MathUtils.html index 9b80962e799ef7..9b50a1b3c87daa 100644 --- a/docs/api/en/math/MathUtils.html +++ b/docs/api/en/math/MathUtils.html @@ -53,6 +53,17 @@

[method:Float lerp]( [param:Float x], [param:Float y], [param:Float t] )

+

[method:Float damp]( [param:Float x], [param:Float y], [param:Float lambda], [param:Float dt] )

+

+ [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]. +

+

[method:Float mapLinear]( [param:Float x], [param:Float a1], [param:Float a2], [param:Float b1], [param:Float b2] )

[page:Float x] — Value to be mapped.
@@ -71,17 +82,6 @@

[method:Float pingpong]( [param:Float x], [param:Float length] )

Returns a value that alternates between 0 and [param:Float length].

-

[method:Float damp]( [param:Float x], [param:Float y], [param:Float lambda], [param:Float dt] )

-

- [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]. -

-

[method:Integer ceilPowerOfTwo]( [param:Number n] )

Returns the smallest power of 2 that is greater than or equal to [page:Number n].

diff --git a/docs/api/zh/math/MathUtils.html b/docs/api/zh/math/MathUtils.html index 95367f4c898a06..9f3171a2f3a263 100644 --- a/docs/api/zh/math/MathUtils.html +++ b/docs/api/zh/math/MathUtils.html @@ -50,6 +50,17 @@

[method:Float lerp]( [param:Float x], [param:Float y], [param:Float t] )

+

[method:Float damp]( [param:Float x], [param:Float y], [param:Float lambda], [param:Float dt] )

+

+ [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]. +

+

[method:Float mapLinear]( [param:Float x], [param:Float a1], [param:Float a2], [param:Float b1], [param:Float b2] )

[page:Float x] — 用于映射的值。
@@ -68,17 +79,6 @@

[method:Float pingpong]( [param:Float x], [param:Float length] )

Returns a value that alternates between 0 and [param:Float length].

-

[method:Float damp]( [param:Float x], [param:Float y], [param:Float lambda], [param:Float dt] )

-

- [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]. -

-

[method:Integer ceilPowerOfTwo]( [param:Number n] )

返回大于等于 [page:Number n] 的2的最小次幂。

diff --git a/src/math/MathUtils.d.ts b/src/math/MathUtils.d.ts index 8475602a79fe98..9314849b232ef5 100644 --- a/src/math/MathUtils.d.ts +++ b/src/math/MathUtils.d.ts @@ -85,15 +85,6 @@ export namespace MathUtils { */ export function lerp( x: number, y: number, t: number ): number; - /** - * Returns a value that alternates between 0 and length. - * - * @param x The value to pingpong. - * @param length The positive value the function will pingpong to. Default is 1. - * @return {number} - */ - export function pingpong( x: number, length?: number ): number; - /** * Smoothly interpolate a number from x toward y in a spring-like * manner using the dt to maintain frame rate independent movement. @@ -106,6 +97,15 @@ export namespace MathUtils { */ export function damp( x: number, y: number, lambda: number, dt: number ): number; + /** + * Returns a value that alternates between 0 and length. + * + * @param x The value to pingpong. + * @param length The positive value the function will pingpong to. Default is 1. + * @return {number} + */ + export function pingpong( x: number, length?: number ): number; + /** * @deprecated Use {@link Math#floorPowerOfTwo .floorPowerOfTwo()} */ diff --git a/src/math/MathUtils.js b/src/math/MathUtils.js index 93a8871ec5c495..9582bf458f9504 100644 --- a/src/math/MathUtils.js +++ b/src/math/MathUtils.js @@ -62,19 +62,19 @@ const MathUtils = { }, - // https://www.desmos.com/calculator/vcsjnyz7x4 + // http://www.rorydriscoll.com/2016/03/07/frame-rate-independent-damping-using-lerp/ - pingpong: function ( x, length = 1 ) { + damp: function ( x, y, lambda, dt ) { - return length - Math.abs( MathUtils.euclideanModulo( x, length * 2 ) - length ); + return MathUtils.lerp( x, y, 1 - Math.exp( - lambda * dt ) ); }, - // http://www.rorydriscoll.com/2016/03/07/frame-rate-independent-damping-using-lerp/ + // https://www.desmos.com/calculator/vcsjnyz7x4 - damp: function ( x, y, lambda, dt ) { + pingpong: function ( x, length = 1 ) { - return MathUtils.lerp( x, y, 1 - Math.exp( - lambda * dt ) ); + return length - Math.abs( MathUtils.euclideanModulo( x, length * 2 ) - length ); }, 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" );