MathUtils (math library luau)

Hello guys! Today I decided to open source my module script named “MathUtils”. This module script is a large math library that contains calculating distances from a point to a line, segment, and plane, projecting and reflecting vectors, finding the closest points on geometric primitives, intersecting rays, segments, and planes, bilinear and trilinear interpolation, a 3x3 rotation matrix, and quaternions (Quaternions themselves are used in this module as a tool for working with orientation, operations with bases, and solving quadratic and cubic equations).
Use it for free! GL all.

05.18.2026: Updated MathUtils! Enjoy.

Github: setmetatable24/MathUtils.luau at main · setmetatable24/setmetatable24 · GitHub

7 Likes

This is a linear algebra library, not really a math library

Linear algebra (3x3 matrices) takes up less than 5% of the code in the module. MathUtils also includes: quaternions (spatial rotation algebra), collision system (computational geometry), physically based smoothing (numerical methods), interpolation and easing (numerical analysis), SDF and sphere tracing (field theory), noise and FBM (procedural generation), Euclidean geometry on the plane and in space, ballistics, color and UV manipulation. The linear algebra functions alone are not enough to classify the entire library as a “linear algebra library.” This is a universal mathematical toolkit, but it is NOT limited to school arithmetic.

1 Like

I would consider a lot of those linear algebra, but I was just saying. Guess math would be more appropriate though now that I look at it

Also to give some actual feedback, I’d highly suggest dividing up these individual mathematical concepts into their own modules. This is not entirely cosmetic either, LSPs can struggle with modules larger than 1k lines.

Thanks for your opinion! Personally, my tests don’t fail (I wrote tests for this module). The division into submodules is more for the “convenience” of those who will use it. But more advanced developers can customize it however they like.

lots of stuff in there. very good work my friend :head_shaking_vertically:

1 Like

does this library have Slerp ?

1 Like

Sure, it contains slerp. function MathUtils.quaternionSlerp it is on 112 line.

are you possibly able to make a vector3 version of it?

your vector3 clamp function is redundant, you can use vector.clamp
https://create.roblox.com/docs/reference/engine/libraries/vector#clamp

1 Like

thats in vector though not Vector3

The vector library supports working with Vector3s so that distinction does not matter.

huh thats interesting..
ive definitely tried to use vectors with Vector3s before and i was getting errors (which is why i made that distinction), but now i just tried it again and its working fine.

1 Like

Superficial criticism? You’re referring to vector.clamp, which in Luau is a Vector2 method and is NOT applicable to Vector3. Even if Roblox had Vector3.clamp(), it would likely accept a single upper limit like math.clamp, but that function performs a component-wise clamp between two vectors (minvalue and maxvalue), which is a more complex and frequently necessary operation. The very essence of my modular script is to provide a SINGLE, consistent set of mathematical tools that works with my ecosystem of types and styles. If you found an equivalent, well done, but encapsulation in a library provides control over edge cases, error handling, and is independent of external API changes.

(Sorry for my bad english)

Its alr there!
function MathUtils.vector3ClampMagnitude(value: Vector3, maxMagnitude: number): Vector3

It does exactly what Vector2:Clamp() does — caps the vector’s length to maxMagnitude. Works identically to the built-in Luau method, but for Vector3.

There’s also a component-wise clamp if you need to constrain each axis individually: MathUtils.vector3Clamp(value: Vector3, minValue: Vector3, maxValue: Vector3): Vector3

So, test it out!

Their criticism is actually very much valid because internally Vector3 is actually vector and in turn means all vector library methods work on Vector3. Did you even actually look into what vector was before firing off that response?

1 Like

:pensive_face: the funnier thing in roblox a vector is just a array with 3 or 2 index’s, If I do recall you can add two arrays together or multiple them or whatever you want and treat them like a vector

You’re right, I’m wrong. But I designed my library to be API-independent, so this makes it easy to port MathUtils to other environments without vector.clamp. It’s not particularly useful for Roblox, but still. Ty for criticism!

1 Like