I was wondering if anyone could help me with this little problem. What I am actually trying to do is implement 'picking' into a game I'm doing, but I'm actually stuck with some of the maths, thats why this isn't in the game forum.

I'm trying to work out a field of view for a camera. If you look at the picture below, I'm trying to work out angle x while knowing the length of sides a and b.


My brother knows a bit about maths and he said you do it like this:x = Tan-1 (b/a)

Anyone have a clue how to do this in code? I'm not familliar at all with math functions so it would be a great help.

Dani AI

Generated

Quick clarification for : if the triangle is right-angled at the camera and a is the adjacent side (distance along the view axis) while b is the opposite side (offset to the edge), the camera half-angle is the inverse tangent of opposite/adjacent. and pointed toward the right functions — below is a compact, safe C++ approach and a few practical gotchas for using it in a picking/FOV context.

#include <cmath>
constexpr double PI = 3.14159265358979323846;

double a = /* distance to plane */;
double b = /* half-width on plane */;

// half-angle in radians (atan2 handles a == 0 and sign of b)
double halfAngleRad = std::atan2(b, a);

// convert to degrees if needed
double halfAngleDeg = halfAngleRad * 180.0 / PI;
double fullFovDeg = 2.0 * halfAngleDeg;

Troubleshooting notes: ensure a and b are floating-point (avoid integer division). std::atan2(y,x) is preferred because it avoids divide-by-zero and yields the correct sign/quadrant; plain atan(y/x) can misbehave if a is zero. The trig functions return radians — convert to degrees only when needed for UI/debugging. If b represents full screen width, use b/2 for the half-angle; for vertical FOV swap in the half-height.

Practical picking hint: compute the camera-space ray by projecting the screen point into normalized device coordinates, unproject with the inverse projection*view matrix, or build the ray using the FOV (half-angle) and aspect ratio. Verifying results with simple cases (e.g., a==1, b==1 should give 45 degrees half-angle) helps catch unit/precision bugs.

Recommended Answers

All 3 Replies

what do you want exactly to find a relationship for x:
if you so

x = tan-1(b/a)

I was wondering if anyone could help me with this little problem. What I am actually trying to do is implement 'picking' into a game I'm doing, but I'm actually stuck with some of the maths, thats why this isn't in the game forum.

I'm trying to work out a field of view for a camera. If you look at the picture below, I'm trying to work out angle x while knowing the length of sides a and b.

[ATTACH]5573[/ATTACH]

My brother knows a bit about maths and he said you do it like this:

a = Tan-1 (b/a)

Anyone have a clue how to do this in code? I'm not familliar at all with math functions so it would be a great help.

Close, but not quite. The formula is this:

tan(x) = b/a

Which leads to this:

x = arctan (b/a), not this:
a = arctan (b/a)

atan or atan2 from the cmath library will probably be your best bets here:

cheers man, great help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.