(Gamemaker Studio) How to draw a vector?

Let's say I want to start from the center pixel of a screen of indeterminate size, and I want to be able to draw an arrow from this center pixel to any other pixel.

The only way I know how to do that is to assign entirely new sprites for each and every possible direction and magnitude of the vector...which might work (even if it's tedious) for around 10 or even 20 possible combinations, but much more and it would really chug a system with the switch statement alone to find which sprite to use.

So, the obvious, much better, way of doing this is just to dynamically draw from one point to another...I don't know how to though.

• You mean, you want to draw a line with a pointer?
– DH.
Jul 24, 2016 at 0:36

You can draw whatever you want on the screen by using the draw_* functions related to primitive shapes instead of sprites.

As example, we can dinamically draw the velocity vector of any object by considering the built-in variables x, y, hspeed and vspeed, or alternatively the variables speed (which is the magnitude of the combination of hspeed and vspeed) and direction. In the first case, we can draw such vector as follows:

draw_arrow(x,y,x+hspeed,y+vspeed,5);


You can multiply hspeed and vspeed for a given constant if you want arrows to be drawn longer, depending on speed values:

var rt = 2.5;
draw_arrow(x,y,x+rt*hspeed,y+rt*vspeed,5);


If you are working with a magnitude (like speed) and an angle (like direction), or similars, you need to add just few more lines:

// MAG can be any magnitude
// DIR can be any 0-360 angle
var vec_x = lengthdir_x(mag,dir);
var vec_y = lengthdir_y(mag,dir);
draw_arrow(x,y,x+vec_x,y+vec_y,5);


Matter is, you can draw arrows, lines, or even circles (assuming your vector as the circumference radius), etcetera. Also, you can change colour and line style to best fit your game appearance.

• Thanks significantly. I'll look in to the documentation for the draw_* (and specifically draw_arrow functions. Jul 25, 2016 at 1:31