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

Matrix3x2.ScaleVector calculation is incorrect for rotated transforms #1137

Open
SSteve opened this issue Mar 19, 2019 · 0 comments
Open

Matrix3x2.ScaleVector calculation is incorrect for rotated transforms #1137

SSteve opened this issue Mar 19, 2019 · 0 comments

Comments

@SSteve
Copy link

SSteve commented Mar 19, 2019

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).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant