Home / Forums / How to calculate 3D Field of View (FOV) and Angle Delta in C++

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Tutorial

How to calculate 3D Field of View (FOV) and Angle Delta in C++

AimbotMath
Trigonometry & Smoothing
MEMBER
Rep: 285
Join Date: Nov 2023
Posts: 27
Thanks: 73
3y ago · Aug 14, 2023 1:10 PM
#1
Calculating the angular distance (FOV delta) between your current camera view angles and a target entity is essential for targeting logic and smooth mouse interpolation.

The Mathematical Formula:
1. Compute the direction vector:
CPP
delta = targetPos - cameraPos

2. Calculate target Pitch & Yaw:
CPP
float pitch = -atan2(delta.z, hypot(delta.x, delta.y)) * (180.0f / M_PI);
float yaw   =  atan2(delta.y, delta.x) * (180.0f / M_PI);

3. Normalize angle differences to [-180, 180] degrees:
CPP
Vector2 NormalizeAngles(Vector2 angle) {
    while (angle.x > 89.0f)  angle.x -= 180.0f;
    while (angle.x < -89.0f) angle.x += 180.0f;
    while (angle.y > 180.0f) angle.y -= 360.0f;
    while (angle.y < -180.0f) angle.y += 360.0f;
    return angle;
}

4. Calculate Euclidean FOV:
CPP
float GetFOV(const Vector2& currentAngles, const Vector2& targetAngles) {
    Vector2 deltaAngle = NormalizeAngles({ targetAngles.x - currentAngles.x, targetAngles.y - currentAngles.y });
    return sqrt(deltaAngle.x * deltaAngle.x + deltaAngle.y * deltaAngle.y);
}
AimbotMath · Angular Normalization & Smooth Angles
The following users thanked AimbotMath for this post:
VectorByte
Graphics & DirectX Dev
VIP
Rep: 420
Join Date: Aug 2022
Posts: 39
Thanks: 115
3y ago · Aug 14, 2023 3:40 PM
#2
Clean and concise! Normalizing the yaw angle is critical when the target crosses the 180 / -180 degree boundary behind you, otherwise the FOV delta will spike to 359 degrees.
VectorByte | DirectX 11/12 Hooking & ImGui Overlays
Quote
Math is the language of game engines.
MatrixRecon
3D Math & Vectors
MEMBER
Rep: 225
Join Date: Jan 2025
Posts: 33
Thanks: 55
3y ago · Aug 14, 2023 6:22 PM
#3
Using
CPP
hypot(delta.x, delta.y)
instead of manual
CPP
sqrt(dx*dx + dy*dy)
avoids numerical overflow on large distances as well. Great reference snippet!
MatrixRecon · 3D Mathematics & View Matrix Calculations