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
Pi constant with long double precision
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.
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).
Input angles (pitch, yaw, roll)
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.
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.
Returns: Angle from source to destination
world_to_screen
bool world_to_screen ( const math :: vec3 origin , math :: vec3 & screen );
Projects a 3D world position to 2D screen coordinates.
World position to project
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.
vec3 vector_transform ( vec3 vector , matrix_3x4 matrix );
Transforms a vector by a 3x4 matrix.
Input vector to transform
Returns: Transformed vector
matrix_position
math :: vec3 matrix_position ( const math :: matrix_3x4 & src );
Extracts position from a transformation matrix.
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 to clamp (modified in-place)
normalize_yaw
float normalize_yaw ( float yaw );
Normalizes a yaw angle to the range [-180, 180].
Returns: Normalized yaw angle
approach_angle
float approach_angle ( float to , float from , const float speed );
Smoothly interpolates from one angle to another.
Returns: Interpolated angle
snap_yaw
float snap_yaw ( float value );
Snaps a yaw value to discrete angles.
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.
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.
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.
Minimum value (inclusive)
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.
Radians to Degrees
Degrees to Radians
float degrees = math :: rad2deg ( 1.57 f ); // ~90 degrees
Example Usage
Angle Calculation
World to Screen
FOV Check
// 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);