Skip to content

A dependency free, lightweight, fast math library for 2D and 3D vectors, quaternions and matrices in Swift with (optional) SIMD support.

License

Notifications You must be signed in to change notification settings

fireblade-engine/math

FirebladeMath

license macOS Linux Windows swift-version-compatibility platform-compatilibilty

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.

✨ Features

  • Vectors: Vec2, Vec3, Vec4 for Float, Double, Int, and UInt.
  • Matrices: Mat2x2, Mat3x3, Mat4x4 for Float and Double.
  • Quaternions: Quat4f and Quat4d.
  • 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 Sendable conformance.

πŸš€ Getting Started

πŸ“‹ Prerequisites

  • Swift 6.1 or higher
  • Swift Package Manager (SPM)

πŸ’» Installation

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"])
    ]
)

πŸ›  SIMD Traits

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.

πŸ“– Usage Examples

Vectors

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)

Matrices

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)

Quaternions

import FirebladeMath

let q = Quat4f(angle: .pi / 4, axis: [0, 1, 0])
let rotatedVector = q * Vec3f(1, 0, 0)

πŸ’ How to contribute

If you want to contribute please see the CONTRIBUTION GUIDE first.

  1. git clone git@github.com:fireblade-engine/math.git
  2. cd math
  3. make 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.

πŸ” License

This project is licensed under the MIT License - see the LICENSE file for details.

β™» Alternatives