public void TestAppendMatrix() { Matrix2D copy = Matrix2D.Create(); copy.CopyFromMatrix(countMatrix); copy.AppendMatrix(identMatrix); Assert.IsTrue(countMatrix.IsEqual(copy), "multiplication with identity modified matrix"); copy.CopyFromMatrix(identMatrix); copy.AppendMatrix(countMatrix); Assert.IsTrue(countMatrix.IsEqual(copy), "multiplication with identity modified matrix"); Matrix2D countdownMatrix = Matrix2D.Create(9, 8, 7, 6, 5, 4); copy.AppendMatrix(countdownMatrix); Assert.IsTrue(CheckMatrixValues(copy, 23, 20, 55, 48, 92, 80), "wrong matrix " + copy); countdownMatrix.AppendMatrix(countMatrix); Assert.IsTrue(CheckMatrixValues(countdownMatrix, 33, 50, 25, 38, 22, 32), "wrong matrix " + copy); }
/// <summary> /// Figures out if the mask can be represented by a scissor rectangle; this is possible /// if it's just a simple (untextured) quad that is parallel to the stage axes. The 'out' /// parameter will be filled with the transformation matrix required to move the mask into /// stage coordinates. /// </summary> private bool IsRectangularMask(DisplayObject mask, Matrix2D outMatrix) { if (mask is Quad quad && quad.Texture == null) { if (mask.Stage != null) { outMatrix = mask.GetTransformationMatrix(null); } else { outMatrix.CopyFromMatrix(mask.TransformationMatrix); outMatrix.AppendMatrix(_state.ModelviewMatrix); } return((MathUtil.Equals(outMatrix.A, 0f) && MathUtil.Equals(outMatrix.D, 0f)) || (MathUtil.Equals(outMatrix.B, 0f) && MathUtil.Equals(outMatrix.C, 0f))); } return(false); }
private void UpdateMatrices() { if (_transformationMatrix != null) { _transformationMatrix.Identity(); } else { _transformationMatrix = Matrix2D.Create(); } if (_transformationMatrixToRoot != null) { _transformationMatrixToRoot.Identity(); } else { _transformationMatrixToRoot = Matrix2D.Create(); } if (_rotated) { _transformationMatrix.Translate(0, -1); _transformationMatrix.Rotate((float)Math.PI / 2.0f); } _transformationMatrix.Scale(_region.Width / _parent.Width, _region.Height / _parent.Height); _transformationMatrix.Translate(_region.X / _parent.Width, _region.Y / _parent.Height); SubTexture texture = this; while (texture != null) { _transformationMatrixToRoot.AppendMatrix(texture._transformationMatrix); texture = texture.Parent as SubTexture; } }
/// <summary> /// Creates a matrix that represents the transformation from the local coordinate system to another. /// </summary> public Matrix2D GetTransformationMatrix(DisplayObject targetSpace) { DisplayObject commonParent = null; DisplayObject currentObject; Matrix2D outMatrix = Matrix2D.Create(); outMatrix.Identity(); if (targetSpace == this) { return(outMatrix); } if (targetSpace == _parent || (targetSpace == null && _parent == null)) { outMatrix.CopyFromMatrix(TransformationMatrix); return(outMatrix); } if (targetSpace == null || targetSpace == Base) { // targetSpace 'null' represents the target coordinate of the base object. // -> move up from this to base currentObject = this; while (currentObject != targetSpace) { outMatrix.AppendMatrix(currentObject.TransformationMatrix); currentObject = currentObject._parent; } return(outMatrix); } if (targetSpace.Parent == this) { outMatrix = targetSpace.GetTransformationMatrix(this); outMatrix.Invert(); return(outMatrix); } // 1.: Find a common parent of this and the target coordinate space. commonParent = FindCommonParent(this, targetSpace); // 2.: Move up from this to common parent currentObject = this; while (currentObject != commonParent) { outMatrix.AppendMatrix(currentObject.TransformationMatrix); currentObject = currentObject._parent; } if (commonParent == targetSpace) { return(outMatrix); } // 3.: Now move up from target until we reach the common parent var sHelperMatrix = Matrix2D.Create(); sHelperMatrix.Identity(); currentObject = targetSpace; while (currentObject != commonParent) { sHelperMatrix.AppendMatrix(currentObject.TransformationMatrix); currentObject = currentObject._parent; } // 4.: Combine the two matrices sHelperMatrix.Invert(); outMatrix.AppendMatrix(sHelperMatrix); return(outMatrix); }