Skip to content

Repository files navigation

Slate Engine - Math

License Java Status Latest Release Downloads Stars

An immutable linear algebra library powering the Slate Engine


Features

  • Immutable vectors (Vector2, Vector3, Vector4)
  • Immutable matrices (Matrix2, Matrix3, Matrix4)
  • Float and double precision
  • Common linear algebra operations
  • Relative epsilon comparisons
  • Linear interpolation
  • Generated source code

Installation

Requirements

  • Java JDK 21+
  • Gradle 9.3.0+ (included)
  • Git

Currently, Slate Math is not on Maven Central. To use it, clone the repository and publish it to your local Maven repository.

Linux / MacOS

git clone https://github.com/breadcat-dev/slate-math.git
cd slate-math
./gradlew publishToMavenLocal

Windows

git clone https://github.com/breadcat-dev/slate-math.git
cd slate-math
./gradlew.bat publishToMavenLocal

Once installed, add the dependency:

Groovy

implementation "cat.breadcat.slate:math:<version>"

Kotlin

implementation("cat.breadcat.slate:math:<version>")

Usage

import cat.breadcat.math.matrices.Matrix4d;
import cat.breadcat.math.vectors.Vector3d;


public class Main
{
    public static void main(String[] args)
    {
        Vector3d vector = Vector3d.of(1, 2, 3);
        Matrix4d matrix = Matrix4d.identity().multiply(2);

        System.out.println(vector);
        System.out.println(matrix);
    }
}

Examples

Vectors

Vector3d v1 = Vector3d.of(1, 2, 3);
Vector3d v2 = Vector3d.of(4, 5, 6);

Vector3d v3 = v1.add(v2).multiply(2);
double dot = v3.dot(v1);
Vector3d normal = v3.normalize();

System.out.println(v3);
System.out.println(dot);
System.out.println(normal);

Console:

Vector3d (+10.000000, +14.000000, +18.000000)
92.0
Vector3d (+0.401610, +0.562254, +0.722897)

Matrices

Matrix3d m1 = Matrix3d.of(
        1, 2, 3,
        4, 5, 6,
        7, 8, 9
);

Matrix3d m2 = Matrix3d.identity();

Matrix3d result = m1.multiply(m2);
System.out.println(result);

Console:

Matrix3d
[
    [+1.000000, +2.000000, +3.000000]
    [+4.000000, +5.000000, +6.000000]
    [+7.000000, +8.000000, +9.000000]
]

Matrices * Vectors

Matrix2d matrix = Matrix2d.of(
        2, 0,
        0, 2
);
Vector2d vector = Vector2d.of(3, 4);

Vector2d result = matrix.multiply(vector);
System.out.println(result);

Console:

Vector2d (+6.000000, +8.000000)

Matrix inverse

Matrix2d matrix = Matrix2d.of(
        4, 7,
        2, 6
);

double determinant = matrix.determinant();
Matrix2d inverse = matrix.inverse();

System.out.println(determinant);
System.out.println(inverse);

Console:

10.0
Matrix2d
[
    [+0.600000, -0.700000]
    [-0.200000, +0.400000]
]

Approximately equal

double almostOne = 1 - 1e-10;
Matrix2d matrix = Matrix2d.of(
        almostOne, 0,
        0, almostOne
);

if(DoubleMath.approximatelyEqual(almostOne, 1, MathConstants.EPSILON))
    System.out.println("almostOne ~= 1");
if(matrix.approximatelyEqual(Matrix2d.identity()))
    System.out.println("matrix ~= identity");

Console:

almostOne ~= 1
matrix ~= identity

Constants

Vector3d origin = Vector3d.zero();
Vector3d up = Vector3d.unitY();

Matrix4d identity = Matrix4d.identity();

Roadmap

  • Matrix transformations (translation, rotation, scale)
  • Improved tests
  • Performance optimizations
  • Quaternions

Dependencies

none

License

PolyForm Noncommercial License 1.0.0