getTransformationMatrix method
Returns the transformation matrix that transforms coordinates from the local coordinate system to the coordinate system of the targetSpace.
The resulting transformation is a concatenation of the transformation matrices of all the display objects along the parent chain from this object to the targetSpace. If targetSpace is null, the result will be in the coordinate system of the base object of this display object.
The resulting transformation is stored in the provided out
matrix
object, or in a new instance of GMatrix if out
is null. The resulting
matrix is returned.
If targetSpace
is this display object, the result is the identity
matrix.
If targetSpace
is the direct or indirect parent of this display object,
the returned transformation matrix will contain only translation,
rotation, and scale transformations.
If targetSpace
is null or the base object of this display object, the
returned transformation matrix will contain the entire chain of
transformations from this display object to the base object.
If targetSpace
is neither null, this display object, nor the parent or
base object of this display object, the returned transformation matrix
will contain the entire chain of transformations from this display object
up to (but not including) the first common ancestor of this display object
and the target space.
Implementation
GMatrix getTransformationMatrix(GDisplayObject? targetSpace, [GMatrix? out]) {
GDisplayObject? commonParent, currentObj;
out?.identity();
out ??= GMatrix();
if (targetSpace == this) {
return out;
}
if (targetSpace == $parent || (targetSpace == null && $parent == null)) {
out.copyFrom(transformationMatrix);
return out;
}
if (targetSpace == null || targetSpace == base) {
currentObj = this;
while (currentObj != targetSpace) {
out.concat(currentObj!.transformationMatrix);
currentObj = currentObj.$parent;
}
return out;
}
if (targetSpace.$parent == this) {
// optimization.
targetSpace.getTransformationMatrix(this, out);
out.invert();
return out;
}
/// 1 - find a common parent between this and targetSpace.
commonParent = GDisplayObject._findCommonParent(this, targetSpace);
/// 2 - move up from this to common parent.````
currentObj = this;
while (currentObj != commonParent) {
out.concat(currentObj!.transformationMatrix);
currentObj = currentObj.$parent;
}
if (commonParent == targetSpace) return out;
/// 3. move up from target until we reach common parent.
_sHelperMatrix.identity();
currentObj = targetSpace;
while (currentObj != commonParent) {
_sHelperMatrix.concat(currentObj!.transformationMatrix);
currentObj = currentObj.$parent;
}
/// 4. combine the matrices.
_sHelperMatrix.invert();
out.concat(_sHelperMatrix);
return out;
}