Asset - Extension GMLinear: Easy matrix and vector operations for GMS 2.x and 1.4

Discussion in 'Marketplace' started by FrostyCat, Apr 21, 2019.

  1. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,444
    GMLinear
    Easy matrix and vector operations for GMS 2.x and 1.4


    Overview
    GMLinear is an implementation of matrix and vector operations in pure GML. You can use it to simplify many common calculations in 2D and 3D geometry and implement algorithms/formulas involving linear algebra.

    Features
    • Easy to use, intuitive constructors for building vectors and matrices quickly
    • Vector operations: Addition, subtraction, dot product, cross product, scalar-vector product, norms, distances, unit vectors, linear interpolation, projection/rejection, coordinate system conversions
    • Matrix operations: Addition, subtraction, scalar-matrix product, matrix-matrix product, matrix-vector product, transpose, inverse
    • Hard-coded optimized functions available for 2D, 3D and 4D vectors and 2x2, 3x3 and 4x4 matrices
    Downloads

    GMS 2.x: YoYo Marketplace | GitHub | Documentation
    GMS 1.4: Download | GitHub | Documentation

    Example

    Find the intersection point between the plane containing the point (0, 0, 1) with a normal of (1, 0, 0), and the line containing the point (4, 9, -4) and pointing in the direction (-1, 3, 0.5). (see: Line-plane intersection)

    With GMLinear:
    Code:
    p_0 = r3(0, 0, 1);
    n = r3(1, 0, 0);
    l_0 = r3(4, 9, -4);
    l = r3(-1, 3, 0.5);
    d = r3_dot(r3_subtract(p_0, l_0), n)/r3_dot(l, n);
    intersection = r3_add(l_0, r3_scale(l, d));
    show_message("Intersection point is: (" + r3_encode_string(intersection) + ")");
    
    Without GMLinear:
    Code:
    p_0x = 0;
    p_0y = 0;
    p_0z = 1;
    nx = 1;
    ny = 0;
    nz = 0;
    l_0x = 4;
    l_0y = 9;
    l_0z = -4;
    lx = -1;
    ly = 3;
    lz = 0.5;
    d = (((p_0x-l_0x)*nx)+((p_0y-l_0y)*ny)+((p_0z-l_0z)*nz))/(lx*nx+ly*ny+lz*nz);
    intersectionx = l_0x+d*lx;
    intersectiony = l_0y+d*ly;
    intersectionz = l_0z+d*lz;
    show_message("Intersection point is: (" + string(intersectionx) + ", " + string(intersectiony) + ", " + string(intersectionz) + ")");
    

    Feedback Welcome!
    If you have any suggestions for new constructors/use cases or bug reports, please open an issue or contribute on GitHub.
     
  2. Walky

    Walky Member

    Joined:
    Oct 13, 2018
    Posts:
    89
    Nice, thanks for sharing this.
     
  3. BCrash

    BCrash Member

    Joined:
    Aug 5, 2016
    Posts:
    22
    This will come in handy. Thanks for sharing! :)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice