A C++ math library for game development, written from scratch as a solo school project over two years. It provides vectors, matrices, quaternions, 2D/3D geometric primitives and collision tests, and is used as the math foundation of all my other projects (software rasterizer, 3D game engine, animation programming).
- Overview
- Core Features
- Usage
- Collisions
- Unit Tests
- Implementation Highlights
- Development History
- Tech Stack
- Build
LibMath translates the formulas learned in math classes into code that is usable in a video game project, while building an intuition for their performance cost. It is split into two CMake targets:
- LibMath: the library itself, exposed under the
LibMathnamespace - UnitTest: an application that tests the library exhaustively
The library has no external dependency: only <cmath>, <climits>, <cfloat> and compiler intrinsics are allowed.
It compiles in Debug and Release with /Wall /WX (all warnings treated as errors).
- Angle conversion between degrees and radians
- Vectors:
Vector2,Vector3,Vector4- Constructors, accessors,
operator[], comparison, arithmetic operators - Dot product, cross product, magnitude, normalization, homogenization (4D)
- Angle between vectors, projection, reflection,
lerp
- Constructors, accessors,
- Matrices: 2x2, 3x3 and 4x4
- Determinant, minors, cofactors, adjugate, transpose, inverse
- Matrix x matrix, matrix x vector and matrix x scalar products
- Factory functions: identity, translation, rotation (X / Y / Z or around a center), scale, full transform, perspective projection
- Quaternions
- Axis-angle construction, conjugate, inverse, normalization
- Vector rotation, conversion to matrix, angle between quaternions
slerp
- 2D geometry:
Point2D,Line2D,Rectangle,Circle - 3D geometry:
Point3D,Line3D(ray),Plane,Prism,Sphere,Capsule - Collision detection in 2D and 3D (see below)
#include <LibMath/Vector/Vector3.h>
#include <LibMath/Matrix/Matrix4.h>
using namespace LibMath;
Vector3 direction(1.f, 2.f, 3.f);
direction.normalize();
Matrix4 transform = Matrix4::createTransform(
Vector3(0.f, 1.f, 0.f), // translation
Vector3(0.f, 1.f, 0.f), // axis
Degree(90.f), // angle
Vector3(1.f)); // scale| 2D | Point | Line | Rectangle | Circle |
|---|---|---|---|---|
| Point | X | X | X | |
| Line | X | X | X | |
| Rectangle | X | |||
| Circle | X |
| 3D | Point | Ray | Plane | Sphere | Capsule | Prism |
|---|---|---|---|---|---|---|
| Point | X | X | X | |||
| Ray | X | X | X | X | ||
| Plane | X | X | X | |||
| Sphere | X | X | ||||
| Capsule | X | X |
The UnitTest application covers every function of the library: constructors, operators, math operations, geometry accessors and collisions.
The library must pass every test before each tagged delivery.
Floating-point comparisons use a tolerance-based comparison rather than ==, as recommended for IEEE-754 floats.
- No dependencies: pure C++ with the standard math header only
- Warning-free: builds with
/Wall /WXin Debug and Release. Three purely informational MSVC warnings that/Wallenables are suppressed with/wd:C5045: compiler inserted Spectre mitigation (informational)C4464: relative include path contains..(used for the library's include layout)
- Float-safe comparisons: combined absolute and relative tolerance for equality checks, including comparisons around zero
- Clean structs: structs only hold data, constructors, accessors and overloaded operators
- Quaternion rotations: rotation uses quaternions rather than Euler angles, and
slerpgives mathematically correct interpolation - Reusable: designed as a standalone dependency for every later project (rasterizer, platformer engine, skeletal animation)
The library grew with the school year, with one git tag per delivery:
| Tag | Content |
|---|---|
Algorithmie |
Angle conversion, 2D vectors, 2D matrices, 2D geometry and collisions |
Rasterizer |
3D and 4D vectors, 3D matrices (3x3, 4x4) |
Platformer |
3D geometry and 3D collisions |
Animation |
Quaternions |
Written in C++ on Windows, built with CMake.
| Purpose | Library |
|---|---|
| Math functions | C++ standard library (<cmath>) |
| Build system | CMake |
No third-party library is used.
The project uses CMake.
- Windows
- CMake 3.24 or newer
- Visual Studio 2022 (MSVC)
git clone https://github.com/Potatoes251/LibMath.git
cd LibMath
cmake -S . -B build
cmake --build build --config Releasebuild/Release/UnitTest.exe(For a Debug build, use --config Debug and run from build/Debug/.)
Add the repository as a subdirectory and link the LibMath target:
add_subdirectory(LibMath)
target_link_libraries(MyProject PRIVATE LibMath)- Both Debug and Release build with
/Wall /WX, minus the three suppressed warnings listed in Implementation Highlights. - Build outputs (
build/,.exe,.obj,.vs/) are excluded from the repository via.gitignore.