Mathfs: An Enhanced Mathematics Library for Unity
Mathfs is a versatile mathematics library designed to enhance Unity's capabilities by providing developers with a broad range of additional mathematical functions. Developed by Freya, this library encapsulates her personal collection of math functionalities, intended for use in Unity projects. It represents a dynamic resource that she continuously refines, without extensive backward compatibility considerations, making it a vibrant and evolving tool for developers.
Installation
Integrating Mathfs into your Unity project is straightforward, with several installation options:
- Plain Install: Simply clone or download the repository and place it within the Assets folder of your Unity project.
- Unity Package Manager (UPM): Edit the Packages/manifest.json file of your project and add:
"com.acegikmo.mathfs": "https://github.com/FreyaHolmer/Mathfs.git#0.1.0",
for a stable, specific version."com.acegikmo.mathfs": "https://github.com/FreyaHolmer/Mathfs.git",
for the latest commits but less assurance of stability.
For more on using UPM with git, check Unity’s manual.
- OpenUPM: With openupm-cli installed, execute
openupm add com.acegikmo.mathfs
to add Mathfs.
Once installed, you can access the library by including the namespace using Freya
in your scripts.
Features
Mathfs offers a comprehensive suite of features crafted for various mathematical needs:
- 2D Intersection Tests: Capable of determining intersections between rays, line segments, lines, and circles.
- Curves & Splines: Support for various types of curves including Bézier, Hermite, Catmull-Rom, and more, allowing for detailed trajectory calculations.
- Trajectory Math: Provides methods to compute projectile aspects like speed, range, and height potential under the influence of gravity.
- Triangle and Polygon Math: Handy tools for calculating angles, areas, and checking point inclusion within shapes.
- Circle Math: Functions for generating circles from points and converting between radius and area/circumference.
- Additional Tools: Includes vector extensions, root finders, remap functions, and color extensions which enhance color manipulation capabilities, among others.
Changes from Unity’s Mathf
Mathfs adapts Unity's Mathf to better suit specific needs, with key differences including:
- All angles use radians to maintain consistency.
- Lerp and InverseLerp are now unclamped by default, providing more numerically stable calculations.
- Traditional functions like Smoothstep are replaced with more explicit alternatives like LerpSmooth and InverseLerpSmooth.
- Min/Max functions now throw exceptions if given empty inputs, preventing default returns of 0.
Conclusion
Mathfs is a robust tool for any Unity developer seeking advanced mathematical functionality beyond the standard Mathf offerings. Its extensive feature set supports complex mathematical operations, making it an excellent companion for game development and simulations within Unity.