Skip to content

Commit d740ca2

Browse files
committed
Use QR decomposition or it all goes wrong!
1 parent 8e04ce5 commit d740ca2

1 file changed

Lines changed: 27 additions & 11 deletions

File tree

src/gameobjects/components/TransformMatrix.js

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,11 @@ var TransformMatrix = new Class({
727727
},
728728

729729
/**
730-
* Decompose this Matrix into its translation, scale and rotation values.
730+
* Decompose this Matrix into its translation, scale and rotation values using QR decomposition.
731+
*
732+
* The result must be applied in the following order to reproduce the current matrix:
733+
*
734+
* translate -> rotate -> scale
731735
*
732736
* @method Phaser.GameObjects.Components.TransformMatrix#decomposeMatrix
733737
* @since 3.0.0
@@ -750,21 +754,33 @@ var TransformMatrix = new Class({
750754
var c = matrix[2];
751755
var d = matrix[3];
752756

753-
var a2 = a * a;
754-
var b2 = b * b;
755-
var c2 = c * c;
756-
var d2 = d * d;
757-
758-
var sx = Math.sqrt(a2 + c2);
759-
var sy = Math.sqrt(b2 + d2);
757+
var determ = a * d - b * c;
760758

761759
decomposedMatrix.translateX = matrix[4];
762760
decomposedMatrix.translateY = matrix[5];
763761

764-
decomposedMatrix.scaleX = sx;
765-
decomposedMatrix.scaleY = sy;
762+
if (a || b)
763+
{
764+
var r = Math.sqrt(a * a + b * b);
766765

767-
decomposedMatrix.rotation = Math.acos(a / sx) * (Math.atan(-c / a) < 0 ? -1 : 1);
766+
decomposedMatrix.rotation = (b > 0) ? Math.acos(a / r) : -Math.acos(a / r);
767+
decomposedMatrix.scaleX = r;
768+
decomposedMatrix.scaleY = determ / r;
769+
}
770+
else if (c || d)
771+
{
772+
var s = Math.sqrt(c * c + d * d);
773+
774+
decomposedMatrix.rotation = Math.PI * 0.5 - (d > 0 ? Math.acos(-c / s) : -Math.acos(c / s));
775+
decomposedMatrix.scaleX = determ / s;
776+
decomposedMatrix.scaleY = s;
777+
}
778+
else
779+
{
780+
decomposedMatrix.rotation = 0;
781+
decomposedMatrix.scaleX = 0;
782+
decomposedMatrix.scaleY = 0;
783+
}
768784

769785
return decomposedMatrix;
770786
},

0 commit comments

Comments
 (0)