소스 검색

Add tests for MathUtils.damp()

Marco Fugaro 4 년 전
부모
커밋
691bf0497d
5개의 변경된 파일44개의 추가작업 그리고 37개의 파일을 삭제
  1. 11 11
      docs/api/en/math/MathUtils.html
  2. 11 11
      docs/api/zh/math/MathUtils.html
  3. 9 9
      src/math/MathUtils.d.ts
  4. 6 6
      src/math/MathUtils.js
  5. 7 0
      test/unit/src/math/MathUtils.tests.js

+ 11 - 11
docs/api/en/math/MathUtils.html

@@ -53,6 +53,17 @@
 		and [page:Float t] = 1 will return [page:Float y].
 		</p>
 
+		<h3>[method:Float damp]( [param:Float x], [param:Float y], [param:Float lambda], [param:Float dt] )</h3>
+		<p>
+		[page:Float x] - Current point. <br />
+		[page:Float y] - Target point. <br />
+		[page:Float lambda] - A higher lambda value will make the movement more sudden, and a lower value will make the movement more gradual. <br />
+		[page:Float dt] - Delta time in seconds.<br><br />
+
+		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].
+		</p>
+
 		<h3>[method:Float mapLinear]( [param:Float x], [param:Float a1], [param:Float a2], [param:Float b1], [param:Float b2] )</h3>
 		<p>
 		[page:Float x] — Value to be mapped.<br />
@@ -71,17 +82,6 @@
 
 		Returns a value that alternates between 0 and [param:Float length].</p>
 
-		<h3>[method:Float damp]( [param:Float x], [param:Float y], [param:Float lambda], [param:Float dt] )</h3>
-		<p>
-		[page:Float x] - Current point. <br />
-		[page:Float y] - Target point. <br />
-		[page:Float lambda] - A higher lambda value will make the movement more sudden, and a lower value will make the movement more gradual. <br />
-		[page:Float dt] - Delta time in seconds.<br><br />
-
-		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].
-		</p>
-
 		<h3>[method:Integer ceilPowerOfTwo]( [param:Number n] )</h3>
 		<p>Returns the smallest power of 2 that is greater than or equal to [page:Number n].</p>
 

+ 11 - 11
docs/api/zh/math/MathUtils.html

@@ -50,6 +50,17 @@
 		如果 [page:Float t] = 1 将会返回 [page:Float y].
 		</p>
 
+		<h3>[method:Float damp]( [param:Float x], [param:Float y], [param:Float lambda], [param:Float dt] )</h3>
+		<p>
+		[page:Float x] - Current point. <br />
+		[page:Float y] - Target point. <br />
+		[page:Float lambda] - A higher lambda value will make the movement more sudden, and a lower value will make the movement more gradual. <br />
+		[page:Float dt] - Delta time in seconds.<br><br />
+
+		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].
+		</p>
+
 		<h3>[method:Float mapLinear]( [param:Float x], [param:Float a1], [param:Float a2], [param:Float b1], [param:Float b2] )</h3>
 		<p>
 		[page:Float x] — 用于映射的值。<br />
@@ -68,17 +79,6 @@
 
 		Returns a value that alternates between 0 and [param:Float length].</p>
 
-		<h3>[method:Float damp]( [param:Float x], [param:Float y], [param:Float lambda], [param:Float dt] )</h3>
-		<p>
-		[page:Float x] - Current point. <br />
-		[page:Float y] - Target point. <br />
-		[page:Float lambda] - A higher lambda value will make the movement more sudden, and a lower value will make the movement more gradual. <br />
-		[page:Float dt] - Delta time in seconds.<br><br />
-
-		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].
-		</p>
-
 		<h3>[method:Integer ceilPowerOfTwo]( [param:Number n] )</h3>
 		<p>返回大于等于 [page:Number n] 的2的最小次幂。</p>
 

+ 9 - 9
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()}
 	 */

+ 6 - 6
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 );
 
 	},
 

+ 7 - 0
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" );