Skip to main content

Math Utilities

The math namespace provides essential mathematical functions for game development, including angle calculations, vector transformations, world-to-screen projection, and various utility functions.

Constants

math::util::pi
constexpr long double
Pi constant with long double precision
math::util::pi_f
constexpr float
Pi constant as float for performance-critical calculations

Angle and Vector Functions

angle_vector

math::vec3 angle_vector(const math::vec3& angle);
Converts an angle to a directional vector.
angle
const math::vec3&
Input angle (pitch, yaw, roll)
Returns: Forward directional vector

angle_vectors

void angle_vectors(
    const math::vec3& angles, 
    math::vec3* fw, 
    math::vec3* rg = nullptr, 
    math::vec3* up = nullptr
);
Converts angles to directional vectors (forward, right, up).
angles
const math::vec3&
Input angles (pitch, yaw, roll)
fw
math::vec3*
Output forward vector (required)
rg
math::vec3*
default:"nullptr"
Output right vector (optional)
up
math::vec3*
default:"nullptr"
Output up vector (optional)

vector_angles

void vector_angles(math::vec3& fw, math::vec3& angles);
Converts a forward vector to angles.
fw
math::vec3&
Input forward vector
angles
math::vec3&
Output angles (pitch, yaw, roll)

calculate_angle

math::vec3 calculate_angle(const math::vec3& src, const math::vec3& dst);
Calculates the angle between two 3D positions.
src
const math::vec3&
Source position
dst
const math::vec3&
Destination position
Returns: Angle from source to destination

Projection and Transformation

world_to_screen

bool world_to_screen(const math::vec3 origin, math::vec3& screen);
Projects a 3D world position to 2D screen coordinates.
origin
const math::vec3
World position to project
screen
math::vec3&
Output screen coordinates (x, y)
Returns: true if position is visible on screen, false otherwise
This function uses the game’s view matrix for projection. Returns false if the position is behind the camera.

vector_transform

vec3 vector_transform(vec3 vector, matrix_3x4 matrix);
Transforms a vector by a 3x4 matrix.
vector
vec3
Input vector to transform
matrix
matrix_3x4
Transformation matrix
Returns: Transformed vector

matrix_position

math::vec3 matrix_position(const math::matrix_3x4& src);
Extracts position from a transformation matrix.
src
const math::matrix_3x4&
Source transformation matrix
Returns: Position vector extracted from matrix

Angle Utilities

clamp_angle

void clamp_angle(math::vec3& angle);
Clamps angles to valid ranges (pitch: -89 to 89, yaw: -180 to 180).
angle
math::vec3&
Angle to clamp (modified in-place)

normalize_yaw

float normalize_yaw(float yaw);
Normalizes a yaw angle to the range [-180, 180].
yaw
float
Yaw angle to normalize
Returns: Normalized yaw angle

approach_angle

float approach_angle(float to, float from, const float speed);
Smoothly interpolates from one angle to another.
to
float
Target angle
from
float
Starting angle
speed
const float
Interpolation speed
Returns: Interpolated angle

snap_yaw

float snap_yaw(float value);
Snaps a yaw value to discrete angles.
value
float
Yaw value to snap
Returns: Snapped yaw value

FOV Calculation

get_fov

template<typename T = float>
T get_fov(const math::vec3& view_angles, const math::vec3& start, const math::vec3& end);
Calculates the field of view (FOV) between view angles and a target.
view_angles
const math::vec3&
Current view angles
start
const math::vec3&
Starting position
end
const math::vec3&
Target position
Returns: FOV angle in degrees

Trigonometric Functions

sin_cos

void sin_cos(const float a, float* s, float* c);
Calculates sine and cosine simultaneously for better performance.
a
const float
Angle in radians
s
float*
Output sine value
c
float*
Output cosine value

Fast Math Functions

Optimized approximations for performance-critical code:
namespace math::util {
    void fast_sqrt(float* __restrict p_out, float* __restrict p_in);
    float fast_asin(float x);
    float fast_sin(float x);
    float fast_cos(const float x);
}
Fast math functions trade some accuracy for performance. Use for non-critical calculations where speed is more important than precision.

Utility Functions

min / max

template<typename T>
constexpr T min(const T& t1, const T& t2);

template<typename T, typename... ts_>
constexpr T min(const T& t1, const T& t2, ts_&&... ts);

template<typename T>
constexpr T max(const T& t1, const T& t2);

template<typename T, typename... ts_>
constexpr T max(const T& t1, const T& t2, ts_&&... ts);
Variadic min/max functions supporting multiple arguments.

random

template<typename T>
constexpr T random(const T min, const T max);
Generates a random value between min and max.
min
const T
Minimum value (inclusive)
max
const T
Maximum value (inclusive)
Returns: Random value in range [min, max]

Angle Conversion

constexpr float rad2deg(const float x);
constexpr float deg2rad(const float x);
Convert between radians and degrees.
float degrees = math::rad2deg(1.57f); // ~90 degrees

Example Usage

// Calculate angle to target
math::vec3 my_pos(100, 200, 50);
math::vec3 target_pos(500, 300, 60);
math::vec3 angle = math::calculate_angle(my_pos, target_pos);
math::clamp_angle(angle);

Build docs developers (and LLMs) love