GithubHelp home page GithubHelp logo

rawify / quaternion.js Goto Github PK

View Code? Open in Web Editor NEW
139.0 139.0 25.0 192 KB

A JavaScript Quaternion library

License: MIT License

JavaScript 100.00%
angle complex conjugate javascript math numbers quaternion rotation

quaternion.js's People

Contributors

aharbick avatar infusion avatar yannbriancon avatar ybr3 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

quaternion.js's Issues

Exponent calculation for quaternions appears to be missing

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 ๐Ÿ˜„

Problem with toString()?

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?

fromBetweenVectors() giving wrong answer

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?

Adding a .toEuler() function

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.

Add Typescript types

Hi Robert,

I created a PR in DefinitelyTyped to create the Typescript types for the library.

Feel free to review it!

minified javascript renames fields

E.g. in the source:

https://github.com/infusion/Quaternion.js/blob/b2c9f86679526d0cec057082ba0c7d6fd133fc1a/quaternion.js#LL810C6-L810C13

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)

Additional features prior to math.js integration

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:

  • various rounding functions
  • powers (real and quaternion)
  • exponential
  • natural logarithm
  • creation of quaternions from a modulus, argument and unit vector (and the other way around)
  • conversion of quaternions to matrix form. (different to converting a quart to a rotation matrix)
  • conversion of matrices to quaternions

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?

toEuler ZXY conversion

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!

fromEuler RPY to toEuler is broken

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.