diff --git a/src/core/math/Matrix.js b/src/core/math/Matrix.js index 5d591ee..ba20737 100644 --- a/src/core/math/Matrix.js +++ b/src/core/math/Matrix.js @@ -346,6 +346,54 @@ }; /** + * Decomposes the matrix (x, y, scaleX, scaleY, and rotation) and sets the properties on to a transform. + * @param {Transform} the transform to apply the properties to. + * @return {Transform} The transform with the newly applied properies +*/ +Matrix.prototype.decompose = function(transform) +{ + // sort out rotation / skew.. + var a = this.a, + b = this.b, + c = this.c, + d = this.d + + var skewX = Math.atan2(-c, d); + var skewY = Math.atan2(b, a); + + var delta = Math.abs(1-skewX/skewY); + + if (delta < 0.00001) + { + target.rotation = skewY; + + if (a < 0 && d >= 0) + { + target.rotation += (target.rotation <= 0) ? Math.PI : -Math.PI; + } + + transform.skew.x = transform.skew.y = 0; + + } + else + { + transform.skew.x = skewX; + transform.skew.y = skewY; + } + + // next set scale + transform.scale.x = Math.sqrt(a * a + b * b); + transform.scale.y = Math.sqrt(c * c + d * d); + + // next set position + transform.position.x = this.tx; + transform.position.y = this.ty; + + return transform; +}; + + +/** * Inverts this matrix * * @return {PIXI.Matrix} This matrix. Good for chaining method calls.