Skip to content
This repository was archived by the owner on Mar 30, 2019. It is now read-only.
This repository was archived by the owner on Mar 30, 2019. It is now read-only.

Matrix3x2.ScaleVector calculation is incorrect for rotated transforms #1137

@SSteve

Description

@SSteve

Matrix3x2.ScaleVector returns new Vector2(M11, M22). This is only correct if M12 == M21 == 0. Here's an easy test:

var transform = Matrix3x2.Scaling(3.0f, 2.0f);
Debug.WriteLine($"Scaling: {transform.ScaleVector}");
// prints "Scaling: X:3 Y:2"
transform *= Matrix3x2.Rotation((float)(Math.PI / 2.0));
Debug.WriteLine($"Scaling: {transform.ScaleVector}");
// prints "Scaling: X:-1.311342E-07 Y:-8.742278E-08"

As explained in This Mathematics Stack Exchange answer, there is no real answer to "What is the scale vector of this matrix?". If you ignore the fact that the scaling can be the negative version of the rotation A + 180 degrees, you can calculate scale like this:

var scaleX = Math.Sqrt(transform.M11 * transform.M11 + transform.M12 * transform.M12);
var scaleY = Math.Sqrt(transform.M21 * transform.M21 + transform.M22 * transform.M22);
var cosA = transform.M11 / scaleX;
var cosB = transform.M22 / scaleY;
var sinA = transform.M21 / scaleY;
var sinB = transform.M12 / scaleX;

At this point, scaleX and scaleY are valid only if cosA == cosB and sinA == -sinB (within tolerance).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions