0
\$\begingroup\$

Currently writing a camera component for FNA (basically XNA), and can't for the life of me figure out why my matrix transform isn't working as expected. This is how I calculate my view & projection matrices:

// projection
// WorldSize is something like 16x9, 20x10, etc. Basically the "simulated" world size.
// Zoom is just a float to zoom the camera.
var left = Zoom * -WorldSize.X / 2;
var right = Zoom * WorldSize.X / 2;
var bottom = Zoom * -WorldSize.Y / 2;
var top = Zoom * WorldSize.Y / 2;
const float near = 0;
const float far = 100;
var projection = Matrix.CreateOrthographicOffCenter(left, right, bottom, top, near, far);

and

// view
// Position is the position of my Camera, e.g. (10, 15), (6.51, 16.612), etc.
var position = new Vector3(Position, 0);
var target = position + Vector3.Forward;
var up = Vector3.Up;
var view = Matrix.CreateLookAt(position, target, up);

// Combine them
var combined = projection * view;

This should, by all the sources I've checked, double-checked, and triple-checked, be correct. However when I apply this matrix to my batch or effects it doesn't show anything at all:

// I would expect a white square to be rendered in the middle of the screen. Since WorldSize is
// 16x9 I would expect a 1x1 square to be clearly visible.
var batch = new SpriteBatch();
batch.begin(/* the rest */, combined);
var texture = new Texture2D(Game.GraphicsDevice, 1, 1);
texture.SetData(new []{Color.White });
batch.Draw(texture, Vector2.Zero, Color.White);
batch.End();

// Also tried just rendering lines, shows nothing
var effect = new BasicEffect(graphicsDevice)
{
    VertexColorEnabled = true,
    View = view,
    Projection = projection
};
effect.CurrentTechnique.Passes[0].Apply();
graphicsDevice.DrawUserPrimitives(PrimitiveType.LineStrip, vertices, 0, vertices.Length - 1);

I have checked so many sources and they all do exactly like this. I even tried copy+pasting the matrix source code for a Java project I made some time back that I know works, and that didn't work either so I don't think the Matrix transforms are to blame. Anyone know what I'm doing wrong?

\$\endgroup\$
2
  • \$\begingroup\$ The most obvious things that are wrong here are that your near plane is at 0, and you seem to be using an ortho projection rather than a perspective projection. \$\endgroup\$ Commented Apr 18, 2021 at 16:18
  • \$\begingroup\$ @MaximusMinimus it's for a 2d game, so I'd think that would be correct. Also those thins wouldn't prevent stuff from showing up completely so I think it's something else :/ \$\endgroup\$ Commented Apr 18, 2021 at 22:24

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.