Jump to content

Matrix decompose scaling bug


satguru
 Share

Recommended Posts

@Deltakosh

The matrix decompose returns wrong scale value when scale is negative

see

https://www.babylonjs-playground.com/#ZXZI83

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

I think the bug got introduced by this change

https://github.com/BabylonJS/Babylon.js/commit/8d1b546a20b3398556f0fe98f6d57329bce92687#diff-ccd6f7b0cc7c001d6fdf41cff591c6a6

Fixed matrix decomposition with negative scaling

 

Link to comment
Share on other sites

@Deltakosh

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 getRotationMatrixToRef(). 
https://github.com/BabylonJS/Babylon.js/blob/0d75ccca5ce75ae46b19adf3aab5078706c5a07a/src/Math/babylon.math.ts#L3444

 Won't m[3],m[7],m[11] always be zero?

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

 

Link to comment
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.

https://www.babylonjs-playground.com/#ZXZI83#1

Link to comment
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?

 

Link to comment
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

Link to comment
Share on other sites

@Deltakosh

 

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 


 

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...