Skip to content

AuburnSounds/godot-math

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

99 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

godot-math

CI

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 transform

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 pure nothrow @nogc @safe
  • LDC, GDC (untested), DMD
  • Uses numem and the C runtime only
Type Status Description Documentation
Vector2 2D float vector, or size, or point Vector2
Vector3 3D float vector, or size, or point Vector3
Vector4 4D float vector or point Vector4
Vector2i 2D int vector, or size, or point Vector2i
Vector3i 3D int vector, or size, or point Vector3i
Vector4i 4D int vector or point Vector4i
Rect2 2D float rectangle Rect2
Rect2i 2D int rectangle Rect2i
Transform2D 2x3 matrix (column-major) Transform2D
Basis 3x3 matrix (row-major) Basis
Transform3D 3x4 matrix (row-major) Transform3D
Projection 4x4 matrix (column-major) Projection
Plane 3D plane Plane
AABB 3D bounding box AABB

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()

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages