# Matrix decompose scaling bug

## Recommended Posts

The matrix decompose returns wrong scale value when scale is negative

see

Here scling.z is being set to -2, decompose returns +2

I think the bug got introduced by this change

Fixed matrix decomposition with negative scaling

##### Share on other sites

You can also notice that scale.y === -1.

Do you have an idea to fix it?

##### Share on other sites

I am not sure how to fix it.
Don't think the previous solution would have worked either.
That relied on the signs of each co-ordinates of the unit vectors.
I think that would only be reliable if the rotation was zero.

We might also have a similar issue with
Won't m[3],m[7],m[11] always be zero?

Anyway It is an interesting problem and I will keep looking into it.

##### Share on other sites

I don't think decompose is wrong. Once a scale is composed into a matrix, the information on which component is negative is lost as there are multiple ways to represent the same transformation.

If you change your playground to output all the values, you'll notice the y component of the scale is negative and note that rotation is not identity.

##### Share on other sites

@bghgary
The more I think about it the more I believe you are right about "Once a scale is composed into a matrix, the information on which component is negative is lost ".
It seems you can find that one of the component is negative but not which one

and maybe just knowing that one of the component is negative is good enough to figure out the correct rotation.
what about getRotationMatrixToRef()? needs to be corrected?

##### Share on other sites

check this playground example wherein I am  printing out the rotationQuaternion using different methods

I get different results

If i comment out line 12 (thus removing the negative scaling), I get correct results.

##### Share on other sites

The math are correct but having a negative scale is something we cannot detect as the extracted scale is always positive (we consider it as a vector length)

I can easily fix the computation with : https://www.babylonjs-playground.com/#ZXZI83#4

But it is a trick as I switch scale.z because I know that initial scling was negative on z

I see no solution to this problem

##### Share on other sites

23 hours ago, Deltakosh said:

I see no solution to this problem

Me too

it seems that based on just the matrix the only conclusion one can  definitely arrive at is
i) one scale is -negative  or all scales are negative  or
ii) one scale is positive  or all scales are positive
but not which scale exactly.

This would be based on taking cross product of two vectors and  then dot product with the third vector
So
if (Vector3.Cross(ux,uy).Dot(uz))<0  then one scale is negative   or all scales are negative
else  one  scale is positive or all scales are positive

Maybe mathematically impossible to determine the sign of the original  scale?

If that is the  case then should  the user provide the information ? as optional params?

Matrix.Decompose(scale,rotation,translation,signX?,signY?,signZ?);

where signX,signY,signZ would be 1 or -1

##### Share on other sites

That's a good option !

##### Share on other sites

I will work on a pr

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