Преглед изворни кода

Improved quaternion application (#26456)

Robert Eisele пре 1 година
родитељ
комит
783b4bb335
2 измењених фајлова са 11 додато и 18 уклоњено
  1. 11 12
      src/math/Vector3.js
  2. 0 6
      test/unit/src/math/Vector3.tests.js

+ 11 - 12
src/math/Vector3.js

@@ -251,21 +251,20 @@ class Vector3 {
 
 	applyQuaternion( q ) {
 
-		const x = this.x, y = this.y, z = this.z;
-		const qx = q.x, qy = q.y, qz = q.z, qw = q.w;
+		// Derived from https://raw.org/proof/vector-rotation-using-quaternions/
 
-		// calculate quat * vector
-
-		const ix = qw * x + qy * z - qz * y;
-		const iy = qw * y + qz * x - qx * z;
-		const iz = qw * z + qx * y - qy * x;
-		const iw = - qx * x - qy * y - qz * z;
+		const vx = this.x, vy = this.y, vz = this.z;
+		const qx = q.x, qy = q.y, qz = q.z, qw = q.w;
 
-		// calculate result * inverse quat
+		// t = 2q x v
+		const tx = 2 * ( qy * vz - qz * vy );
+		const ty = 2 * ( qz * vx - qx * vz );
+		const tz = 2 * ( qx * vy - qy * vx );
 
-		this.x = ix * qw + iw * - qx + iy * - qz - iz * - qy;
-		this.y = iy * qw + iw * - qy + iz * - qx - ix * - qz;
-		this.z = iz * qw + iw * - qz + ix * - qy - iy * - qx;
+		// v + w t + q x t
+		this.x = vx + qw * tx + qy * tz - qz * ty;
+		this.y = vy + qw * ty + qz * tx - qx * tz;
+		this.z = vz + qw * tz + qx * ty - qy * tx;
 
 		return this;
 

+ 0 - 6
test/unit/src/math/Vector3.tests.js

@@ -14,7 +14,6 @@ import {
 	x,
 	y,
 	z,
-	w,
 	eps
 } from '../../utils/math-constants.js';
 
@@ -303,11 +302,6 @@ export default QUnit.module( 'Maths', () => {
 			assert.strictEqual( a.y, y, 'Identity rotation: check y' );
 			assert.strictEqual( a.z, z, 'Identity rotation: check z' );
 
-			a.applyQuaternion( new Quaternion( x, y, z, w ) );
-			assert.strictEqual( a.x, 108, 'Normal rotation: check x' );
-			assert.strictEqual( a.y, 162, 'Normal rotation: check y' );
-			assert.strictEqual( a.z, 216, 'Normal rotation: check z' );
-
 		} );
 
 		QUnit.todo( 'project', ( assert ) => {