Browse Source

Quaternion: Added .angleTo()

Mugen87 7 năm trước cách đây
mục cha
commit
0053135f78

+ 5 - 1
docs/api/math/Quaternion.html

@@ -59,13 +59,17 @@
 
 		<h2>Methods</h2>
 
+		<h3>[method:Float angleTo]( [param:Quaternion q] )</h3>
+		<p>
+			Returns the angle between this quaternion and quaternion [page:Quaternion q] in radians.
+		</p>
+
 		<h3>[method:Quaternion clone]()</h3>
 		<p>
 			Creates a new Quaternion with identical [page:.x x], [page:.y y],
 			[page:.z z] and [page:.w w] properties to this one.
 		</p>
 
-
 		<h3>[method:Quaternion conjugate]()</h3>
 		<p>
 		Returns the rotational conjugate of this quaternion. The conjugate of a quaternion

+ 16 - 0
src/math/Quaternion.js

@@ -393,6 +393,22 @@ Object.assign( Quaternion.prototype, {
 
 	}(),
 
+	angleTo: function () {
+
+		var p = new Quaternion();
+
+		return function angleTo( q ) {
+
+			p.copy( q ).inverse();
+
+			p.premultiply( this );
+
+			return 2 * Math.acos( p.w );
+
+		};
+
+	}(),
+
 	inverse: function () {
 
 		// quaternion is assumed to have unit length

+ 12 - 0
test/unit/src/math/Quaternion.tests.js

@@ -391,6 +391,18 @@ export default QUnit.module( 'Maths', () => {
 
 		} );
 
+		QUnit.test( "angleTo", ( assert ) => {
+
+			var a = new Quaternion();
+			var b = new Quaternion().setFromEuler( new Euler( 0, Math.PI, 0 ) );
+			var c = new Quaternion().setFromEuler( new Euler( 0, Math.PI * 2, 0 ) );
+
+			assert.ok( a.angleTo( a ) <= eps, "Passed!" );
+			assert.ok( a.angleTo( b ) - Math.PI <= eps, "Passed!" );
+			assert.ok( a.angleTo( c ) - ( Math.PI * 2 ) <= eps, "Passed!" );
+
+		} );
+
 		QUnit.test( "inverse/conjugate", ( assert ) => {
 
 			var a = new Quaternion( x, y, z, w );