Jump to content

Euler/Quaternion conversion issue


lostwoods91
 Share

Recommended Posts

hi everyone!

what's wrong with this?: http://playground.babylonjs.com/#C0VAG#2

first i set an orientation vector, called eulAngles, then i apply it to a cube.

after conversion to a quaternion and viceversa, the angles are slightly wrong!

maybe i'm doing something wrong (not sure if it is a bug), but it's strange...

the conversion is correct with these rotations: X, Y, Z, XY, YZ; it is wrong with: XZ, XYZ (general case) rotations instead.

so i noticed that it is wrong in all cases X and Z are rotated together...

i've already tried switching axis orders, but nothing!

Link to comment
Share on other sites

I was dealing with the same problem, here is a a quaternoin conversion that works:

quaternionToEuler(q) {
        var heading, attitude, bank;
        var x = q[0], y = q[1], z = q[2], w = q[3];

        var test = x*y + z*w;
        if (test > 0.499) { 
            heading = 2 * Math.atan2(x,w);
            attitude = Math.PI/2;
            bank = 0;
        }
        if (test < -0.499) { 
            heading = -2 * Math.atan2(x,w);
            attitude = - Math.PI/2;
            bank = 0;
        }
        if(isNaN(heading)){
            var sqx = x*x;
            var sqy = y*y;
            var sqz = z*z;
            heading = Math.atan2(2*y*w - 2*x*z , 1 - 2*sqy - 2*sqz);
            attitude = Math.asin(2*test);
            bank = Math.atan2(2*x*w - 2*y*z , 1 - 2*sqx - 2*sqz);
        }

        return [bank, heading, attitude];
    };

 

Link to comment
Share on other sites

23 minutes ago, adam said:

Can someone explain to me why there seems to be a difference in rotation order between what BJS does and what the Quaternion.toEuler function appears to support?

I'm pretty sure I was thinking in terms of world space and I should have been thinking in local space.

Link to comment
Share on other sites

Rotations are very tricky, not least because they are non-commutative and can take place in WORLD or LOCAL space. Also the toEulerAngles function returns the angles in the order  YZX not XYZ

In BJS the use of mesh.rotation is in WORLD space and yaw, pitch, roll take place in local space.

In the example http://playground.babylonjs.com/#C0VAG#6

mesh.rotate is used and this can be set to use LOCAL space. The box is taken to be moving in the positive z direction with  roll about z axis, pitch about x axis  and yaw about y axis. The rotations are first done in the order yaw, pitch, roll. To find the quaternion the prameters must be entered in yaw, pitch, roll order. The function toEulerAngles returns yaw, roll, pitch and the final rotations are applied in this order.

In this example http://playground.babylonjs.com/#C0VAG#7

The box is taken to be moving in the positive y direction so roll about y axis, pitch about x and yaw about z

The rotation is applied to cube1 and cube 2 shows that this matches with rotate in the order ZXY in WORLD space.

The values for eulAngles in the order yaw, pitch roll, ie zxy, are used to determine the quaternion and apply it to cube3.

The euler angles are then found using toEulerAngles in the order YZX and are applied locally in that order to give the same rotation to cube4. 

Link to comment
Share on other sites

1 hour ago, adam said:

I'll submit a PR this weekend.

Plz do that @adam .I am trying to implement @satguru's edit control in my editor, and i have problems with rotation values getting back right (using euler values).

I think the function Quaternion.prototype.toEulerAnglesToRef = function (result, order) {......needs to be updated with  support for  ZXY  as well.

Maybe other orders to???

As @JohnK mentioned......the function is not complete yet. I would not call it a bug, but missing options about the order.

Link to comment
Share on other sites

5 minutes ago, DigiHz Data said:

I think the function Quaternion.prototype.toEulerAnglesToRef = function (result, order) {......needs to be updated with  support for  ZXY  as well.

Can someone prove that the current conversion is working correctly (YZX order)?

 

Link to comment
Share on other sites

The quatToRot function seems to be working for both rotation orders.  I tested it in world and local space too.

http://www.babylonjs-playground.com/#1FZQZH#7

Here is another test with random rotation:

http://www.babylonjs-playground.com/#1FZQZH#8

I don't think it's going to be necessary to specify the order of rotation when converting from quat to euler.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...