Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved quaternion application #26456

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 11 additions & 12 deletions src/math/Vector3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Fixed Show fixed Hide fixed
this.y = vy + qw * ty + qz * tx - qx * tz;
this.z = vz + qw * tz + qx * ty - qy * tx;

return this;

Expand Down
6 changes: 0 additions & 6 deletions test/unit/src/math/Vector3.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
x,
y,
z,
w,
eps
} from '../../utils/math-constants.js';

Expand Down Expand Up @@ -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' );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove these unit tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These unit tests are silly. They don't actually proof the correctness of the actual rotation. A non-normalized quaternion that is applied to the rotation algorithm can only serve as a kind of fingerprint for the formulas if the arbitrary numbers that get were put in, generate a certain output. As the formulas have changed, they don't provide the same output for non-normalized quaternions. All other unit-tests that work on actual versors work perfectly fine.


} );

QUnit.todo( 'project', ( assert ) => {
Expand Down