I have an existing codebase that uses Matrix.CreateOrthographicOffCenter() to draw lists of primitives, which make up a very 2D undulating landscape.
The code to create the matrix looks like:
cameraPosition.X - screenSize.X / scaleFactor / 2f,
cameraPosition.X + screenSize.X / scaleFactor / 2f,
cameraPosition.Y - screenSize.Y / scaleFactor / 2f,
cameraPosition.Y + screenSize.Y / scaleFactor / 2f,
There is a scaleFactor to figure in zooming. I never set a view matrix, leaving it to the default value. This has all been working very well so far.
I've decided I need a 3D effect to further develop my game - I want the base of the landscape to appear closer to the camera. I've been looking into using CreatePerspectiveOffCenter() instead, but I couldn't for the life of me figure out how to use it to create a similar view. I was looking through Reimer's tutorials & found he usually used CreatePerspectiveFieldOfView() and a view matrix, so I tried with that. I can actually get it rendering exactly what I want, just using magic numbers I don't really understand:
return Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), screenSize.X / screenSize.Y, 0.01f, 1000);
and a view matrix:
return Matrix.CreateLookAt(new Vector3(cameraPosition.X, cameraPosition.Y, 870),
new Vector3(cameraPosition.X, cameraPosition.Y, -10), Vector3.Up);
Not sure if you can see my highlighted numbers (1000 in the first code block, 870 in the second), but why do they have to be so big? Why cant I just use a perspective of 0.001 to 10 like before? Anyway, problems occur if I try to zoom my view, which is a feature built in to that original CreateOrthographicOffCenter() code. If I try:
return Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45 + ((1 / scaleFactor) - 1) * 30), screenSize.X / screenSize.Y, 0.01f, 1000);
there is another magic number (30). The problem here is this multiplication is generally too little at small zoom levels, and too much at large zoom levels. I think it increases exponentially instead of linearly.
So my question really is - how do I get 3d primitives displaying with perspective, instead of orthographically, but still keeping the same coordinate system (with zoom) my game currently uses? Thanks for any help!