/// <summary> /// Accepts a vector in local coordinates and converts it to coordinates in another Drawable's space. /// </summary> /// <param name="input">A vector in local coordinates.</param> /// <param name="other">The drawable in which space we want to transform the vector to.</param> /// <returns>The vector in other's coordinates.</returns> public Vector2i ToSpaceOfOtherObject(Vector2i input, PhysicalObject other) { if (other == this) { return(input); } return(input * Matrix * other.MatrixInverse); }
/// <summary> /// Gets a vector that is calculated using a percentile /// value of the object size. /// </summary> /// <param name="percent">Percentile.</param> /// <param name="other">Source object.</param> /// <returns>A new vector.</returns> public Vector2 PercentileVecByOtherObject(double percent, PhysicalObject other) { return(other.Size * (percent / 100)); }
private void CalculateMatrix() { PhysicalObject parent = null; Matrix = Matrix.Identity; DrawMatrix = Matrix.Identity; Vector2 scaledSize = ScaledSize; // - Check if we have a parent if (Parent != null && Parent is PhysicalObject) { parent = Parent as PhysicalObject; } // - Apply rotation to the matrix { Vector2 rotationCenter = -ComputeAnchorPosition(scaledSize, RotationCustomCenter, RotationCenter); var matrix = Matrix.CreateTranslation(rotationCenter.X, rotationCenter.Y, 0); Matrix *= Matrix.CreateRotationZ(Rotation); Matrix *= Matrix.CreateTranslation(-rotationCenter.X, -rotationCenter.Y, 0); } // - Create translation to requests point { Vector2 finalPosition = Position; if (parent != null) { // - Apply origin point finalPosition *= ComputeVectorDir(Anchor, Origin); finalPosition += ComputeAnchorPosition(parent.ScaledSize, CustomOrigin, Origin); } // - Apply anchor point finalPosition -= ComputeAnchorPosition(scaledSize, CustomAnchor, Anchor); // - Calculate matrix Matrix *= Matrix.CreateTranslation(finalPosition.X, finalPosition.Y, 0); } // - Scale with parent if (parent != null && ScaleWithParent) { Vector2 scaleReference = parent.ScaleReference; Vector2 parentScaledSize = parent.ScaledSize; Vector2 scaleFactor = parentScaledSize / scaleReference; Matrix *= Matrix.CreateScale(scaleFactor.X, scaleFactor.Y, 0); } // - If we have a parent do relative transformation if (parent != null) { Matrix *= parent.Matrix; } // - Set object size DrawMatrix *= Matrix.CreateScale(scaledSize.Width, scaledSize.Y, 0) * Matrix; DrawMatrix *= ToRelativeFloat; MatrixInverse = Matrix.Invert(Matrix); TransformationChanged?.Invoke(this, EventArgs.Empty); }