3.7. Matrix.Scale

I just upgraded my project to MG 3.7 and got an error for Matrix.Scale.
Why was that removed? And is there a less ugly way of getting Matrix.Scale than:

var scale = Vector3.One;
var rotation = Quaternion.Identity;
var translate = Vector3.One;
matrix.Decompose(out scale, out rotation, out translate);

This seems really wird considering I only need scale here.

If you’re on C# 7, you can use

var scale=Vector3.One
matrix.Decompose (out scale,out _,out _)

“out _” discards the output.

in a posterior subversion of C# 7 (don’t remember which), you can even declare the vector after the out:

matrix.Decompose (out Vector3 scale, out _,out _);

The reason is here ,but honestly I haven’t read it: https://github.com/MonoGame/MonoGame/issues/5553

EDIT: or you can write an extension method to Matrix (i.e. GetScale()) which wraps this code, so you’d only have to call it with matrix.GetScale ();

Thanks, the “_” placeholder was exactly what I was looking for.

By the way:
For C#7 there’s “Deconstruct”. If monogame renamed “Decompose” to “Deconstruct” we’d be able to do the following:

var (scale, rotation, translate) = matrix;
1 Like

They were removed because.

  1. These were not functions that were in xna.
  2. The getter functions on them were incorrect in most contexts.
  3. The setter function on them were improper in most cases.
  4. Essentially the behavior varied on how they were used as even the setter was improper in a general sense, as it didn’t set m44 to 1 in the scalar which in most cases a scale matrix should do.

So this was a case of if your going to break it break it all the way.

You can create extension methods to the matrix class if you need them with clearly defined functionally.

// untested should be fine
public static class Extensions
{
  public static Matrix CreateScale(this Matrix m, float x, float y, float z)
  {
      var m = Matrix.Identity;
      m.M11 = x;
      m.M22 = y;
      m.M33 = z;
      m.M44 = 1f;
      return m;
  }
}
1 Like

Yeah, would be cool to add that :slight_smile: