How do you use Matrix.CreatePerspectiveFieldOfView?

I’m attempting to perform a perspective projection of Vector3's onto a 2D plane via a Matrix created with

Matrix m = Matrix.CreatePerspectiveFieldOfView((float)Math.PI / 2, 1, 1, 100)

by calling

Vector3 v2 = Vector3.Transform(v1, m)

but v1.Z never has any bearing on v2.X or v2.Y; it’s as though I’m just doing an orthographic projection.

Am I doing something wrong?

When you transform a vector with a perspective matrix, you should use a Vector4 with the W-component set to 1.

Matrix m = Matrix.CreatePerspectiveFieldOfView((float)Math.PI / 2, 1, 1, 100);
Vector4 v2 = Vector4.Transform(new Vector4(v1, 1), m);
v2 /= v2.W;

The final division by v2.W is needed to get the perspective effect.

3 Likes