sharpdx / SharpDX

SharpDX GitHub Repository
http://sharpdx.org
MIT License
1.68k stars 642 forks source link

Matrix3x2.ScaleVector calculation is incorrect for rotated transforms #1137

Open SSteve opened 5 years ago

SSteve commented 5 years ago

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