0
\$\begingroup\$

This is my code that draws a top down view tile based map. But Iam do not know how high the camera needs to be placed in the sky so that the map is fully visible so that nothing is cut away and I do not see any borders at the screen border.

I tried to use the formula "tan(angle) = a/b" where "angle" I guess is my FOV (45 degrees) and "a" my screenwidth and "b" the wanted height but it did not return the correct result. Is this the correct formula or can the problem lie somewhere else?

        float aspectRatio = graphicsDevice.Viewport.AspectRatio;
        float fieldOfView = MathHelper.ToRadians(45);

        //var height = camera.WindowSize.X / MathF.Tan(FieldOfView); // this does not give the right result
        float height = 2410; // found out experimentally, todo how to calculate this?
        var camPos = new Vector3(camera.ScaledWindowSize.X * 0.5f, camera.ScaledWindowSize.Y * 0.5f, height);
        var camTarget = camPos;
        camTarget.Z = 0;

        float nearClip = 0.1f;
        float farClip = 10000.0f;

        var mProjection = Matrix.CreatePerspectiveFieldOfView(
            fieldOfView,
            aspectRatio,
            nearClip,
            farClip);

        var mView = Matrix.CreateLookAt(camPos, camTarget, Vector3.Up)
            * camera.GetViewMatrix()
            * Matrix.CreateScale(1, -1, 1);
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

If your field of view is measured from the bottom of the frustum to the top, then at a distance d your frustum is h units tall, where:

h = 2 * d * tan(fieldOfView/2)

So you can solve this to get d:

d = h / (2 * tan(fieldOfView/2))

If your field of view is measured from the camera's forward axis to the top of the frustum (half the angle) then use tan(fieldOfView) instead of tan(fieldOfView/2). Unfortunately both conventions are in use in different engines/libraries, and the documentation is often less explicit than it could be about which one to expect.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .