0

I am working on a game mod and I need to calculate a field of view setting, based on a subject's distance from the camera. I have already written a test programme (basically a programme that creates a camera at a fixed distance from the subject and then lets me manually adjust FOV to the value that I think is best) that has given me the following values:

  • 1 - 90
  • 2 - 60
  • 3 - 40
  • 4 - 30
  • 5 - 24
  • 6 - 20

Where the first number is the distance from the camera and the second value is the FOV setting I need to use to keep the subject at full height within the screen. The distance units are fictional and non-specific and the FOV values have no real connection to real-world lenses.

I can't see any obvious relationship between the values, so I suspect this requires a formula or equation to define one. Maths of this kind is not my strong suit and I am not even sure where to start to formulate something that fits the situation.

I have a fallback plan of creating a coarse resolution lookup table, i.e. for every .1 distance change, store a value but I would prefer a smoother transition between values if possible.

So how would I go about formulating a function that maps itself to these defined values?

Nico Schertler
  • 32,049
  • 4
  • 39
  • 70
  • This question is probably better suited to [Programmers](http://programmers.stackexchange.com/), which is for more general programming questions, or [Mathematics](http://math.stackexchange.com/). This doesn't really have anything to do with C# specifically. – smead Aug 25 '16 at 22:44
  • You want to *interpolate* between these values. *Linear interpolation* is easy to implement, and should look fine. Or you could try *polynomial interpolation*, if you want a smoother curve. – Blorgbeard Aug 25 '16 at 22:44
  • 1
    [This answer](http://stackoverflow.com/a/30438865/1193647) should give you a starting point (at least to decide how you're going to interpolate between the points you specified) – Kolichikov Aug 25 '16 at 23:29
  • @smead - Yeah, sorry about that I thought I was in the right section... first question and I get it wrong. –  Aug 26 '16 at 22:15
  • @Kolichikov Thanks, I will have a look at that link. –  Aug 26 '16 at 22:16

2 Answers2

0

The height of the screen at depth d is

h = 2d * tan(fov / 2)

, where fov is the vertical field of view.

You can solve for fov with

fov = 2 * arc tan(h / 2d)
Nico Schertler
  • 32,049
  • 4
  • 39
  • 70
  • that seems like a more flexible approach than trying to determine a fixed curve for a variable scenario... I will give that a try, thank you. –  Aug 26 '16 at 22:20
0

Nico's answer above is good - if you would like a visual idea of where the tan function comes from, you can imagine the camera as sitting at half the person's height.

The formula comes from making a triangle from the camera to the person's waist, and to the ground. At his waist, the angle of the triangle is 90 degrees. pov is the angle as viewed (at the camera) between the person's head and feet. (This means that half of the angle is between the feet and waist, which is our triangle).

As the person gets further away, we know the height (h) stays the same, and using the definition of tan for right-angled triangles gives you the equation between pov/2 (the angle at the camera), h/2 (the height from the ground to the person's waist) and d the distance along the ground from the camera to the person.

Kevin C
  • 324
  • 1
  • 14