What is the difference between rotation, rotationQuaternion, rotate(), addRotation()?

What’s the difference or connection?

Hello :slight_smile:

  • rotation: BABYLON.Vector3 defining the rotation of a mesh in EULER mode
  • rotationQuaternion: BABYLON.Quaternion (see it as a Vector4) defining the rotation of a mesh in QUATERNION mode
  • rotate(): Method for rotating around a specific axis
  • addRotation(): Method for adding Euler angles to the current rotation

Now if you wonder why using Quaternion, it’s usefull because it a way to avoid the Gimbal Lock problem of euler angles.

Best way to understand it is to open Blender and :

  • Rotate a cube 90° on Y axis
  • Now you cannot rotate global X anymore. Both X and Z rotation seems to be influencing the global Z axis and you are basically stuck. Rotating global X is now impossible.

gimbal_lock

++
Tricotou

3 Likes

oh,my baby,thanks for solving the problem. :grinning:

1 Like

One more hint:

If you want to use euler rotations on a mesh which has a rotation quaternion set, you must set the quaternion to null before you can rotate the mesh using eulers.

2 Likes

Absolutely.
I use to add this on GLB loader, when I don’t need quaternion :

meshes.forEach(function(mesh){
    if(mesh.rotationQuaternion){
        const rotation = mesh.rotationQuaternion.toEulerAngles();
        mesh.rotationQuaternion = null;
        mesh.rotation = rotation;
    }
})

That way you keep as well the initial rotation state even if it was Quaternion :slight_smile:

1 Like

Else, I believe you know that setting a ‘new BABYLON.vector3’ for ‘rotation’ actually nulls the rotationQuartenion.

Didn’t know !

Then I guess we can save a few lines of code :grin:

meshes.forEach(function(mesh){
    mesh.rotation = mesh.rotationQuaternion ? mesh.rotationQuaternion.toEulerAngles() : mesh.rotation;
})

Thanks for the advice.

Yup, I believe it works. I do hope it still does as I’m using this quite often :grin:
It’s kind of written here

So Euler angles and rotationQuartenion are mutually exclusive

It is as in the text. To be honest, my english is not the best so I’m not sure to fully understand ‘mutually exclusive’? If by this you mean you cannot mix (i.e. by parenting) rotationQuartenion and the ‘standard’ rotation with euler angles, yes this is true… you can’t. Reason why you can convert it (or null one and switch to the other).

I translated it using DeepL.
To set rotationQuartenion to null when using rotation.
To set rotation to null when using rotationQuartenion.

Oh, should I understand that english is also not your language? :grin: :sweat_smile: This can become confusing for this kind of subject :sweat_smile: In short, from my experience (and from the warnings in the doc), it’s best to avoid ‘mixing’ both methods (i.e. in hierarchy with animations). Choose if you want to work your object/hierarchy from either one (quartenion using ‘rotate’ or ‘rotation’ using angles from the ‘fixed’ center point of object (or pivot). To be honest, lazy me sometimes keeps with a mix :face_with_hand_over_mouth: but to be honest, it can become real messy, so you’d better avoid it.

How do you convert rotation to rotationQuartenion :question:

Same as the other way round. By setting ‘new’.

mesh.rotationQuaternion = new BABYLON.Quaternion.RotationAxis(new BABYLON.Vector3(1, 0, -1), Math.PI / 3);

or by setting ‘new’ on rotate, i.e.:


> mesh.rotate(new BABYLON.Vector3(1, 0 -1), Math.PI / 3, BABYLON.Space.WORLD);

Check on the references from the doc here:
https://doc.babylonjs.com/features/featuresDeepDive/mesh/transforms/center_origin/rotation_quaternions
https://doc.babylonjs.com/features/featuresDeepDive/mesh/transforms/center_origin/rotation

EDIT: Oh, and actually there’s also a PG in the doc showing the conversion to euler angles. May be this will help?

Thank you very much.

To be precise it doesn’t work in this scenario:

  1. import a mesh which has a quaternion rotation set
  2. mesh.rotation.x = 0.5 will be ignored
  3. mesh.rotationQuaternion = null; mesh.rotation.x = 0.5 will do the thing

@mawa @Tricotou

Tada! :smiley:

1 Like

Tada! :grin: I can see how bad I’m with words, so let me share it with a PG :joy:

Indeed, HOWEVER, instead of nulling the quartenion first, you can straight NULL IT by simply setting a NEW VECTOR3 (it has to be ‘new’ and has to be a ‘Vector3’), like this:

You are not. I understood you perfectly. :smiley:

1 Like