godot-math 0.0.4

Port of Godot vectors, matrices, quaternions, rectangles to D


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

godot-math

godot-math is a D port of Godot's linear algebra with unchanged semantics.

➡️ See Godot Math documentation

Using DUB you can add it to your project with:

"dependencies":
{
    "godot-math": "~>1.0"
}
dependency "godot-math" version="~>1.0"

Examples

Transform a point in 2D:

import godotmath;

// Create a Vector2
Vector2 v = Vector2(2.0f, 3.0f);

// Create a Transform2D: rotation by 45 degrees and translate by (1, 1)
float angle = GM_PI / 4;
Transform2D t2d = Transform2D(angle, Vector2(1, 1));
Vector2 v2 = t2d * v; // Apply the transform

Transform a point in 3D:

import godotmath;

auto proj = Projection.create_perspective(60.0f, 16.0f/9.0f, 0.1f, 100.0f);

// Transform a 3D point
Vector4 point = Vector4(5.0f, 3.0f, 10.0f, 1.0f);
Vector4 projected = proj * point;  // Apply projection transform

The Projection matrix handles the perspective divide automatically, converting 3D world coordinates to normalized device coordinates for rendering.

Why?

Godot's math API is well-tested and documented.

Godot's math bring helpful semantics to small vectors and matrices. A Transform2D/Transform3D is scaling + translation + rotation without shearing. A Projection is a 4x4 matrix scaling with potentially perspective transform. A Basis is a 3x3 matrix that only deals with base change / rotation.

Features

  • one .d file
  • Everything is fully pure nothrow @nogc @safe.
  • LDC, GDC, DMD
TypeStatusDescriptionDocumentation
Vector22D float vector, or size, or pointVector2
Vector33D float vector, or size, or pointVector3
Vector44D float vector or pointVector4
Vector2i2D int vector, or size, or pointVector2i
Vector3i3D int vector, or size, or pointVector3i
Vector4i4D int vector or pointVector4i
Rect22D float rectangleRect2
Rect2i2D int rectangleRect2i
Transform2D2x3 matrix (column-major)Transform2D
Basis3x3 matrix (row-major)Basis
Transform3D3x4 matrix (row-major)Transform3D
Projection4x4 matrix (column-major)Projection
Plane3D Plane matrixPlane
AABB3D bounding boxAABB

Basis: Row-major storage (but conceptually column-major, stored transposed) Transform2D: Column-major storage Projection: Column-major storage

Math functions

Note: When a float function exist, a double overload also exist with identical name.

float gm_abs(float x);
int   gm_abs(int x);
float gm_acos(float x);
float gm_acosh(float x);
float gm_angle_difference(float from, float to);
float gm_asin(float x);
float gm_asinh(float x);
float gm_atan(float x);
float gm_atan2(float y, float x);
float gm_bezier_derivative(float start, float control_1, float control_2, float end, float t);
float gm_bezier_interpolate(float start, float control_1, float control_2, float end, float t);
float gm_ceil(float x);
float gm_clamp(float value, float min, float max);
int   gm_clamp(int value, int min, int max);
float gm_cos(float x);
float gm_cubic_interpolate(float from, float to, float pre, float post, float weight);
float gm_cubic_interpolate_in_time(float from, float to, float pre, float post, float weight, float to_t, float pre_t, float post_t);
float gm_deg_to_rad(float y);
float gm_floor(float x);
float gm_fmod(float x, float y);
float gm_fposmod(float x, float y);
bool  gm_is_equal_approx(float left, float right);
bool  gm_is_equal_approx(float left, float right, float tolerance);
bool  gm_is_finite(float x);
bool  gm_is_zero_approx(float value);
float gm_lerp(float from, float to, float weight);
float gm_lerp_angle(float from, float to, float weight);
bool  gm_likely(bool b);
bool  gm_unlikely(bool b);
float gm_rad_to_deg(float y);
float gm_round(float x);
int   gm_sign(int v);
float gm_sign(float v);
bool  gm_signbit(float num);
bool  gm_signbit(double num);
float gm_sin(float x);
float gm_snapped(float value, float step);
int   gm_snapped(int value, int step);
float gm_sqrt(float x);
void  gm_swap(T)(ref T a, ref T b);
float gm_tan(float x);

Differing names

  • Both float and double versions exist at once, with a ***d suffix (eg: Projectiond, Vector2d, Rect2d).
  • In global scope, function symbols get a gm_ prefix.
  • For vectors:
  • .clampf/.clampi replaced by overloaded .clamp
  • .snappedf / .snappedi replaced by overloaded .snapped.
  • .minf/.mini replaced by overloaded .min
  • .maxf/.maxi replaced by overloaded .max

Small semantic differences

  • Rect2/Rect2i/Rect2d have a .merge_non_empty method that, in case of a union with an empty rectangle, return the other rectangle.
  • Bonus methods for rectangles, such has .left, .top, .right, .bottom, .scale.
  • Projection inverse is less precise than in original Godot. You can always go double to get more precise .inverse()
Authors:
  • Juan Linietsky
  • Ariel Manzur
  • Guillaume Piolat
Dependencies:
numem
Versions:
0.0.5 2026-Jan-06
0.0.4 2026-Jan-04
0.0.3 2026-Jan-03
0.0.2 2026-Jan-03
0.0.1 2026-Jan-02
Show all 6 versions
Download Stats:
  • 6 downloads today

  • 54 downloads this week

  • 191 downloads this month

  • 191 downloads total

Score:
1.8
Short URL:
godot-math.dub.pm