lostwoods91 Posted October 12, 2016 Share Posted October 12, 2016 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! Quote Link to comment Share on other sites More sharing options...
hen Posted October 12, 2016 Share Posted October 12, 2016 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]; }; Quote Link to comment Share on other sites More sharing options...
lostwoods91 Posted October 12, 2016 Author Share Posted October 12, 2016 thank you! i'll try it soon Quote Link to comment Share on other sites More sharing options...
adam Posted October 12, 2016 Share Posted October 12, 2016 It appears that BJS does rotation in the order ZXY. That toEulers function only supports YZX. https://github.com/BabylonJS/Babylon.js/blob/master/src/Math/babylon.math.ts#L1916 https://github.com/BabylonJS/Babylon.js/blob/master/src/Math/babylon.math.ts#L1938 Quote Link to comment Share on other sites More sharing options...
hen Posted October 12, 2016 Share Posted October 12, 2016 19 minutes ago, adam said: It appears that BJS does rotation in the order ZXY. That toEulers function only supports YZX. https://github.com/BabylonJS/Babylon.js/blob/master/src/Math/babylon.math.ts#L1916 https://github.com/BabylonJS/Babylon.js/blob/master/src/Math/babylon.math.ts#L1938 I am using this function alot in our app.. it works, guaranteed Quote Link to comment Share on other sites More sharing options...
adam Posted October 12, 2016 Share Posted October 12, 2016 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? Quote Link to comment Share on other sites More sharing options...
lostwoods91 Posted October 12, 2016 Author Share Posted October 12, 2016 21 minutes ago, hen said: I am using this function alot in our app.. it works, guaranteed How do you explain my playground demo, then? How can I fix it? Thanks Quote Link to comment Share on other sites More sharing options...
hen Posted October 12, 2016 Share Posted October 12, 2016 On 10/12/2016 at 9:36 AM, lostwoods91 said: How do you explain my playground demo, then? How can I fix it? Thanks Just try this: http://playground.babylonjs.com/#C0VAG#3 Quote Link to comment Share on other sites More sharing options...
adam Posted October 12, 2016 Share Posted October 12, 2016 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. Quote Link to comment Share on other sites More sharing options...
adam Posted October 12, 2016 Share Posted October 12, 2016 On 10/12/2016 at 9:45 AM, hen said: Just try this: http://playground.babylonjs.com/#C0VAG#3 Same problem: http://playground.babylonjs.com/#C0VAG#4 Quote Link to comment Share on other sites More sharing options...
hen Posted October 12, 2016 Share Posted October 12, 2016 Weird... i must say that im still using v2.4 here. No idea what could be wrong, maybe the problem is somewhere else. Quote Link to comment Share on other sites More sharing options...
lostwoods91 Posted October 12, 2016 Author Share Posted October 12, 2016 Maybe you use it without having X and Z axis together in the same rotation...? Quote Link to comment Share on other sites More sharing options...
JohnK Posted October 13, 2016 Share Posted October 13, 2016 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. Quote Link to comment Share on other sites More sharing options...
adam Posted October 13, 2016 Share Posted October 13, 2016 I think I figured it out: http://playground.babylonjs.com/#C0VAG#10 I used the function from this page: https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles I had to modify it so that the rotation order is ZXY. Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted October 13, 2016 Share Posted October 13, 2016 Sorray late to the party:) is there a bug in our math lib? Quote Link to comment Share on other sites More sharing options...
adam Posted October 13, 2016 Share Posted October 13, 2016 There does appear to be a bug in Quaternion.toEulerAnglesToRef. Quote Link to comment Share on other sites More sharing options...
JohnK Posted October 13, 2016 Share Posted October 13, 2016 Looking at Quaternion.toEulerAnglesToRef in Babylon max it takes a parameter - a string that was intended to provide the return order. It seems it was never completed so just uses the default order. DigiHz Data 1 Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted October 14, 2016 Share Posted October 14, 2016 Happy to validate a PR here (not enough hours in a single day to do everything ) Quote Link to comment Share on other sites More sharing options...
adam Posted October 14, 2016 Share Posted October 14, 2016 I'll submit a PR this weekend. DigiHz Data 1 Quote Link to comment Share on other sites More sharing options...
DigiHz Data Posted October 14, 2016 Share Posted October 14, 2016 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. Quote Link to comment Share on other sites More sharing options...
adam Posted October 14, 2016 Share Posted October 14, 2016 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)? Quote Link to comment Share on other sites More sharing options...
DigiHz Data Posted October 14, 2016 Share Posted October 14, 2016 On 10/13/2016 at 9:17 AM, adam said: I think I figured it out: http://playground.babylonjs.com/#C0VAG#10 I used the function from this page: https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles I had to modify it so that the rotation order is ZXY. Can you give me the modificated code for ZXY ? Quote Link to comment Share on other sites More sharing options...
adam Posted October 14, 2016 Share Posted October 14, 2016 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. Quote Link to comment Share on other sites More sharing options...
adam Posted October 15, 2016 Share Posted October 15, 2016 I just submitted the pull request. This PG should work correctly once the PR is validated and the PG is updated. http://www.babylonjs-playground.com/#1FZQZH#10 JohnK and lostwoods91 2 Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted October 17, 2016 Share Posted October 17, 2016 PG updated PG works Thanks a lot @adam! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.