# Euler -> Quaternion -> Euler

## Recommended Posts

Hi,

I got a small problem with conversion from rotation vector to quaternion and back to rotation vector.

I searched a bit and found this - http://www.html5gamedevs.com/topic/3159-quaternionrotationyawpitchroll-parameters-and-toeulerangles-result-should-be-the-same/page-2

so I ran the very simple test that was suggested there:

var e = new BABYLON.Vector3(0.1, 0.2, 0.3);var q = BABYLON.Quaternion.RotationYawPitchRoll(e.y, e.x, e.z); console.log(e);                 console.log(q.toEulerAngles());

The results were not what I expected :-)

I would expect that the two console outputs would be the same (with a certain epsilon), but they are way off :

e : {x: 0.1, y: 0.2, z: 0.3}toEulerAngles : {x: 1.1031300122019663, y: 0.22330745949001432, z: -0.8131717646205182}

I also tried a different order while creating the quaternion and found no good combination.

Anyone encountered this? Would be great to know if there is a simple solution, before I dive into the Quaternion class :-)

##### Share on other sites

I had this problem and I give up .

##### Share on other sites

So, I have been working on that for the last few hours.

Here is my test (if anyone wants an explanation, please let me know) - http://www.babylonjs-playground.com/#UCEF7

The main problem is the toEuler function in the quaternion class. This seems to malfunction. It is returning the wrong rotation back. This function is only used at the webVR camera, and it is (very sadly) exactly where I need it :-) .

I did further inspection. I have created a new euler to quaternion function based on this page - http://www.euclideanspace.com/maths/geometry/rotations/conversions/eulerToQuaternion/index.htm . The results were correct according to many tools, but are wrong in babylon's space. However (and this is where it gets strange) - converting from this quaternion back to euler, using a self-implemented function, returns the correct(!) euler angels. So, quaternion is wrong in bjs, euler that comes back is correct. Which made me wonder how this could be.

So, I went and checked the toRotationMatrix function of Quaternion and have compared it to the online version I found here - http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToMatrix/index.htm . There seems to be some kind of weird anomalies between the two. Could this be the reason for all of this? that somehow the quaternion being calculated is off, but then the rotation matrix brings it back to the correct form? And this is how it works correctly in BJS?

Tl;Dr

Is it possible that the toRotationMatrix in Quaternion is wrong? (I doubt it, but I can't find a different explanation to this entire thingy...)

Possibly:)

I'll investigate

##### Share on other sites

Hi DK,

Just wanted to know if it might be the case. I'll run some tests and post the results here. Seems like you have enough to do :-)

##### Share on other sites

Hi Ranaan,

If this can help, working on my own small demo from babylon 1.14 to 2.0, I've noticed the problem is located in "toEulerAngles" function. I've switched back to 1.14 version which give correct results for me.

For example:

e:  {x: 0, y: 3.141592653589793, z: 0}
toEulerAngles (2.0 version): {x: 3.141592653589793, y: 3.141592653589793, z: 0}
toEulerAngles (1.14 version): {x: 0, y: 3.141592653589793, z: 0}

I admit I've been to lazy to go further at this moment... I'll try look into this next week end if no one found the explanation before

##### Share on other sites

Hi Lerige,

thanks! Sadly this is not the issue ( i assume). 3.14159 (and so on and so on :-) ) is Math.PI, which means that the rotation in both is correct (360 degrees rotation is actually == 0). Would be however interesting to see what was changed between the two, as this is probably not the result of the same function. I'll look into that.

I did further tests. I changed most of the quaternion functions -

http://www.babylonjs-playground.com/#UCEF7#4

The rotation of all boxes is now correct. It did, however, completely (!!) messed up the entire camera rotation. Try moving the mouse and you will understand what I mean. setTarget won't work anymore as well. The rotation given to the camera (check the console log, it is the only vector that is not 0.7 in all positions) is correct.

I guess there is one more function to check, and I assume it is somewhere in the Matrix function (maybe the static LookAt, maybe others).

Changing the camera to ArcRotate totally worked and fixed everything - http://www.babylonjs-playground.com/#UCEF7#3 . So it is somewhere in the target camera class.

@DK - Do you think it is the right direction?

Another question would be - do we really need that? the toEuler function is hardly used. The only reason I need it, is to set the rotation of a (target) camera from a quaternion. Maybe adding rotationQuaternion to the target camera would simply solve this entire thing? The only problem I see is the _update function of the camera, as X and Y (of the mouse) is being used to change the X and Y of the camera's rotation. with a quaternion it would look a bit different.

I started writing as long as Wingnut... Hi de wingy!

##### Share on other sites

Actually, only unit quaternions have corresponding Euler angles.  You should try q.normalize().toEulerAngles() instead of q.toEulerAngles().

##### Share on other sites

Actually, only unit quaternions have corresponding Euler angles.  You should try q.normalize().toEulerAngles() instead of q.toEulerAngles().

Tried this, I thought this was the reason as well. http://www.babylonjs-playground.com/#UCEF7#7

Still off.

For some reason the camera still behaves funny, even after i commented out the prototype overrides (and reloaded completely, of course). Happens on your side as well?

## 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.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.