A lightweight, header-only C++20 math library built on top of GLM. It provides expressive type aliases, bounding-box primitives (AABB & OBB), and ray-intersection utilities — ideal for game engines, physics simulations, and spatial queries.
- Vectors —
Vector2/3/4type aliases (int,float,double) with concept-constrained templates andstd::formatsupport. - Matrices —
Matrix2/3/4type aliases with the same concept constraints and formatting. - AABB — Axis-Aligned Bounding Box with containment, intersection, merge, enclose, corners, lerp, closest-point, distance, scale/expand, operators, and type casting.
- OBB — Oriented Bounding Box with Euler-angle rotation, local/world transforms, corners, closest-point, distance, and AABB conversion.
- Ray — Ray casting against AABB and OBB with hit/miss, distance, position, surface normal, and inside-flag.
- Configurable namespace — Wrap everything in a custom namespace (default:
mth) via a CMake option. std::formatintegration — All vector, matrix, and bounding-box types are directly formattable.
include(FetchContent)
FetchContent_Declare(
cpp-math-essentials
GIT_REPOSITORY https://github.com/pablobh2147/cpp-math-essentials.git
GIT_TAG main
)
FetchContent_MakeAvailable(cpp-math-essentials)
target_link_libraries(your_target PRIVATE math_essentials)add_subdirectory(path/to/cpp-math-essentials)
target_link_libraries(your_target PRIVATE math_essentials)Note: GLM is required as a dependency of your project when using this library. You can provide it yourself or let your build system fetch it.
By default the library wraps all symbols in the mth namespace. Override it with:
set(MATH_ESSENTIALS_NAMESPACE my_namespace CACHE STRING "" FORCE)#include "Vector.hpp"
#include "BoundingBox.hpp"
#include "Ray.hpp"
#include <format>
#include <iostream>
int main() {
using namespace mth;
// Vectors
Vector3f position(1.0f, 2.0f, 3.0f);
std::cout << std::format("Position: {}\n", position);
// AABB
AABBf box(Vector3f(0.0f), Vector3f(10.0f));
bool inside = box.Contains(position);
// Ray intersection
Rayf ray(Vector3f(-5.0f, 1.0f, 1.0f), Vector3f(1.0f, 0.0f, 0.0f));
RayIntersectionf hit = ray.Intersect(box);
if (hit) {
std::cout << std::format("Hit at {} (distance: {})\n", hit.position, hit.distance);
}
// OBB
OBBf obb(Vector3f(0.0f), Vector3f(1.0f), Vector3f(0.0f, glm::radians(45.0f), 0.0f));
RayIntersectionf obb_hit = ray.Intersect(obb);
std::cout << std::format("Hit info: {}\n", obb_hit);
return 0;
}cmake -B build -DBUILD_TESTS=ON
cmake --build build
ctest --test-dir buildTests use Google Test (v1.14.0) and GLM (v1.0.1), both fetched automatically via CMake FetchContent.
- C++20-compatible compiler (GCC ≥ 12, Clang ≥ 15, MSVC ≥ 19.29)
- CMake ≥ 3.14
- GLM (fetched automatically for tests, must be provided by the consumer project otherwise)
This project is licensed under the MIT License.
Copyright (c) 2026 Pablo Bermejo Hernández