A dependency-free, lightweight, fast math library for 2D and 3D vectors, quaternions, and matrices in Swift with (optional) SIMD support. It is developed and maintained as part of the Fireblade Game Engine project.
- Vectors:
Vec2,Vec3,Vec4forFloat,Double,Int, andUInt. - Matrices:
Mat2x2,Mat3x3,Mat4x4forFloatandDouble. - Quaternions:
Quat4fandQuat4d. - Geometric Types:
Point,Size,Rect. - Math Functions: A comprehensive set of functions including
sin,cos,tan,asin,acos,atan2,dot,cross,normalize,reflect,refract, and many more. - SIMD Support: Leveraging hardware acceleration where available, with a fallback implementation when SIMD is not supported.
- Swift 6 Ready: Fully compatible with Swift 6 and
Sendableconformance.
- Swift 6.1 or higher
- Swift Package Manager (SPM)
Add Fireblade Math as a dependency to your Package.swift file:
// swift-tools-version: 6.1
import PackageDescription
let package = Package(
name: "YourPackageName",
dependencies: [
.package(url: "https://github.com/fireblade-engine/math.git", from: "1.0.0")
],
targets: [
.target(
name: "YourTargetName",
dependencies: ["FirebladeMath"])
]
)Starting with version 1.0.0, FirebladeMath uses Swift traits to manage SIMD support. By default, SIMD is enabled on Apple platforms. You can manually control it in your package configuration if needed.
import FirebladeMath
let v1 = Vec3f(1, 2, 3)
let v2 = Vec3f(4, 5, 6)
let dotProduct = dot(v1, v2)
let crossProduct = cross(v1, v2)
let unitVector = normalize(v1)import FirebladeMath
// Create a transformation matrix
var modelMatrix = Mat4x4f.identity
modelMatrix.translate(by: [10, 20, 30])
modelMatrix.rotate(by: .pi / 2, axis: [0, 1, 0])
// Project a point
let projection = Mat4x4f.perspectiveRH(fovy: .pi / 4, aspect: 16/9, zNear: 0.1, zFar: 100)import FirebladeMath
let q = Quat4f(angle: .pi / 4, axis: [0, 1, 0])
let rotatedVector = q * Vec3f(1, 0, 0)If you want to contribute please see the CONTRIBUTION GUIDE first.
git clone git@github.com:fireblade-engine/math.gitcd mathmake setupEnvironment
Before committing code please ensure to run:
make precommit
This project is currently maintained by @ctreffs.
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE file for details.