GML Draw the path that the object will follow.

GGJuanPabon

Member
I would like to have an idea, that some expert illuminate me of what are the functions that I should use to draw and put styles.

I have an object that goes from position A to position B, when I click the object draws the path, and goes to the position of the Click if it is free. So far normal, it works well.

However, the route you draw is painted as part of the object, I mean, draw the route well and the object follows the route well. But the drawing of the route is static and moves with the object.


1) I would like the object to travel the route and as the object travels the route, the route is gradually erased.

I have this drawing.

draw_path(path, x, y, false);
draw_self();

And

draw_set_alpha(0.3);
mp_grid_draw(global.grid);
draw_set_alpha(1);

2) Once I managed to draw the route, I would like to add some style.

This is an example, which I want to reach, a beautiful line draws the path my player will have


 
Last edited:
R

robproctor83

Guest
Well, the easiest way to draw a path is to use the draw_path function, but it's drawing is limited and you would basically be restricted to editing it through a surface or a shader so it's probably not what you actually want.


Otherwise, you need to loop through the path points and draw them from a controller object would be my suggestion. You could draw it from the player and calculate the absolute position, but if the players depth is changing then the drawn path would be changing depths with it which is likely not what you would want from the example image. So, drawing the path from a controller object that you can manage the depth separately.

To draw a path is relatively simple, you just loop over the points and connect them. I don't have the exact code reference because I am not at that computer so take this likely won't work without some tweaks

Code:
 //variable containing your path
mypath = YOUR PATH;
//grab the first x and y position
var _x1 = path_get_x(mypath,0);
var _y1 = path_get_y(mypath,0);
var _x2, _y2;
//loop over the path starting on the second position (index 1) since we already have 0 in the first xy
for(i = 1; i < path_get_number(mypath); i++){
    //grab the next position
    _x2 =  path_get_x(mypath,i);
    _y2 = path_get_y(mypath,i);
    //draw the line using your xy data
    draw_line_width(_x1, _y1, _x2, _y2, 5);
    //set the first xy to the last xy so that it connects
    _x1 = _x2;
    _y1 = _y2;
}
]

Something like that should get you going, but that is still just a basic concept. To get results like in your example it is going to be A LOT more complicated. Rather than drawing the points directly you will need to interpolate easing between them, which isn't hard but you need to look into how that works and the math behind it. The other thing is that instead of just drawing a line you will need to draw primitives. You would make a single straight line with the spark effect on it, and then you would break that image up into triangles and then using triangle math (trigonometry) you would position triangles along the interpolated path.
 

Pep Andorra

Member
path_get_x needs a real between 0 and 1 to retrieve the position.

Its easy use path_get_point_x that use the position between 0 and the path_get_number
 

HeadlessOne

Member
Hey Mate,

the built in paths are the way to go i recon, I actually just recently found this really great tutorial on youtube for drawing pretty paths, Its in GMS 1 but all the code is all the same and it takes you through all the steps of both drawing the pretty lines and also how to manipulate paths in game.
Path Tutorial

Well worth a watch.
 
Top