Search code examples
flashactionscript-3mathmatrixscale

AS3: Getting the scale of a Matrix object


Most often, questions are asked about how to scale a DisplayObject, and the answer is usually to use a Matrix.

My question is, how to you GET the scale of a Matrix (scaleX and scaleY)?

There's a Matrix.scale method to set the scaleX and scaleY, but it doesn't return a value, and no other properties exist to read it back.

The reason I ask, I'm using object burried deep down into a Display list, and each may be transformed. So I use the child object's sprite.transform.concatenatedMatrix getter, but am stuck at this point on how to read the scale from it.

Any Math Wiz in the house?


Solution

  • Generally, a reliable way to isolate the scaling component in a matrix is to use the matrix in question to transform the unit vectors along the axes, and then measure the length of the resulting vectors.

    For instance, given the transform from a DisplayObject, and using the Matrix3D, the scaleX would be obtained as follows:

    transform.matrix3D.deltaTransformVector(Vector3D.X_AXIS).length
    

    Or, if you use the concatenated 2D Matrix, the scaleY would be:

    transform.concatenatedMatrix.deltaTransformPoint(new Point(0,1)).length
    

    Note that the deltaTransform* functions ignore the translation effects of the matrices, which have no effect on the scaling.