Available

amalgame-math-vec

Vec3 / Vec4 / Mat4 column-major (OpenGL).

amc package add math-vec GitHub ↗ ← Back to the ecosystem

Overview

Linear algebra for graphics and games: 3D and 4D vectors, 4×4 matrices. Matrices are column-major, the OpenGL convention. Angles are in radians.

Features

  • ✓Vec3: Add, Sub, Scale, Dot, Cross, Length, Normalize, Equals
  • ✓Vec4: Add, Sub, Scale, Dot; W is 1 for a point, 0 for a direction
  • ✓Column-major Mat4: Identity, Multiply, TransformVec4, Get, Set
  • ✓Builders: Mat4.Translate, Scale, RotateX, RotateY, RotateZ
  • ✓Written in Amalgame; only libm is called (sqrt, sin, cos)

Example

Minimal usage example — copy-paste runnable after install.

amc package add math-vec

import Amalgame.Math.Vec

class Program {
    public static void Main() {
        let a = new Vec3(1.0, 2.0, 3.0)
        let b = new Vec3(4.0, 5.0, 6.0)
        Console.WriteLine(String_FromFloat(a.Dot(b)))            // 32
        Console.WriteLine(String_FromFloat(a.Cross(b).GetY()))   // 6

        // rotate 90° around Z, then move 5 along X
        let model: Mat4 = Mat4.Translate(5.0, 0.0, 0.0).Multiply(Mat4.RotateZ(1.5707963))
        let p: Vec4 = model.TransformVec4(new Vec4(1.0, 0.0, 0.0, 1.0))
        Console.WriteLine(String_FromFloat(p.GetX()) + " " + String_FromFloat(p.GetY()))  // 5 1
    }
}

At a glance

Related packages