toEulerAngles Conversion Issue


#1

Hi,

rotationQuaternion.toEulerAngles is not returning the proper value when we have rotated the perticular mesh in the X-axis by 180° (1π rad).

Example:
Step 1: Create mesh
Step 2: Rotate mesh in X-axis by 180°
mesh.rotateAround(pt, new BABYLON.Vector3(1, 0, 0), Math.PI / 2 * 2);
Step 3: Convert Euler angles that can be used in mesh.rotation by doing rotation quaternion
mesh.rotationQuaternion.toEulerAngles();

In this scenario, expected output for x coordinate is 3.14 but we are getting 4.440892098500626e-16

Can you please help us over here?

Thanks,
Tejashwini


#2

well 0 or 2PI are equivalent and technically represents the same angle

This is unfortunate but there is no simple way to decide between both values

Can you tell me why 0 is not good in your case?


#3

@Deltakosh, I am rotating Pi rad/ 180deg. Not 0/2Pi .


#4

Oh sorry I misunderstood!

Can you repro that in the playground? I will have a look at the math then


#5

@Deltakosh, Kindly refer this playground: https://playground.babylonjs.com/#7QYKIG#1


#6

actually the result is correct (even though it is not the one you expected) as the complete rotation angles is
(0, PI, PI)

That is the problem when converting from quaternion as multiple euler angles combinations can be used

I recommend keeping everything in euler since the beginning:
https://playground.babylonjs.com/#7QYKIG#2


#7

@Deltakosh, Please refer this latest code: https://playground.babylonjs.com/#7QYKIG#4

Basically, toEulerAngles() has issue while converting 180deg rotation. It is working fine with other angles.


#8

Do not quite understand the problem. The method rotateAbout is cumulative as you can see in this PG https://www.babylonjs-playground.com/#A1210C#37. This is what you are doing two rotations of 90 degs make 180 degs https://playground.babylonjs.com/#7QYKIG#4


#9

@JohnK, yes you are right. But I am not facing any issue while doing rotation.

Once we do mesh.rotateAround(), the rotation details will be maintained by mesh.rotationQuaternion property and mesh.rotation will become invalid. As, when a rotationQuaternion is set, the value of rotation is set to (0, 0, 0).

https://doc.babylonjs.com/features/position,_rotation,_scaling#rotationquaternion

Now, rotation details are in Quaternion, i.e mesh.rotationQuaternion will have the rotation details.

But, I want the rotation details in Euler Angles, so I will use quaternion.toEulerAngles()

https://doc.babylonjs.com/resources/rotation_conventions#warning

The Bug I am reporting here is quaternion.toEulerAngles() is not converting Quaternion to Euler Angles properly, when the rotated angle is +/-180deg.


#10

https://playground.babylonjs.com/#7QYKIG#5

As you can see when you convert the rotationQuaternion to Euler Angles and then use them on the clone of the subComponent the Euler Angles give the same rotation. As DK said for any quaternion there are more than one set of Euler angles that give the same result. It is just that the algorithm used in producing the Euler angles from the quaternion do not produce the ones you expect.


#11

Ok got it. So we can’t do anything here:(
Thank you @Deltakosh and @JohnK :slight_smile: