Gaming Gecko
Gaming Gecko

Reputation: 53

Multiply Matrix and Vector4 XNA

How do I multiply a Matrix by a Vector4 in XNA/monogame?

        Matrix a = Matrix.Identity;
        Vector4 b = Vector4.Zero;
        b = a*b;

This gives an error. This should be allowed afaik. Do I really need to make my own function to multiply a vector by a matrix???

Upvotes: 1

Views: 257

Answers (1)

Dave Cousineau
Dave Cousineau

Reputation: 13198

There are a bunch of static Transform methods on Vector4 which are probably what you're looking for. In particular this one.

public static Vector4 Transform(Vector4 value, Matrix matrix)
Matrix a = Matrix.Identity;
Vector4 b = Vector4.Zero;
Vector4 c = Vector4.Transform(b, a);

The implementation looks similar to the following (presented in terms of the above variables).

c.X = b.X * a.M11 + b.Y * a.M21 + b.Z * a.M31 + b.W * a.M41;
c.Y = b.X * a.M12 + b.Y * a.M22 + b.Z * a.M32 + b.W * a.M42;
c.Z = b.X * a.M13 + b.Y * a.M23 + b.Z * a.M33 + b.W * a.M43;
c.W = b.X * a.M14 + b.Y * a.M24 + b.Z * a.M34 + b.W * a.M44;

Upvotes: 1

Related Questions