How to use a 'extension method' to the Matrix class.

Does anyone know how i can extend into the matrix struct.
Is this something to do with the aliasing to microsoft.xna.framework.

This doesn’t seem to be seen how the heck do you do this lol.

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MyMgHelperStructsAndExtentions
{
    using Microsoft.Xna.Framework;
    
    // how many times has this screwed me up now.
    // you know what i ill just toss a extension method in here
    //
    // or not !
    //public struct Extensions {
    public static class Extensions
    {
        //public static Matrix CreateLookAtForward(ref Matrix m, Vector3 cameraPosition, Vector3 cameraForward, Vector3 cameraUpVector)
        public static Matrix CreateLookAtForward(this Matrix m, Vector3 cameraPosition, Vector3 cameraForward, Vector3 cameraUpVector)
        {
            Matrix result;
            var cameraTarget = cameraPosition + cameraForward * 10f;
            var vector = Vector3.Normalize(cameraPosition - cameraTarget);
            var vector2 = Vector3.Normalize(Vector3.Cross(cameraUpVector, vector));
            var vector3 = Vector3.Cross(vector, vector2);
            result.M11 = vector2.X;
            result.M12 = vector3.X;
            result.M13 = vector.X;
            result.M14 = 0f;
            result.M21 = vector2.Y;
            result.M22 = vector3.Y;
            result.M23 = vector.Y;
            result.M24 = 0f;
            result.M31 = vector2.Z;
            result.M32 = vector3.Z;
            result.M33 = vector.Z;
            result.M34 = 0f;
            result.M41 = -Vector3.Dot(vector2, cameraPosition);
            result.M42 = -Vector3.Dot(vector3, cameraPosition);
            result.M43 = -Vector3.Dot(vector, cameraPosition);
            result.M44 = 1f;
            return result;
        }

Extension methods can only be defined for member methods, not static methods. It looks like you are wanting to do

var matrix = Matrix.CreateLookAtForward(camPos, camFwd, camUp);

The extension method would be used like this

var someOtherMatrix = new Matrix();
var matrix = someOtherMatrix.CreateLookAtForward(camPos, camFwd, camUp);

even though it ignores someOtherMatrix, which is not you want.

A small trick:

Make the namespace Microsoft.Xna.Framework.
This way the extension is always available without having to add using MyMgHelperStructsAndExtentions on each file.

Ah well that just ruins it i wanted to do it the first way lol.
damn i was hoping there was a way around it.
Then again it would be pretty cool to just type…

       view = worldCamera.CreateLookAtForward(up);

… edit oh i see… i didn’t catch your meaning at first.

oh that’s a cool trick, Thanks nkast

Can’t you do that with a child class?

What he was wanting to do was Matrix.CreateLookAtForward(...) with the emphasis on the method being a member of the Matrix type. The static method itself could live anywhere, and there is no need for a derived type. It was the convenience and cleanliness of having it as a member of the Matrix type, but with a static method that is not possible.