Make one mesh follow the rotation of another, each with different scale

Hi everyone,

In this playground, I want to rotate one mesh and make the other two meshes follow. When the scaling sign (+ or -) matches, it is straightforward, but somehow I have one mesh whose scaling.z is negative, of course I cannot apply the rotation directly, the problem is I don’t know what calculation is needed in order for it to appear in the same rotation of the master mesh. Can anyone point me to the right direction? Thanks a lot!

Antony.

Do you mean relative rotation?

Yes :slight_smile:

Just tried and seems negate z and w would do the trick.

Sorry seems I over simplified my question, it is more complicated than this, that is involves also different parent rotation, in this PG if you rotate the gizmos you’ll see box3 cannot follow the correct rotation:

Okay, seems this works:

        var worldMatrix = box3.parent.computeWorldMatrix(true);

        var scaling = new BABYLON.Vector3();
        var rotation = new BABYLON.Quaternion();

        worldMatrix.decompose(scaling, rotation);

        var inversedRotation = BABYLON.Quaternion.Inverse(rotation);

        var localRotation = inversedRotation.multiply(rotationQuaternion);

        if (scaling.x < 0) {
            localRotation.x *= -1;
        }

        if (scaling.y < 0) {
            localRotation.y *= -1;
        }

        if (scaling.z < 0) {
            localRotation.z *= -1;
        }

        if (scaling.x < 0 ||
            scaling.y < 0 ||
            scaling.z < 0) {

            localRotation.w *= -1;
        }

        return localRotation;

This is the starting scene:

Just right after I rotate the gizmo a little bit it jumps to this state:

Is this what you wanted?


I thought you wanted to preserve the starting rotation of box3 and rotate it relative to how the gizmo rotation has changed. If so, you just need calculate the difference of the rotation of the gizmo or source box in each frame. Something like oldRotation - currentRotation and apply the difference to the rotation of box3.

2 Likes

Thanks roland, my initial problem is I have difficulty finding out the correct rotation when the parent of a mesh has different rotation + scaling with another, when I want them to follow the same absolute rotation visually. But seems I have found the solution in the above playground code, though I’m not 100% sure why this works. :sweat_smile:

1 Like

Try this one:

The axis viewer shows that the direction of the parent’s objects are different on the z-axis but the rotation of the meshes is the same: