rawify / quaternion.js Goto Github PK
View Code? Open in Web Editor NEWA JavaScript Quaternion library
License: MIT License
A JavaScript Quaternion library
License: MIT License
In quaternion.d.ts
, toAxisAngle()
is defined on the Quaternion namespace as a function alongside fromAxisAngle()
, when it should be a method on the class declaration (according to the docs, anyway).
When interpreting a quaternion as a relative rotation (and not an orientation), you could e.g. want to apply only half or only 30% of a rotation. The computation of this appears to be commonly notated with q as base and e.g. 0.5 or 0.3 as exponent: q ** 0.3 (as described here: https://math.stackexchange.com/questions/162863/how-to-get-a-part-of-a-quaternion-e-g-get-half-of-the-rotation-of-a-quaternion/162892#162892 )
However, this concept of an exponentiation of a quaternion (with the quaternion as a base and a float as an exponent) seems to be missing from Quaternion.js. There is an exp
function, but it doesn't have any parameter for the exponent as I would have expected it.
Is there any chance this could be added?
EDIT: sorry, I missed the pow
function. Closing this ticket, my mistake ๐
I obtain the following behavior:
const q = new Quaternion(0.0, 0.0, 0.0, 1.0);
const str = q.toString(); // "k"
Is this expected?
When I edit the test should rotate one vector onto the other
to use these values:
var u = [0,0,1];
var v = [0,0,-1];
I get this failure:
1 failing
1) Quaternions
should rotate one vector onto the other:
AssertionError [ERR_ASSERTION]: '-k' == 'k'
+ expected - actual
--k
+k
at Function.assert.q (tests/quaternion.test.js:38:12)
at Context.<anonymous> (tests/quaternion.test.js:713:12)
at processImmediate (internal/timers.js:464:21)
Unless I'm missing something, this should be a simple rotation of 180 degrees (represented as a Quaternion of course). Is this some sort of edge condition?
Being able to convert to Euler angles (and not just from) would be very nice IMO. Let me know what you think and if you agree I'm happy to implement it and submit a PR.
Hi Robert,
I created a PR in DefinitelyTyped to create the Typescript types for the library.
Feel free to review it!
E.g. in the source:
it returns:
return {
// X-axis rotation
roll: Math.atan2(2 * (w * x + y * z), 1 - 2 * (x * x + y * y)),
// Y-axis rotation
pitch: t >= 1 ? Math.PI / 2 : (t <= -1 ? -Math.PI / 2 : Math.asin(t)),
// Z-axis rotation
yaw: Math.atan2(2 * (w * z + x * y), 1 - 2 * (y * y + z * z))
};
but the minified code https://github.com/infusion/Quaternion.js/blob/master/quaternion.min.js
has:
toEuler: function () {
var a = this.w,
b = this.x,
c = this.y,
d = this.z,
e = 2 * (a * c - d * b);
return {
g: Math.atan2(2 * (a * b + c * d), 1 - 2 * (b * b + c * c)),
pitch: 1 <= e ? Math.PI / 2 : -1 >= e ? -Math.PI / 2 : Math.asin(e),
h: Math.atan2(2 * (a * d + b * c), 1 - 2 * (c * c + d * d)),
};
},
where you can see two of the euler values are renamed (roll
=> g
, and yaw
=> h
)
This is a continuation of this: josdejong/mathjs#794
I think how many extra functions that are included will depend on how centered @infusion wants to keep the library on the 3D application of quaternions.
The features that I previously added in the PR that could be worth implementing are:
One thing I was confused about is on the current toMatrix()
and toMatrix4()
methods in quaternion.js would it not make more sense for them to returns 2d arrays?
Hello!
I'm trying to reproduce the output one would get when using Unity Quaternion's eulerAngles in javascript, and I believe I'm getting different results because of the order of the rotations. Unity uses ZXY, whereas this library uses ZYX for the euler conversion, correct?
I'm doing the following test:
// Quaternion.js
var w = 0.87851f;
var x = -0.24479f;
var y = -0.18215f;
var z = 0.36758f;
var quat = new Quaternion(w, x, y, z);
quat.toEuler();
// convert eah value from rad to degrees with * 180 / Math.PI
// outputs (after rad to degree conversion) x (roll): -34.724241397722885, y (pitch): -8.052555186105621, z (yaw): 47.93138878621218
// unity3d
var quat = new Quaternion(x, y, z, w);
quat.eulerAngles;
// outputs (x: 342.77, y: 328.43, z: 50.31)
Would it be possible to also support ZXY? If you point me in the right direction I can create a PR with that support.
Thanks!
If you create a Quaternion using fromEuler then use toEuler to get the inputs back out it returns completely different values. I tried to do this with a simple 45 degree pitch and received values for all 3 yaw roll pitch
Hello !
Thanks for this great minified library.
Is there a way to create a Quaternion from a matrix as the opposite of fromMatrix?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.