Github | 0.0.1
What is mathsm?
mathsm is a extensible, modular and powerful math library that goes far beyond what Roblox currently offers.
Whether you’re creating physics simulations, neural networks, custom rendering systems, or anything in between and beyond mathsm can help you.
Features
- Full Vector Support
- Dot/cross products, normalization, projection, transformations, and support for homogeneous coordinates.
- Full Matrix Support
- all operations, inverses, RREF, ranks, LU decomposition, determinant, transpose and more.
- Transform Utilities
- Create transformation matrices from position, rotation and scale.
Examples
Example 1 - Transforming a Vector
In this example, we’ll create a simple transformation matrix that translates a vector by (10, 0, 0), rotates it 90degs around the Z-axis and lastly scales it (no scalling in this example so it’s (1, 1, 1)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local mathsm = require(ReplicatedStorage.mathsm.Initializer)
local types = require(ReplicatedStorage.mathsm.Types)
type vec = types.Vec<{number}>
type matrix = types.Matrix<{number}>
-- Original vector (Vec3)
local Vec: vec = mathsm.Vec.new({2, 4, 1})
-- Position offset (translation)
local positionVec: vec = mathsm.Vec.new({10, 0, 0})
-- Rotation 90° around Z-axis
local rotationMatrix: matrix = mathsm.Matrix.new({
{ 0, -1, 0, 0 },
{ 1, 0, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 }
})
-- Scaling (1,1,1 means no scale change)
local scaleVec: vec = mathsm.Vec.new({1, 1, 1}, "Scalevec")
-- Create a transformation matrix
local transform = mathsm.Matrix.fromTransform(positionVec, rotationMatrix, scaleVec)
-- Convert Vec3 to homogeneous coordinates, then transform it
local transformed = transform * Vec:toHomogeneous()
-- Print result: should now be moved + rotated
print(transformed) -- {6, 2, 1, 1} - in homogeneous
Example 2 - Finding the Closest Point on a Line
Let’s say you have a moving player and a ray beam defined by two points a and b.
You want to find the closest point on that beam to the player’s position.
This could be useful for detecting if a player is near a path, AI navigation and etc..
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local mathsm = require(ReplicatedStorage.mathsm.Initializer)
local types = require(ReplicatedStorage.mathsm.Types)
type vec = types.Vec<{number}>
-- Ray segment from point A to B
local A: vec = mathsm.Vec.new({0, 0, 0}) -- player
local B: vec = mathsm.Vec.new({10, 0, 0}) -- ray
-- Player position (moving around)
local playerPos: vec = mathsm.Vec.new({7, 3, 0})
-- Vector from A to B
local AB = B - A
-- Vector from A to player
local AP = playerPos - A
-- Project AP onto AB to find closest point
local projection = AP:project(AB)
-- Closest point = A + projection
local closestPoint = A + projection
-- Print result
print("Closest Point:", closestPoint:value()) -- (7, 0, 0)
Benchmarks
mathsm did well through all the tests
| Benchmark | Total Time (s) | Avg Time (s) | Runs |
|---|---|---|---|
| Vec Dot | 1.137325 | 0.00000114 | 1,000,000 |
| Vec Mul | 4.649543 | 0.00000465 | 1,000,000 |
| Vec Div | 29.306327 | 0.00002931 | 1,000,000 |
| Vec Sub | 9.767209 | 0.00000977 | 1,000,000 |
| Vec Add | 9.815857 | 0.00000982 | 1,000,000 |
| Vec3 Cross | 30.207469 | 0.00003021 | 1,000,000 |
| Vec Lerp | 13.671228 | 0.00001367 | 1,000,000 |
| Vec Project | 17.218875 | 0.00001722 | 1,000,000 |
| Vec Distance | 14.720008 | 0.00001472 | 1,000,000 |
| Vec Angle-Between | 10.104772 | 0.00001010 | 1,000,000 |
| Vec IsParallel | 42.740762 | 0.00004274 | 1,000,000 |
| Vec Equals | 14.320398 | 0.00001432 | 1,000,000 |
| Vec ToHomogeneous | 9.155959 | 0.00000916 | 1,000,000 |
| Matrix Mul 4x4 (Vec Bench) | 43.355540 | 0.00004336 | 1,000,000 |
| Matrix Addition | 5.012368 | 0.00005012 | 100,000 |
| Matrix Subtraction | 1.072155 | 0.00001072 | 100,000 |
| Matrix Multiplication (4x4) | 0.188731 | 0.00001887 | 10,000 |
| Matrix Scalar Division | 0.120677 | 0.00001207 | 10,000 |
| Matrix Determinant | 0.167449 | 0.00003349 | 5,000 |
| Matrix Inverse | 1.375827 | 0.00068791 | 2,000 |
| Matrix RREF | 0.057053 | 0.00002853 | 2,000 |
| LU Decompose | 0.067873 | 0.00003394 | 2,000 |
| Forward Substitution | 0.038110 | 0.00000381 | 10,000 |
| Backward Substitution | 0.039185 | 0.00000392 | 10,000 |
| Matrix Transpose | 0.069743 | 0.00000697 | 10,000 |
| Matrix Rotate 90 | 0.401704 | 0.00004017 | 10,000 |
| Matrix Clone | 0.741801 | 0.00000742 | 100,000 |
| Matrix Trace | 0.016283 | 0.00000163 | 10,000 |
| Matrix Fill | 0.015259 | 0.00000153 | 10,000 |
| Matrix Is Zero | 0.055135 | 0.00000055 | 100,000 |
| Matrix Flatten | 0.050093 | 0.00000501 | 10,000 |
| Matrix Get Rank | 0.058860 | 0.00002943 | 2,000 |
| Matrix Shape | 0.205024 | 0.00000205 | 100,000 |
| Matrix Is Square | 0.246277 | 0.00000246 | 100,000 |
All benchmarks were done without utilizing parallel luau as 0.0.1 does not support it.
Installation
Currently the quickest and easiest way to get your hands on mathsm is by installing the latest release 0.0.1 from the github releases here
Developer notes
- Solo Project. This is a one-person project, maintained entirely by me.
- Driven by Passion. mathsm was created out of pure interest in math, programming, and Roblox development.
- Ongoing Development. I actively improve and expand mathsm whenever I have time, so expect updates, optimizations, and new features in future releases.
