# Decomposing negative scaling matrices

## Recommended Posts

Consider:

``````var s = new BABYLON.Vector3();
var r = new BABYLON.Quaternion();
var t = new BABYLON.Vector3();

if (BABYLON.Matrix.Scaling(1, -1, 1).decompose(s, r, t))
{
console.log("scale: " + s);
}``````

I get the following output:

``scale: {X: 1 Y:1 Z:1}``

Naively I expected to get {1, -1, 1} back out again. What am I missing here?

Thanks

##### Share on other sites

What are the values of t and r?

##### Share on other sites

56 minutes ago, Deltakosh said:

What are the values of t and r?

r: {X: 0 Y:0 Z:0 W:0.7071067811865476}
r.toEulerAngles(): {X: 0 Y:0 Z:0}
t: {X: 0 Y:0 Z:0}

I wonder what that nonzero W component in r is doing. From the Euler angles it looks like it's not doing anything.

##### Share on other sites

I'm not 100% sure about what I'm going to tell, but this reminds me old things.

If I remember correctly most of the matrix decomposition routines can't extract non uniform scale (i.e. not the same scale value for x, y & z). I think it has something to do with finding the matrix determinant which is needed after. Well can't remember exactly but googling will tell you if I'm right or not.

##### Share on other sites

Hmm, well if the decompose function only yields correct outputs for certain inputs, it would be useful if the documentation stated these preconditions.

##### Share on other sites

1 hour ago, Eelis said:

Hmm, well if the decompose function only yields correct outputs for certain inputs, it would be useful if the documentation stated these preconditions.

Well, mixing 3 different matrices into one may lead to information loss, which means you can't decompose to get back what you had initially in some cases. There's no mystery, the decomposition only works if some specific conditions are met.

But we, you're right, documentation should state the limitations of this methods. Overall if you deal with non uniform scale be aware there could be other "issues" like that. A good general rule is to allow non uniform scaling only for final stage animation (vertex blending/bones/etc.).

If you still need to get it back I suggest you to work with a custom structure I used to call PRS: Position (Vector3), Rotation (Quaternion) and Scale (Vector3), you can easily create a matrix from it and you will always have each info stored specifically.

##### Share on other sites

The problem is that the decomposition is not unique. Several matrices can lead to same transform. Here the scale is positive because the quaternion will then rotate the mesh to achieve the same result as scaling to 1, -1, 1

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