• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Basic Vector Functions in 2.3 (proper way?)

samspade

Member
I'm working on translating my current vector scripts from 2.2 to 2.3. The basics seem to be working, but I'm not very experienced with the new functions and methods. So if anyone with more experience has a moment to look this over and let me know if it looks right, and is the proper way to do it now, I'd appreciate it.

Everything below is located in a script file.

GML:
function vector(_x, _y) constructor {
    x = _x;
    y = _y;
  
    static set = function(_x, _y) {
        x = 0;
        y = 0;
    }
  
    static add = function(_other) {
        x += _other.x;
        y += _other.y;
    }
  
    static subtract = function(_other) {
        x -= _other.x;
        y -= _other.y;
    }
  
    static multiply = function(_scalar) {
        x *= _scalar;
        y *= _scalar;
    }

    static normalize = function() {
        if ((x != 0) || (y != 0)) {
            var _factor = 1/sqrt((x * x) + (y *y));
            x = _factor * x;
            y = _factor * y; 
        }
    }
  
    static set_magnitude = function(_scalar) {
        normalize();
        multiply(_scalar); 
    }
  
    static limit = function(_limit) {
        if (point_distance(0, 0, x, y) > _limit) {
            set_magnitude(_limit);
        }
    }

}
 
Last edited:

kburkhart84

Firehammer Games
It seems fine, though it could use a few more features. I'm thinking maybe some things that consider the use of the Vector as a position in space. Considering that, you could add some things like distance, etc... An alternative is to make a math library of sorts and just use these Vectors in it.
 

samspade

Member
It is very basic at the moment, I just wanted to make sure I was setting it up properly before I started adding everything over.
 

kburkhart84

Firehammer Games
The initial setup seems fine. You are using a constructor, and some static functions. One thing to consider IMO...maybe make it a Vector2, or a Vector2d. If you ever make a 3d one you want to differentiate. Another thing to consider...if you want it to be really robust, you could have it where your constructor can take 0, 1, or 2 arguments. If it is zero, then just set the Vector to 0,0. If there is one argument, check if its another Vector, and if so, set the new one equally. And of course if its two arguments, do what you have now. This shouldn't really be anything that would mess with performance, since it is only upon creation.

But yes, I'd consider more functions, both for using the Vector as a position, and for interacting with other Vectors. Something you might consider(and I will likely do when I make my struct Math library of stuff), is to look at the documentation for another engine(Unity is a good choice). See what all their Vectors do, what all they interact with, etc... and do a lot of those things, maybe skipping a few that simply aren't applicable to Gamemaker.

In my case, I'm going to eventually have a Vector2, Vector3, Quaternion, and Matrix, as far as basic math assets. Each one of those will be able to operate with each other, for example rotating a Vector3 by a Quaternion. The Matrix is likely to be assumed to be a "uniform" matrix and simply be made of 3 Vectors, or I may make it a 2d array(don't know yet). But the idea/point I getting at is to consider the uses of the thing and other related things, and eventually make more if it is something you could use.
 
Top